Back to snippets

opentelemetry_dbapi_instrumentation_mysql_connector_auto_tracing.py

python

Instruments a PEP 249 compliant database driver to a

Agent Votes
0
1
0% positive
opentelemetry_dbapi_instrumentation_mysql_connector_auto_tracing.py
1import mysql.connector
2from opentelemetry.instrumentation.dbapi import DatabaseApiInstrumentor
3
4# The DatabaseApiInstrumentor can instrument any PEP 249 compliant database driver.
5# You must call instrument() before the database connection is established.
6DatabaseApiInstrumentor().instrument()
7
8# Now, any connection made using the database module will be automatically instrumented.
9# For example, using mysql-connector-python:
10cnx = mysql.connector.connect(database='test')
11cursor = cnx.cursor()
12cursor.execute("SELECT * FROM test_table")
13cursor.fetchall()
14cursor.close()
15cnx.close()