Skip to content

Commit ea17a2a

Browse files
Add pymysql sqlcomment support
1 parent 4e6ab3f commit ea17a2a

File tree

1 file changed

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

1 file changed

+76
-1
lines changed

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

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

71137
dbapi.wrap_connect(
72138
__name__,
@@ -76,14 +142,21 @@ def _instrument(self, **kwargs):
76142
_CONNECTION_ATTRIBUTES,
77143
version=__version__,
78144
tracer_provider=tracer_provider,
145+
enable_commenter=enable_sqlcommenter,
146+
commenter_options=commenter_options,
79147
)
80148

81149
def _uninstrument(self, **kwargs):
82150
""" "Disable PyMySQL instrumentation"""
83151
dbapi.unwrap_connect(pymysql, "connect")
84152

85153
@staticmethod
86-
def instrument_connection(connection, tracer_provider=None):
154+
def instrument_connection(
155+
connection,
156+
tracer_provider=None,
157+
enable_commenter=None,
158+
commenter_options=None,
159+
):
87160
"""Enable instrumentation in a PyMySQL connection.
88161
89162
Args:
@@ -102,6 +175,8 @@ def instrument_connection(connection, tracer_provider=None):
102175
_CONNECTION_ATTRIBUTES,
103176
version=__version__,
104177
tracer_provider=tracer_provider,
178+
enable_commenter=enable_commenter,
179+
commenter_options=commenter_options,
105180
)
106181

107182
@staticmethod

0 commit comments

Comments
 (0)