Update dbapi instrumentation
Fix span name for dbapi
Decode db user for pymysql
@@ -143,7 +143,7 @@ def wrap_connect(
def unwrap_connect(
- connect_module: typing.Callable[..., typing.Any], connect_method_name: str,
+ connect_module: typing.Callable[..., typing.Any], connect_method_name: str
):
"""Disable integration with DB API library.
https://www.python.org/dev/peps/pep-0249/
@@ -251,8 +251,7 @@ class DatabaseApiIntegration:
args: typing.Tuple[typing.Any, typing.Any],
kwargs: typing.Dict[typing.Any, typing.Any],
):
- """Add object proxy to connection object.
- """
+ """Add object proxy to connection object."""
connection = connect_method(*args, **kwargs)
self.get_connection_attributes(connection)
return get_traced_connection_proxy(connection, self)
@@ -278,6 +277,9 @@ class DatabaseApiIntegration:
self.database = self.database.decode(errors="ignore")
self.name += "." + self.database
user = self.connection_props.get("user")
+ # PyMySQL encodes this data
+ if user and isinstance(user, bytes):
+ user = user.decode()
if user is not None:
self.span_attributes["db.user"] = str(user)
host = self.connection_props.get("host")
@@ -325,8 +327,10 @@ class TracedCursor:
span.set_attribute(
"component", self._db_api_integration.database_component
)
- span.set_attribute("db.type", self._db_api_integration.database_type)
- span.set_attribute("db.instance", self._db_api_integration.database)
+ span.set_attribute(
+ "db.system", self._db_api_integration.database_component
+ )
+ span.set_attribute("db.name", self._db_api_integration.database)
span.set_attribute("db.statement", statement)
for (
@@ -344,9 +348,16 @@ class TracedCursor:
*args: typing.Tuple[typing.Any, typing.Any],
**kwargs: typing.Dict[typing.Any, typing.Any]
):
+ name = ""
+ if args:
+ name = args[0]
+ elif self._db_api_integration.database:
+ name = self._db_api_integration.database
+ else:
+ name = self._db_api_integration.name
with self._db_api_integration.get_tracer().start_as_current_span(
- self._db_api_integration.name, kind=SpanKind.CLIENT
+ name, kind=SpanKind.CLIENT
) as span:
self._populate_span(span, *args)
try:
