Skip to content

Commit 1092344

Browse files
authored
Add docstring for instrument_connection() and tests for database drivers (#3108)
1 parent 54cbf59 commit 1092344

File tree

8 files changed

+221
-21
lines changed
  • instrumentation
    • opentelemetry-instrumentation-aiopg/src/opentelemetry/instrumentation/aiopg
    • opentelemetry-instrumentation-mysqlclient/src/opentelemetry/instrumentation/mysqlclient
    • opentelemetry-instrumentation-mysql/src/opentelemetry/instrumentation/mysql
    • opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2
    • opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg
    • opentelemetry-instrumentation-pymysql/src/opentelemetry/instrumentation/pymysql
    • opentelemetry-instrumentation-sqlite3

8 files changed

+221
-21
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,39 @@
2525
2626
import aiopg
2727
from opentelemetry.instrumentation.aiopg import AiopgInstrumentor
28-
28+
# Call instrument() to wrap all database connections
2929
AiopgInstrumentor().instrument()
3030
3131
cnx = await aiopg.connect(database='Database')
3232
cursor = await cnx.cursor()
33+
await cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)")
3334
await cursor.execute("INSERT INTO test (testField) VALUES (123)")
3435
cursor.close()
3536
cnx.close()
3637
3738
pool = await aiopg.create_pool(database='Database')
39+
3840
cnx = await pool.acquire()
3941
cursor = await cnx.cursor()
42+
await cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)")
4043
await cursor.execute("INSERT INTO test (testField) VALUES (123)")
4144
cursor.close()
4245
cnx.close()
4346
47+
.. code-block:: python
48+
49+
import aiopg
50+
from opentelemetry.instrumentation.aiopg import AiopgInstrumentor
51+
52+
# Alternatively, use instrument_connection for an individual connection
53+
cnx = await aiopg.connect(database='Database')
54+
instrumented_cnx = AiopgInstrumentor().instrument_connection(cnx)
55+
cursor = await instrumented_cnx.cursor()
56+
await cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)")
57+
await cursor.execute("INSERT INTO test (testField) VALUES (123)")
58+
cursor.close()
59+
instrumented_cnx.close()
60+
4461
API
4562
---
4663
"""

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,30 @@
2626
import mysql.connector
2727
from opentelemetry.instrumentation.mysql import MySQLInstrumentor
2828
29+
# Call instrument() to wrap all database connections
2930
MySQLInstrumentor().instrument()
3031
3132
cnx = mysql.connector.connect(database="MySQL_Database")
3233
cursor = cnx.cursor()
34+
cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)")
3335
cursor.execute("INSERT INTO test (testField) VALUES (123)")
3436
cursor.close()
3537
cnx.close()
3638
39+
.. code:: python
40+
41+
import mysql.connector
42+
from opentelemetry.instrumentation.mysql import MySQLInstrumentor
43+
44+
# Alternatively, use instrument_connection for an individual connection
45+
cnx = mysql.connector.connect(database="MySQL_Database")
46+
instrumented_cnx = MySQLInstrumentor().instrument_connection(cnx)
47+
cursor = instrumented_cnx.cursor()
48+
cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)")
49+
cursor.execute("INSERT INTO test (testField) VALUES (123)")
50+
cursor.close()
51+
instrumented_cnx.close()
52+
3753
API
3854
---
3955
"""
@@ -86,12 +102,16 @@ def instrument_connection(self, connection, tracer_provider=None):
86102
"""Enable instrumentation in a MySQL connection.
87103
88104
Args:
89-
connection: The connection to instrument.
90-
tracer_provider: The optional tracer provider to use. If omitted
91-
the current globally configured one is used.
105+
connection:
106+
The existing MySQL connection instance to instrument. This connection is typically
107+
obtained through `mysql.connector.connect()` and is instrumented to collect telemetry
108+
data about database interactions.
109+
tracer_provider:
110+
An optional `TracerProvider` instance to use for tracing. If not provided, the globally
111+
configured tracer provider will be automatically used.
92112
93113
Returns:
94-
An instrumented connection.
114+
An instrumented MySQL connection with OpenTelemetry tracing enabled.
95115
"""
96116
return dbapi.instrument_connection(
97117
__name__,

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

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,40 @@
4646
import MySQLdb
4747
from opentelemetry.instrumentation.mysqlclient import MySQLClientInstrumentor
4848
49-
49+
# Call instrument() to wrap all database connections
5050
MySQLClientInstrumentor().instrument(enable_commenter=True, commenter_options={})
5151
5252
cnx = MySQLdb.connect(database="MySQL_Database")
5353
cursor = cnx.cursor()
54+
cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)")
5455
cursor.execute("INSERT INTO test (testField) VALUES (123)"
5556
cnx.commit()
5657
cursor.close()
5758
cnx.close()
5859
60+
.. code:: python
61+
62+
import MySQLdb
63+
from opentelemetry.instrumentation.mysqlclient import MySQLClientInstrumentor
64+
65+
# Alternatively, use instrument_connection for an individual connection
66+
cnx = MySQLdb.connect(database="MySQL_Database")
67+
instrumented_cnx = MySQLClientInstrumentor().instrument_connection(
68+
cnx,
69+
enable_commenter=True,
70+
commenter_options={
71+
"db_driver": True,
72+
"mysql_client_version": True,
73+
"driver_paramstyle": False
74+
}
75+
)
76+
cursor = instrumented_cnx.cursor()
77+
cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)")
78+
cursor.execute("INSERT INTO test (testField) VALUES (123)"
79+
instrumented_cnx.commit()
80+
cursor.close()
81+
instrumented_cnx.close()
82+
5983
For example,
6084
::
6185
@@ -162,12 +186,28 @@ def instrument_connection(
162186
"""Enable instrumentation in a mysqlclient connection.
163187
164188
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-
189+
connection:
190+
The MySQL connection instance to instrument. This connection is typically
191+
created using `MySQLdb.connect()` and needs to be wrapped to collect telemetry.
192+
tracer_provider:
193+
A custom `TracerProvider` instance to be used for tracing. If not specified,
194+
the globally configured tracer provider will be used.
195+
enable_commenter:
196+
A flag to enable the OpenTelemetry SQLCommenter feature. If set to `True`,
197+
SQL queries will be enriched with contextual information (e.g., database client details).
198+
Default is `None`.
199+
commenter_options:
200+
A dictionary of configuration options for SQLCommenter. All options are enabled (True) by default.
201+
This allows you to customize metadata appended to queries. Possible options include:
202+
203+
- `db_driver`: Adds the database driver name and version.
204+
- `dbapi_threadsafety`: Adds threadsafety information.
205+
- `dbapi_level`: Adds the DB-API version.
206+
- `mysql_client_version`: Adds the MySQL client version.
207+
- `driver_paramstyle`: Adds the parameter style.
208+
- `opentelemetry_values`: Includes traceparent values.
169209
Returns:
170-
An instrumented connection.
210+
An instrumented MySQL connection with OpenTelemetry support enabled.
171211
"""
172212

173213
return dbapi.instrument_connection(

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,31 @@
8888
import psycopg
8989
from opentelemetry.instrumentation.psycopg import PsycopgInstrumentor
9090
91-
91+
# Call instrument() to wrap all database connections
9292
PsycopgInstrumentor().instrument()
9393
9494
cnx = psycopg.connect(database='Database')
95+
9596
cursor = cnx.cursor()
97+
cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)")
9698
cursor.execute("INSERT INTO test (testField) VALUES (123)")
9799
cursor.close()
98100
cnx.close()
99101
102+
.. code-block:: python
103+
104+
import psycopg
105+
from opentelemetry.instrumentation.psycopg import PsycopgInstrumentor
106+
107+
# Alternatively, use instrument_connection for an individual connection
108+
cnx = psycopg.connect(database='Database')
109+
instrumented_cnx = PsycopgInstrumentor().instrument_connection(cnx)
110+
cursor = instrumented_cnx.cursor()
111+
cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)")
112+
cursor.execute("INSERT INTO test (testField) VALUES (123)")
113+
cursor.close()
114+
instrumented_cnx.close()
115+
100116
API
101117
---
102118
"""
@@ -196,6 +212,18 @@ def _uninstrument(self, **kwargs):
196212
# TODO(owais): check if core dbapi can do this for all dbapi implementations e.g, pymysql and mysql
197213
@staticmethod
198214
def instrument_connection(connection, tracer_provider=None):
215+
"""Enable instrumentation in a psycopg connection.
216+
217+
Args:
218+
connection: psycopg.Connection
219+
The psycopg connection object to be instrumented.
220+
tracer_provider: opentelemetry.trace.TracerProvider, optional
221+
The TracerProvider to use for instrumentation. If not provided,
222+
the global TracerProvider will be used.
223+
224+
Returns:
225+
An instrumented psycopg connection object.
226+
"""
199227
if not hasattr(connection, "_is_instrumented_by_opentelemetry"):
200228
connection._is_instrumented_by_opentelemetry = False
201229

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,31 @@
8888
import psycopg2
8989
from opentelemetry.instrumentation.psycopg2 import Psycopg2Instrumentor
9090
91-
91+
# Call instrument() to wrap all database connections
9292
Psycopg2Instrumentor().instrument()
9393
9494
cnx = psycopg2.connect(database='Database')
95+
9596
cursor = cnx.cursor()
97+
cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)")
9698
cursor.execute("INSERT INTO test (testField) VALUES (123)")
9799
cursor.close()
98100
cnx.close()
99101
102+
.. code-block:: python
103+
104+
import psycopg2
105+
from opentelemetry.instrumentation.psycopg2 import Psycopg2Instrumentor
106+
107+
# Alternatively, use instrument_connection for an individual connection
108+
cnx = psycopg2.connect(database='Database')
109+
instrumented_cnx = Psycopg2Instrumentor().instrument_connection(cnx)
110+
cursor = instrumented_cnx.cursor()
111+
cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)")
112+
cursor.execute("INSERT INTO test (testField) VALUES (123)")
113+
cursor.close()
114+
instrumented_cnx.close()
115+
100116
API
101117
---
102118
"""
@@ -160,6 +176,19 @@ def _uninstrument(self, **kwargs):
160176
# TODO(owais): check if core dbapi can do this for all dbapi implementations e.g, pymysql and mysql
161177
@staticmethod
162178
def instrument_connection(connection, tracer_provider=None):
179+
"""Enable instrumentation in a psycopg2 connection.
180+
181+
Args:
182+
connection: psycopg2.extensions.connection
183+
The psycopg2 connection object to be instrumented.
184+
tracer_provider: opentelemetry.trace.TracerProvider, optional
185+
The TracerProvider to use for instrumentation. If not specified,
186+
the global TracerProvider will be used.
187+
188+
Returns:
189+
An instrumented psycopg2 connection object.
190+
"""
191+
163192
if not hasattr(connection, "_is_instrumented_by_opentelemetry"):
164193
connection._is_instrumented_by_opentelemetry = False
165194

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

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import pymysql
2727
from opentelemetry.instrumentation.pymysql import PyMySQLInstrumentor
2828
29+
# Call instrument() to wrap all database connections
2930
PyMySQLInstrumentor().instrument()
3031
3132
cnx = pymysql.connect(database="MySQL_Database")
@@ -35,6 +36,28 @@
3536
cursor.close()
3637
cnx.close()
3738
39+
40+
.. code:: python
41+
42+
import pymysql
43+
from opentelemetry.instrumentation.pymysql import PyMySQLInstrumentor
44+
45+
# Alternatively, use instrument_connection for an individual connection
46+
cnx = pymysql.connect(database="MySQL_Database")
47+
instrumented_cnx = PyMySQLInstrumentor().instrument_connection(
48+
cnx,
49+
enable_commenter=True,
50+
commenter_options={
51+
"db_driver": True,
52+
"mysql_client_version": True
53+
}
54+
)
55+
cursor = instrumented_cnx.cursor()
56+
cursor.execute("INSERT INTO test (testField) VALUES (123)"
57+
instrumented_cnx.commit()
58+
cursor.close()
59+
instrumented_cnx.close()
60+
3861
SQLCOMMENTER
3962
*****************************************
4063
You can optionally configure PyMySQL instrumentation to enable sqlcommenter which enriches
@@ -165,10 +188,20 @@ def instrument_connection(
165188
"""Enable instrumentation in a PyMySQL connection.
166189
167190
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-
191+
connection:
192+
The existing PyMySQL connection instance that needs to be instrumented.
193+
This connection was typically created using `pymysql.connect()` and is wrapped with OpenTelemetry tracing.
194+
tracer_provider:
195+
An optional `TracerProvider` instance that specifies which tracer provider should be used.
196+
If not provided, the globally configured OpenTelemetry tracer provider is automatically applied.
197+
enable_commenter:
198+
A flag to enable the SQL Commenter feature. If `True`, query logs will be enriched with additional
199+
contextual metadata (e.g., database version, traceparent IDs, driver information).
200+
commenter_options:
201+
A dictionary containing configuration options for the SQL Commenter feature.
202+
You can specify various options, such as enabling driver information, database version logging,
203+
traceparent propagation, and other customizable metadata enhancements.
204+
See *SQLCommenter Configurations* above for more information.
172205
Returns:
173206
An instrumented connection.
174207
"""

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,31 @@
2727
import sqlite3
2828
from opentelemetry.instrumentation.sqlite3 import SQLite3Instrumentor
2929
30-
30+
# Call instrument() to wrap all database connections
3131
SQLite3Instrumentor().instrument()
3232
3333
cnx = sqlite3.connect(':memory:')
3434
cursor = cnx.cursor()
35-
cursor.execute("CREATE TABLE test (testField INTEGER)")
35+
cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)")
3636
cursor.execute("INSERT INTO test (testField) VALUES (123)")
3737
cursor.close()
3838
cnx.close()
3939
40+
.. code:: python
41+
42+
import sqlite3
43+
from opentelemetry.instrumentation.sqlite3 import SQLite3Instrumentor
44+
45+
# Alternatively, use instrument_connection for an individual connection
46+
conn = sqlite3.connect(":memory:")
47+
instrumented_connection = SQLite3Instrumentor().instrument_connection(conn)
48+
cursor = instrumented_connection.cursor()
49+
cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)")
50+
cursor.execute("INSERT INTO test (testField) VALUES (123)")
51+
cursor.execute("SELECT * FROM test")
52+
cursor.close()
53+
instrumented_connection.close()
54+
4055
API
4156
---
4257
"""
@@ -104,9 +119,10 @@ def instrument_connection(
104119
the current globally configured one is used.
105120
106121
Returns:
107-
An instrumented connection.
108-
"""
122+
An instrumented SQLite connection that supports
123+
telemetry for tracing database operations.
109124
125+
"""
110126
return dbapi.instrument_connection(
111127
__name__,
112128
connection,

0 commit comments

Comments
 (0)