Skip to content

Commit 74b3636

Browse files
committed
Add docstrings for instrument_connection() for psycopg, psycopg2, and pymysql
1 parent db31f28 commit 74b3636

File tree

4 files changed

+71
-9
lines changed
  • instrumentation
    • opentelemetry-instrumentation-mysqlclient/src/opentelemetry/instrumentation/mysqlclient
    • opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2
    • opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg
    • opentelemetry-instrumentation-pymysql/src/opentelemetry/instrumentation/pymysql

4 files changed

+71
-9
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
cursor.close()
5757
cnx.close()
5858
59-
instrumented_connection = MySQLClientInstrumentor.instrument_connection(
59+
instrumented_cnx = MySQLClientInstrumentor.instrument_connection(
6060
cnx,
6161
enable_commenter=True,
6262
commenter_options={
@@ -65,11 +65,11 @@
6565
"driver_paramstyle": False
6666
}
6767
)
68-
cursor = instrumented_connection.cursor()
68+
cursor = instrumented_cnx.cursor()
6969
cursor.execute("INSERT INTO test (testField) VALUES (123)"
70-
instrumented_connection.commit()
70+
instrumented_cnx.commit()
7171
cursor.close()
72-
instrumented_connection.close()
72+
instrumented_cnx.close()
7373
7474
For example,
7575
::

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,18 @@
9292
PsycopgInstrumentor().instrument()
9393
9494
cnx = psycopg.connect(database='Database')
95+
9596
cursor = cnx.cursor()
9697
cursor.execute("INSERT INTO test (testField) VALUES (123)")
9798
cursor.close()
9899
cnx.close()
99-
100+
101+
instrumented_cnx = instrument_connection(cnx)
102+
cursor = instrumented_cnx.cursor()
103+
cursor.execute("INSERT INTO test (testField) VALUES (123)")
104+
cursor.close()
105+
instrumented_cnx.close()
106+
100107
API
101108
---
102109
"""
@@ -196,6 +203,18 @@ def _uninstrument(self, **kwargs):
196203
# TODO(owais): check if core dbapi can do this for all dbapi implementations e.g, pymysql and mysql
197204
@staticmethod
198205
def instrument_connection(connection, tracer_provider=None):
206+
"""Enable instrumentation in a psycopg connection.
207+
208+
Args:
209+
connection: psycopg.Connection
210+
The psycopg connection object to be instrumented.
211+
tracer_provider: opentelemetry.trace.TracerProvider, optional
212+
The TracerProvider to use for instrumentation. If not provided,
213+
the global TracerProvider will be used.
214+
215+
Returns:
216+
An instrumented psycopg connection object.
217+
"""
199218
if not hasattr(connection, "_is_instrumented_by_opentelemetry"):
200219
connection._is_instrumented_by_opentelemetry = False
201220

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,17 @@
9292
Psycopg2Instrumentor().instrument()
9393
9494
cnx = psycopg2.connect(database='Database')
95+
9596
cursor = cnx.cursor()
9697
cursor.execute("INSERT INTO test (testField) VALUES (123)")
9798
cursor.close()
9899
cnx.close()
99100
101+
instrumented_cnx = Psycopg2Instrumentor.instrument_connection(cnx)
102+
cursor = instrumented_cnx.cursor()
103+
cursor.execute("INSERT INTO test (testField) VALUES (123)")
104+
cursor.close()
105+
instrumented_cnx.close()
100106
API
101107
---
102108
"""
@@ -160,6 +166,19 @@ def _uninstrument(self, **kwargs):
160166
# TODO(owais): check if core dbapi can do this for all dbapi implementations e.g, pymysql and mysql
161167
@staticmethod
162168
def instrument_connection(connection, tracer_provider=None):
169+
"""Enable instrumentation in a psycopg2 connection.
170+
171+
Args:
172+
connection: psycopg2.extensions.connection
173+
The psycopg2 connection object to be instrumented.
174+
tracer_provider: opentelemetry.trace.TracerProvider, optional
175+
The TracerProvider to use for instrumentation. If not specified,
176+
the global TracerProvider will be used.
177+
178+
Returns:
179+
An instrumented psycopg2 connection object.
180+
"""
181+
163182
if not hasattr(connection, "_is_instrumented_by_opentelemetry"):
164183
connection._is_instrumented_by_opentelemetry = False
165184

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@
5656
cnx.commit()
5757
cursor.close()
5858
cnx.close()
59+
60+
instrumented_cnx = PyMySQLInstrumentor().instrument_connection(
61+
cnx,
62+
enable_commenter=True,
63+
commenter_options={
64+
"db_driver": True,
65+
"mysql_client_version": True
66+
}
67+
)
68+
cursor = instrumented_cnx.cursor()
69+
cursor.execute("INSERT INTO test (testField) VALUES (123)"
70+
instrumented_cnx.commit()
71+
cursor.close()
72+
instrumented_cnx.close()
5973
6074
6175
For example,
@@ -165,10 +179,20 @@ def instrument_connection(
165179
"""Enable instrumentation in a PyMySQL connection.
166180
167181
Args:
168-
connection: The connection to instrument.
169-
tracer_provider: The optional tracer provider to use. If omitted
170-
the current globally configured one is used.
171-
182+
connection (pymysql.Connection):
183+
The existing PyMySQL connection instance that needs to be instrumented.
184+
This connection was typically created using `pymysql.connect()` and is wrapped with OpenTelemetry tracing.
185+
tracer_provider (TracerProvider, optional):
186+
An optional `TracerProvider` instance that specifies which tracer provider should be used.
187+
If not provided, the globally configured OpenTelemetry tracer provider is automatically applied.
188+
enable_commenter (bool, optional):
189+
A flag to enable the SQL Commenter feature. If `True`, query logs will be enriched with additional
190+
contextual metadata (e.g., database version, traceparent IDs, driver information).
191+
commenter_options (dict, optional):
192+
A dictionary containing configuration options for the SQL Commenter feature.
193+
You can specify various options, such as enabling driver information, database version logging,
194+
traceparent propagation, and other customizable metadata enhancements.
195+
See *SQLCommenter Configurations* above for more information.
172196
Returns:
173197
An instrumented connection.
174198
"""

0 commit comments

Comments
 (0)