|
108 | 108 | """ |
109 | 109 |
|
110 | 110 | import logging |
| 111 | +from importlib import import_module |
111 | 112 | from typing import ( |
112 | 113 | Any, |
113 | 114 | Callable, |
114 | 115 | Collection, |
115 | 116 | Dict, |
| 117 | + Optional, |
116 | 118 | Tuple, |
117 | 119 | ) |
118 | 120 |
|
@@ -172,36 +174,52 @@ def _uninstrument(self, **kwargs): |
172 | 174 | def instrument_connection( |
173 | 175 | self, |
174 | 176 | 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, |
178 | 180 | ): |
179 | | - if not hasattr(connection, "_is_instrumented_by_opentelemetry"): |
180 | | - connection._is_instrumented_by_opentelemetry = False |
| 181 | + """Enable instrumentation in a MySQL connection. |
181 | 182 |
|
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) |
195 | 209 |
|
196 | 210 | def uninstrument_connection( |
197 | 211 | self, |
198 | 212 | connection, |
199 | 213 | ): |
200 | | - connection.cursor_factory = getattr( |
201 | | - connection, _OTEL_CURSOR_FACTORY_KEY, None |
202 | | - ) |
| 214 | + """Disable instrumentation in a MySQL connection. |
203 | 215 |
|
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) |
205 | 223 |
|
206 | 224 |
|
207 | 225 | class DatabaseApiIntegration(dbapi.DatabaseApiIntegration): |
|
0 commit comments