Skip to content

Commit 89182ad

Browse files
Update PyMySQL docs
1 parent 34db73e commit 89182ad

File tree

1 file changed

+56
-54
lines changed
  • instrumentation/opentelemetry-instrumentation-pymysql/src/opentelemetry/instrumentation/pymysql

1 file changed

+56
-54
lines changed

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

Lines changed: 56 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,24 @@
6262
Configuration
6363
-------------
6464
65-
SQLCOMMENTER
66-
*****************************************
67-
You can optionally configure PyMySQL instrumentation to enable sqlcommenter which enriches
68-
the query with contextual information.
65+
SQLCommenter
66+
************
67+
You can optionally enable sqlcommenter which enriches the query with contextual
68+
information. Queries made after setting up trace integration with sqlcommenter
69+
enabled will have configurable key-value pairs appended to them, e.g.
70+
``"select * from auth_users; /*traceparent=00-01234567-abcd-01*/"``. This
71+
supports context propagation between database client and server when database log
72+
records are enabled. For more information, see:
73+
74+
* `Semantic Conventions - Database Spans <https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/database-spans.md#sql-commenter>`_
75+
* `sqlcommenter <https://google.github.io/sqlcommenter/>`_
6976
7077
.. code:: python
7178
7279
import pymysql
7380
from opentelemetry.instrumentation.pymysql import PyMySQLInstrumentor
7481
75-
PyMySQLInstrumentor().instrument(enable_commenter=True, commenter_options={})
82+
PyMySQLInstrumentor().instrument(enable_commenter=True)
7683
7784
cnx = pymysql.connect(database="MySQL_Database")
7885
cursor = cnx.cursor()
@@ -82,72 +89,67 @@
8289
cursor.close()
8390
cnx.close()
8491
85-
For example,
86-
::
92+
SQLCommenter with commenter_options
93+
***********************************
94+
The key-value pairs appended to the query can be configured using
95+
``commenter_options``. When sqlcommenter is enabled, all available KVs/tags
96+
are calculated by default. ``commenter_options`` supports *opting out*
97+
of specific KVs.
8798
88-
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
89-
the query will get appended with some configurable tags like "INSERT INTO test (testField) VALUES (123) /*tag=value*/;"
90-
91-
92-
SQLCommenter Configurations
93-
***************************
94-
We can configure the tags to be appended to the sqlquery log by adding configuration inside commenter_options(default:{}) keyword
95-
96-
db_driver = True(Default) or False
97-
98-
For example,
99-
::
100-
Enabling this flag will add pymysql and its version, e.g. /*pymysql%%3A1.2.3*/
101-
102-
dbapi_threadsafety = True(Default) or False
103-
104-
For example,
105-
::
106-
Enabling this flag will add threadsafety /*dbapi_threadsafety=2*/
107-
108-
dbapi_level = True(Default) or False
109-
110-
For example,
111-
::
112-
Enabling this flag will add dbapi_level /*dbapi_level='2.0'*/
113-
114-
mysql_client_version = True(Default) or False
115-
116-
For example,
117-
::
118-
Enabling this flag will add mysql_client_version /*mysql_client_version='123'*/
119-
120-
driver_paramstyle = True(Default) or False
99+
.. code:: python
121100
122-
For example,
123-
::
124-
Enabling this flag will add driver_paramstyle /*driver_paramstyle='pyformat'*/
101+
import pymysql
102+
from opentelemetry.instrumentation.pymysql import PyMySQLInstrumentor
125103
126-
opentelemetry_values = True(Default) or False
104+
# Opts into sqlcomment for PyMySQL trace integration.
105+
# Opts out of tags for mysql_client_version, db_driver.
106+
PyMySQLInstrumentor().instrument(
107+
enable_commenter=True,
108+
commenter_options={
109+
"mysql_client_version": False,
110+
"db_driver": False,
111+
}
112+
)
127113
128-
For example,
129-
::
130-
Enabling this flag will add traceparent values /*traceparent='00-03afa25236b8cd948fa853d67038ac79-405ff022e8247c46-01'*/
114+
Available commenter_options
115+
###########################
116+
117+
The following sqlcomment key-values can be opted out of through ``commenter_options``:
118+
119+
+---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+
120+
| Commenter Option | Description | Example |
121+
+===========================+===========================================================+===========================================================================+
122+
| ``db_driver`` | Database driver name with version. | ``pymysql='1.2.3'`` |
123+
+---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+
124+
| ``dbapi_threadsafety`` | DB-API threadsafety value: 0-3 or unknown. | ``dbapi_threadsafety=2`` |
125+
+---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+
126+
| ``dbapi_level`` | DB-API API level: 1.0, 2.0, or unknown. | ``dbapi_level='2.0'`` |
127+
+---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+
128+
| ``driver_paramstyle`` | DB-API paramstyle for SQL statement parameter. | ``driver_paramstyle='pyformat'`` |
129+
+---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+
130+
| ``mysql_client_version`` | MySQL client version. | ``mysql_client_version='123'`` |
131+
+---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+
132+
| ``opentelemetry_values`` | OpenTelemetry context as traceparent at time of query. | ``traceparent='00-03afa25236b8cd948fa853d67038ac79-405ff022e8247c46-01'`` |
133+
+---------------------------+-----------------------------------------------------------+---------------------------------------------------------------------------+
131134
132135
SQLComment in span attribute
133136
****************************
134-
If sqlcommenter is enabled, you can optionally configure PyMySQL instrumentation to append sqlcomment to query span attribute for convenience of your platform.
137+
If sqlcommenter is enabled, you can opt into the inclusion of sqlcomment in
138+
the query span ``db.statement`` attribute for your needs. If ``commenter_options``
139+
have been set, the span attribute comment will also be configured by this
140+
setting.
135141
136142
.. code:: python
137143
138144
from opentelemetry.instrumentation.pymysql import PyMySQLInstrumentor
139145
146+
# Opts into sqlcomment for PyMySQL trace integration.
147+
# Opts into sqlcomment for `db.statement` span attribute.
140148
PyMySQLInstrumentor().instrument(
141149
enable_commenter=True,
142150
enable_attribute_commenter=True,
143151
)
144152
145-
For example,
146-
::
147-
148-
Invoking cursor.execute("select * from auth_users") will lead to sql query "select * from auth_users" but when SQLCommenter and attribute_commenter are enabled
149-
the query will get appended with some configurable tags like "select * from auth_users /*tag=value*/;" for both server query and `db.statement` span attribute.
150-
151153
API
152154
---
153155
"""

0 commit comments

Comments
 (0)