Skip to content

Commit c967d0d

Browse files
Fix mysql-conn instrument_connection and uninstrument
1 parent 0b2340b commit c967d0d

File tree

1 file changed

+40
-22
lines changed
  • instrumentation/opentelemetry-instrumentation-mysql/src/opentelemetry/instrumentation/mysql

1 file changed

+40
-22
lines changed

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

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,13 @@
108108
"""
109109

110110
import logging
111+
from importlib import import_module
111112
from typing import (
112113
Any,
113114
Callable,
114115
Collection,
115116
Dict,
117+
Optional,
116118
Tuple,
117119
)
118120

@@ -172,36 +174,52 @@ def _uninstrument(self, **kwargs):
172174
def instrument_connection(
173175
self,
174176
connection,
175-
tracer_provider=None,
176-
enable_commenter=None,
177-
commenter_options=None,
177+
tracer_provider: Optional[trace_api.TracerProvider] = None,
178+
enable_commenter: bool = False,
179+
commenter_options: dict = None,
178180
):
179-
if not hasattr(connection, "_is_instrumented_by_opentelemetry"):
180-
connection._is_instrumented_by_opentelemetry = False
181+
"""Enable instrumentation in a MySQL connection.
181182
182-
if not connection._is_instrumented_by_opentelemetry:
183-
setattr(
184-
connection, _OTEL_CURSOR_FACTORY_KEY, connection.cursor_factory
185-
)
186-
connection.cursor_factory = _new_cursor_factory(
187-
tracer_provider=tracer_provider
188-
)
189-
connection._is_instrumented_by_opentelemetry = True
190-
else:
191-
_logger.warning(
192-
"Attempting to instrument mysql-connector connection while already instrumented"
193-
)
194-
return connection
183+
Args:
184+
connection: The connection to instrument.
185+
tracer_provider: The optional tracer provider to use. If omitted
186+
the current globally configured one is used.
187+
enable_commenter: Flag to enable/disable sqlcommenter.
188+
commenter_options: Configurations for tags to be appended at the sql query.
189+
190+
Returns:
191+
An instrumented connection.
192+
"""
193+
if isinstance(connection, wrapt.ObjectProxy):
194+
_logger.warning("Connection already instrumented")
195+
return connection
196+
197+
db_integration = DatabaseApiIntegration(
198+
__name__,
199+
self._DATABASE_SYSTEM,
200+
self._CONNECTION_ATTRIBUTES,
201+
version=__version__,
202+
tracer_provider=tracer_provider,
203+
enable_commenter=enable_commenter,
204+
commenter_options=commenter_options,
205+
connect_module=import_module("mysql.connector"),
206+
)
207+
db_integration.get_connection_attributes(connection)
208+
return get_traced_connection_proxy(connection, db_integration)
195209

196210
def uninstrument_connection(
197211
self,
198212
connection,
199213
):
200-
connection.cursor_factory = getattr(
201-
connection, _OTEL_CURSOR_FACTORY_KEY, None
202-
)
214+
"""Disable instrumentation in a MySQL connection.
203215
204-
return connection
216+
Args:
217+
connection: The connection to uninstrument.
218+
219+
Returns:
220+
An uninstrumented connection.
221+
"""
222+
return dbapi.uninstrument_connection(connection)
205223

206224

207225
class DatabaseApiIntegration(dbapi.DatabaseApiIntegration):

0 commit comments

Comments
 (0)