You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The source files of this example are available `here <https://github.com/open-telemetry/opentelemetry-python/tree/main/docs/examples/sqlcommenter/>`_.
12
+
This example uses Docker to manage a database server and OpenTelemetry collector.
13
+
14
+
Run MySQL server
15
+
----------------
16
+
17
+
A running MySQL server with general logs enabled will store query statements with context resulting from the sqlcommenter feature enabled in this example.
18
+
19
+
.. code-block:: sh
20
+
21
+
cd books_database
22
+
docker build -t books-db .
23
+
docker run -d --name books-db -p 3366:3306 books-db
24
+
cd ..
25
+
26
+
Check that the run is working and the general log is available:
For each instrumented ``SELECT`` call, a query was made and logged with
81
+
a sqlcomment appended. For example:
82
+
83
+
.. code::
84
+
85
+
2025-09-02T18:49:06.981980Z 186 Query SELECT * FROM authors WHERE id = 1 /*db_driver='mysql.connector%%3A9.4.0',dbapi_level='2.0',dbapi_threadsafety=1,driver_paramstyle='pyformat',mysql_client_version='9.4.0',traceparent='00-2c45248f2beefdd9688b0a94eb4ac9ee-4f3af9a825aae9b1-01'*/
86
+
87
+
In the running OpenTelemetry collector, you'll also see one span per
88
+
``SELECT`` call. Each of those span's trace ID and span ID will
89
+
correspond to a query log sqlcomment. With the comment-in-attribute
90
+
feature enabled, the span's ``db.statement`` attribute will also contain
-> db.statement: Str(SELECT * FROM authors WHERE id = %s /*db_driver='mysql.connector%%3A9.4.0',dbapi_level='2.0',dbapi_threadsafety=1,driver_paramstyle='pyformat',mysql_client_version='9.4.0',traceparent='00-2c45248f2beefdd9688b0a94eb4ac9ee-4f3af9a825aae9b1-01'*/)
* `OpenTelemetry MySQL instrumentation <https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-mysql>`_
RUN echo "CREATE USER IF NOT EXISTS 'books'@'%' IDENTIFIED WITH mysql_native_password BY 'books123';" > /docker-entrypoint-initdb.d/01-create-user.sql && \
9
+
echo "GRANT ALL PRIVILEGES ON books.* TO 'books'@'%';" >> /docker-entrypoint-initdb.d/01-create-user.sql && \
0 commit comments