Skip to content

Commit 8e4cb08

Browse files
calculate_commenter_data at init of DatabaseApiIntegration
1 parent b9fbc50 commit 8e4cb08

File tree

1 file changed

+60
-55
lines changed
  • instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi

1 file changed

+60
-55
lines changed

instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py

Lines changed: 60 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,52 @@ def __init__(
280280
self.name = ""
281281
self.database = ""
282282
self.connect_module = connect_module
283+
self.commenter_data = self.calculate_commenter_data()
284+
285+
def calculate_commenter_data(
286+
self,
287+
):
288+
commenter_data = {}
289+
if not self.enable_commenter:
290+
return commenter_data
291+
292+
db_driver = self.connect_module.__name__
293+
db_version = ""
294+
if db_driver in _DB_DRIVER_ALIASES:
295+
db_version = util_version(_DB_DRIVER_ALIASES[db_driver])
296+
else:
297+
db_version = self.connect_module.__version__
298+
commenter_data = {
299+
"db_driver": f"{db_driver}:{db_version.split(' ')[0]}",
300+
"dbapi_threadsafety": self.connect_module.threadsafety,
301+
"dbapi_level": self.connect_module.apilevel,
302+
"driver_paramstyle": self.connect_module.paramstyle,
303+
}
304+
305+
if self.database_system == "postgresql":
306+
if hasattr(self.connect_module, "__libpq_version__"):
307+
libpq_version = self.connect_module.__libpq_version__
308+
else:
309+
libpq_version = self.connect_module.pq.__build_version__
310+
commenter_data.update(
311+
{
312+
"libpq_version": libpq_version,
313+
}
314+
)
315+
elif self.database_system == "mysql":
316+
mysqlc_version = ""
317+
if db_driver == "MySQLdb":
318+
mysqlc_version = self.connect_module._mysql.get_client_info()
319+
elif db_driver == "pymysql":
320+
mysqlc_version = self.connect_module.get_client_info()
321+
322+
commenter_data.update(
323+
{
324+
"mysql_client_version": mysqlc_version,
325+
}
326+
)
327+
328+
return commenter_data
283329

284330
def wrapped_connection(
285331
self,
@@ -410,7 +456,7 @@ def get_statement(self, cursor, args): # pylint: disable=no-self-use
410456
return statement.decode("utf8", "replace")
411457
return statement
412458

413-
def traced_execution( # pylint: disable=too-many-branches
459+
def traced_execution(
414460
self,
415461
cursor,
416462
query_method: typing.Callable[..., typing.Any],
@@ -432,64 +478,23 @@ def traced_execution( # pylint: disable=too-many-branches
432478
if args and self._commenter_enabled:
433479
try:
434480
args_list = list(args)
435-
db_driver = (
436-
self._db_api_integration.connect_module.__name__
437-
)
438-
db_version = ""
439-
if db_driver in _DB_DRIVER_ALIASES:
440-
db_version = util_version(
441-
_DB_DRIVER_ALIASES[db_driver]
442-
)
443-
else:
444-
db_version = (
445-
self._db_api_integration.connect_module.__version__
446-
)
447-
448-
commenter_data = {
449-
"db_driver": f"{db_driver}:{db_version.split(' ')[0]}",
450-
"dbapi_threadsafety": self._connect_module.threadsafety,
451-
"dbapi_level": self._connect_module.apilevel,
452-
"driver_paramstyle": self._connect_module.paramstyle,
453-
}
454481

482+
# lazy capture of mysql-connector client version using cursor
455483
if (
456-
self._db_api_integration.database_system
457-
== "postgresql"
484+
self._db_api_integration.database_system == "mysql"
485+
and self._db_api_integration.connect_module.__name__
486+
== "mysql.connector"
487+
and not self._db_api_integration.commenter_data[
488+
"mysql_client_version"
489+
]
458490
):
459-
if hasattr(self._connect_module, "__libpq_version__"):
460-
libpq_version = (
461-
self._connect_module.__libpq_version__
462-
)
463-
else:
464-
libpq_version = (
465-
self._connect_module.pq.__build_version__
466-
)
467-
commenter_data.update(
468-
{
469-
"libpq_version": libpq_version,
470-
}
471-
)
472-
elif self._db_api_integration.database_system == "mysql":
473-
mysqlc_version = ""
474-
if db_driver == "mysql.connector":
475-
mysqlc_version = (
476-
cursor._cnx._cmysql.get_client_info()
477-
)
478-
if db_driver == "MySQLdb":
479-
mysqlc_version = (
480-
self._db_api_integration.connect_module._mysql.get_client_info()
481-
)
482-
if db_driver == "pymysql":
483-
mysqlc_version = (
484-
self._db_api_integration.connect_module.get_client_info()
485-
)
486-
487-
commenter_data.update(
488-
{
489-
"mysql_client_version": mysqlc_version,
490-
}
491-
)
491+
self._db_api_integration.commenter_data[
492+
"mysql_client_version"
493+
] = cursor._cnx._cmysql.get_client_info()
492494

495+
commenter_data = dict(
496+
self._db_api_integration.commenter_data
497+
)
493498
if self._commenter_options.get(
494499
"opentelemetry_values", True
495500
):

0 commit comments

Comments
 (0)