Skip to content

Commit

Permalink
support opentelemetry-instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
methane committed Jan 10, 2025
1 parent 8103651 commit 857fd87
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/MySQLdb/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,20 +196,18 @@ class object, used to create cursors (keyword only)
# PEP-249 requires autocommit to be initially off
autocommit = kwargs2.pop("autocommit", False)

self._set_attributes(*args, **kwargs2)
super().__init__(*args, **kwargs2)

self.cursorclass = cursorclass
self.encoders = {
k: v
for k, v in conv.items()
if type(k) is not int # noqa: E721
}

self.database = kwargs2.get("database", "")

self._server_version = tuple(
[numeric_part(n) for n in self.get_server_info().split(".")[:2]]
)

self.encoding = "ascii" # overridden in set_character_set()

if not charset:
Expand Down Expand Up @@ -240,6 +238,21 @@ class object, used to create cursors (keyword only)
self.autocommit(autocommit)
self.messages = []

def _set_attributes(self, host=None, user=None, password=None, database="", port=3306,
unix_socket=None, **kwargs):
"""set some attributes for otel"""
if unix_socket and not host:
host = "localhost"

Check warning on line 245 in src/MySQLdb/connections.py

View check run for this annotation

Codecov / codecov/patch

src/MySQLdb/connections.py#L245

Added line #L245 was not covered by tests
# support opentelemetry-instrumentation-dbapi
self.host = host
# _mysql.Connection provides self.port
self.user = user
self.database = database
# otel-inst-mysqlclient uses db instead of database.
self.db = database
# NOTE: We have not supported semantic conventions yet.
# https://opentelemetry.io/docs/specs/semconv/database/sql/

def __enter__(self):
return self

Expand Down

0 comments on commit 857fd87

Please sign in to comment.