|
55 | 55 | cnx.commit()
|
56 | 56 | cursor.close()
|
57 | 57 | cnx.close()
|
| 58 | + |
| 59 | + instrumented_connection = MySQLClientInstrumentor.instrument_connection( |
| 60 | + cnx, |
| 61 | + enable_commenter=True, |
| 62 | + commenter_options={ |
| 63 | + "db_driver": True, |
| 64 | + "mysql_client_version": True, |
| 65 | + "driver_paramstyle": False |
| 66 | + } |
| 67 | + ) |
| 68 | + cursor = instrumented_connection.cursor() |
| 69 | + cursor.execute("INSERT INTO test (testField) VALUES (123)" |
| 70 | + instrumented_connection.commit() |
| 71 | + cursor.close() |
| 72 | + instrumented_connection.close() |
58 | 73 |
|
59 | 74 | For example,
|
60 | 75 | ::
|
@@ -162,12 +177,29 @@ def instrument_connection(
|
162 | 177 | """Enable instrumentation in a mysqlclient connection.
|
163 | 178 |
|
164 | 179 | Args:
|
165 |
| - connection: The connection to instrument. |
166 |
| - tracer_provider: The optional tracer provider to use. If omitted |
167 |
| - the current globally configured one is used. |
168 |
| -
|
| 180 | + connection (MySQLdb.connect or Connection object): |
| 181 | + The MySQL connection instance to instrument. This connection is typically |
| 182 | + created using `MySQLdb.connect()` and needs to be wrapped to collect telemetry. |
| 183 | + tracer_provider (TracerProvider, optional): |
| 184 | + A custom `TracerProvider` instance to be used for tracing. If not specified, |
| 185 | + the globally configured tracer provider will be used. |
| 186 | + enable_commenter (bool, optional): |
| 187 | + A flag to enable the OpenTelemetry SQLCommenter feature. If set to `True`, |
| 188 | + SQL queries will be enriched with contextual information (e.g., database client details). |
| 189 | + Default is `None`. |
| 190 | + commenter_options (dict, optional): |
| 191 | + A dictionary of configuration options for SQLCommenter. This allows you to customize |
| 192 | + metadata appended to queries. Possible options include: |
| 193 | + - `db_driver`: Adds the database driver name and version. |
| 194 | + - `dbapi_threadsafety`: Adds threadsafety information. |
| 195 | + - `dbapi_level`: Adds the DB-API version. |
| 196 | + - `mysql_client_version`: Adds the MySQL client version. |
| 197 | + - `driver_paramstyle`: Adds the parameter style. |
| 198 | + - `opentelemetry_values`: Includes traceparent values. |
| 199 | + Refer to *SQLCommenter Configurations* above for more information |
| 200 | + |
169 | 201 | Returns:
|
170 |
| - An instrumented connection. |
| 202 | + An instrumented MySQL connection with OpenTelemetry support enabled. |
171 | 203 | """
|
172 | 204 |
|
173 | 205 | return dbapi.instrument_connection(
|
|
0 commit comments