Skip to content

Commit 4283005

Browse files
Add mysql-connector sqlcomment support
1 parent 4e6ab3f commit 4283005

File tree

1 file changed

+76
-1
lines changed
  • instrumentation/opentelemetry-instrumentation-mysql/src/opentelemetry/instrumentation/mysql

1 file changed

+76
-1
lines changed

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

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,69 @@
3434
cursor.close()
3535
cnx.close()
3636
37+
SQLCOMMENTER
38+
*****************************************
39+
You can optionally configure mysql-connector instrumentation to enable sqlcommenter which enriches
40+
the query with contextual information.
41+
42+
.. code:: python
43+
44+
import mysql.connector
45+
from opentelemetry.instrumentation.mysql import MySQLInstrumentor
46+
47+
MySQLInstrumentor().instrument(enable_commenter=True, commenter_options={})
48+
49+
cnx = mysql.connector.connect(database="MySQL_Database")
50+
cursor = cnx.cursor()
51+
cursor.execute("INSERT INTO test (testField) VALUES (123)")
52+
cursor.close()
53+
cnx.close()
54+
55+
For example,
56+
::
57+
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
58+
the query will get appended with some configurable tags like "INSERT INTO test (testField) VALUES (123) /*tag=value*/;"
59+
60+
SQLCommenter Configurations
61+
***************************
62+
We can configure the tags to be appended to the sqlquery log by adding configuration inside commenter_options(default:{}) keyword
63+
64+
db_driver = True(Default) or False
65+
66+
For example,
67+
::
68+
Enabling this flag will add mysql.connector and its version, e.g. /*mysql.connector%%3A1.2.3*/
69+
70+
dbapi_threadsafety = True(Default) or False
71+
72+
For example,
73+
::
74+
Enabling this flag will add threadsafety /*dbapi_threadsafety=2*/
75+
76+
dbapi_level = True(Default) or False
77+
78+
For example,
79+
::
80+
Enabling this flag will add dbapi_level /*dbapi_level='2.0'*/
81+
82+
mysql_client_version = True(Default) or False
83+
84+
For example,
85+
::
86+
Enabling this flag will add mysql_client_version /*mysql_client_version='123'*/
87+
88+
driver_paramstyle = True(Default) or False
89+
90+
For example,
91+
::
92+
Enabling this flag will add driver_paramstyle /*driver_paramstyle='pyformat'*/
93+
94+
opentelemetry_values = True(Default) or False
95+
96+
For example,
97+
::
98+
Enabling this flag will add traceparent values /*traceparent='00-03afa25236b8cd948fa853d67038ac79-405ff022e8247c46-01'*/
99+
37100
API
38101
---
39102
"""
@@ -66,6 +129,8 @@ def _instrument(self, **kwargs):
66129
https://dev.mysql.com/doc/connector-python/en/
67130
"""
68131
tracer_provider = kwargs.get("tracer_provider")
132+
enable_sqlcommenter = kwargs.get("enable_commenter", False)
133+
commenter_options = kwargs.get("commenter_options", {})
69134

70135
dbapi.wrap_connect(
71136
__name__,
@@ -75,14 +140,22 @@ def _instrument(self, **kwargs):
75140
self._CONNECTION_ATTRIBUTES,
76141
version=__version__,
77142
tracer_provider=tracer_provider,
143+
enable_commenter=enable_sqlcommenter,
144+
commenter_options=commenter_options,
78145
)
79146

80147
def _uninstrument(self, **kwargs):
81148
""" "Disable MySQL instrumentation"""
82149
dbapi.unwrap_connect(mysql.connector, "connect")
83150

84151
# pylint:disable=no-self-use
85-
def instrument_connection(self, connection, tracer_provider=None):
152+
def instrument_connection(
153+
self,
154+
connection,
155+
tracer_provider=None,
156+
enable_commenter=None,
157+
commenter_options=None,
158+
):
86159
"""Enable instrumentation in a MySQL connection.
87160
88161
Args:
@@ -100,6 +173,8 @@ def instrument_connection(self, connection, tracer_provider=None):
100173
self._CONNECTION_ATTRIBUTES,
101174
version=__version__,
102175
tracer_provider=tracer_provider,
176+
enable_commenter=enable_commenter,
177+
commenter_options=commenter_options,
103178
)
104179

105180
def uninstrument_connection(self, connection):

0 commit comments

Comments
 (0)