Skip to content

Commit 66afad7

Browse files
Add mysqlclient sqlcomment support
1 parent 8755339 commit 66afad7

File tree

1 file changed

+78
-1
lines changed
  • instrumentation/opentelemetry-instrumentation-mysqlclient/src/opentelemetry/instrumentation/mysqlclient

1 file changed

+78
-1
lines changed

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

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,72 @@
3636
cursor.close()
3737
cnx.close()
3838
39+
SQLCOMMENTER
40+
*****************************************
41+
You can optionally configure MySQLClient instrumentation to enable sqlcommenter which enriches
42+
the query with contextual information.
43+
44+
.. code:: python
45+
46+
import MySQLdb
47+
from opentelemetry.instrumentation.mysqlclient import MySQLClientInstrumentor
48+
49+
50+
MySQLClientInstrumentor().instrument(enable_commenter=True, commenter_options={})
51+
52+
cnx = MySQLdb.connect(database="MySQL_Database")
53+
cursor = cnx.cursor()
54+
cursor.execute("INSERT INTO test (testField) VALUES (123)"
55+
cnx.commit()
56+
cursor.close()
57+
cnx.close()
58+
59+
For example,
60+
::
61+
62+
Invoking cursor.execute("INSERT INTO test (testField) VALUES (123)") will lead to sql query "INSERT INTO test (testField) VALUES (123)" but when SQLCommenter is enabled
63+
the query will get appended with some configurable tags like "INSERT INTO test (testField) VALUES (123) /*tag=value*/;"
64+
65+
SQLCommenter Configurations
66+
***************************
67+
We can configure the tags to be appended to the sqlquery log by adding configuration inside commenter_options(default:{}) keyword
68+
69+
db_driver = True(Default) or False
70+
71+
For example,
72+
::
73+
Enabling this flag will add MySQLdb and its version, e.g. /*MySQLdb%%3A1.2.3*/
74+
75+
dbapi_threadsafety = True(Default) or False
76+
77+
For example,
78+
::
79+
Enabling this flag will add threadsafety /*dbapi_threadsafety=2*/
80+
81+
dbapi_level = True(Default) or False
82+
83+
For example,
84+
::
85+
Enabling this flag will add dbapi_level /*dbapi_level='2.0'*/
86+
87+
mysql_client_version = True(Default) or False
88+
89+
For example,
90+
::
91+
Enabling this flag will add mysql_client_version /*mysql_client_version='123'*/
92+
93+
driver_paramstyle = True(Default) or False
94+
95+
For example,
96+
::
97+
Enabling this flag will add driver_paramstyle /*driver_paramstyle='pyformat'*/
98+
99+
opentelemetry_values = True(Default) or False
100+
101+
For example,
102+
::
103+
Enabling this flag will add traceparent values /*traceparent='00-03afa25236b8cd948fa853d67038ac79-405ff022e8247c46-01'*/
104+
39105
API
40106
---
41107
"""
@@ -67,6 +133,8 @@ def _instrument(self, **kwargs):
67133
https://github.com/PyMySQL/mysqlclient/
68134
"""
69135
tracer_provider = kwargs.get("tracer_provider")
136+
enable_sqlcommenter = kwargs.get("enable_commenter", False)
137+
commenter_options = kwargs.get("commenter_options", {})
70138

71139
dbapi.wrap_connect(
72140
__name__,
@@ -76,14 +144,21 @@ def _instrument(self, **kwargs):
76144
_CONNECTION_ATTRIBUTES,
77145
version=__version__,
78146
tracer_provider=tracer_provider,
147+
enable_commenter=enable_sqlcommenter,
148+
commenter_options=commenter_options,
79149
)
80150

81151
def _uninstrument(self, **kwargs):
82152
""" "Disable mysqlclient instrumentation"""
83153
dbapi.unwrap_connect(MySQLdb, "connect")
84154

85155
@staticmethod
86-
def instrument_connection(connection, tracer_provider=None):
156+
def instrument_connection(
157+
connection,
158+
tracer_provider=None,
159+
enable_commenter=None,
160+
commenter_options=None,
161+
):
87162
"""Enable instrumentation in a mysqlclient connection.
88163
89164
Args:
@@ -102,6 +177,8 @@ def instrument_connection(connection, tracer_provider=None):
102177
_CONNECTION_ATTRIBUTES,
103178
version=__version__,
104179
tracer_provider=tracer_provider,
180+
enable_commenter=enable_sqlcommenter,
181+
commenter_options=commenter_options,
105182
)
106183

107184
@staticmethod

0 commit comments

Comments
 (0)