Skip to content

Commit db31f28

Browse files
committed
Add docstring for instrument_connection for aiopg and mysqlclient
1 parent 4369a05 commit db31f28

File tree

3 files changed

+53
-10
lines changed
  • instrumentation
    • opentelemetry-instrumentation-aiopg/src/opentelemetry/instrumentation/aiopg
    • opentelemetry-instrumentation-mysql/src/opentelemetry/instrumentation/mysql
    • opentelemetry-instrumentation-mysqlclient/src/opentelemetry/instrumentation/mysqlclient

3 files changed

+53
-10
lines changed

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,19 @@
3535
cnx.close()
3636
3737
pool = await aiopg.create_pool(database='Database')
38+
3839
cnx = await pool.acquire()
3940
cursor = await cnx.cursor()
4041
await cursor.execute("INSERT INTO test (testField) VALUES (123)")
4142
cursor.close()
4243
cnx.close()
43-
44+
45+
cnx = AiopgInstrumentor().instrument_connection(cnx)
46+
cursor = await cnx.cursor()
47+
await cursor.execute("INSERT INTO test (testField) VALUES (123)")
48+
cursor.close()
49+
cnx.close()
50+
4451
API
4552
---
4653
"""

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

+8-4
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,16 @@ def instrument_connection(self, connection, tracer_provider=None):
9292
"""Enable instrumentation in a MySQL connection.
9393
9494
Args:
95-
connection: The connection to instrument.
96-
tracer_provider: The optional tracer provider to use. If omitted
97-
the current globally configured one is used.
95+
connection (mysql.connector.Connection):
96+
The existing MySQL connection instance to instrument. This connection is typically
97+
obtained through `mysql.connector.connect()` and is instrumented to collect telemetry
98+
data about database interactions.
99+
tracer_provider (TracerProvider, optional):
100+
An optional `TracerProvider` instance to use for tracing. If not provided, the globally
101+
configured tracer provider will be automatically used.
98102
99103
Returns:
100-
An instrumented connection.
104+
An instrumented MySQL connection with OpenTelemetry tracing enabled,
101105
"""
102106
return dbapi.instrument_connection(
103107
__name__,

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

+37-5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@
5555
cnx.commit()
5656
cursor.close()
5757
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()
5873
5974
For example,
6075
::
@@ -162,12 +177,29 @@ def instrument_connection(
162177
"""Enable instrumentation in a mysqlclient connection.
163178
164179
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+
169201
Returns:
170-
An instrumented connection.
202+
An instrumented MySQL connection with OpenTelemetry support enabled.
171203
"""
172204

173205
return dbapi.instrument_connection(

0 commit comments

Comments
 (0)