The integration with PyMySQL supports the PyMySQL library and can be enabled by using PyMySQLInstrumentor
.
import pymysql from opentelemetry.instrumentation.pymysql import PyMySQLInstrumentor # Call instrument() to wrap all database connections PyMySQLInstrumentor().instrument() cnx = pymysql.connect(database="MySQL_Database") cursor = cnx.cursor() cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)") cursor.execute("INSERT INTO test (testField) VALUES (123)") cnx.commit() cursor.close() cnx.close()
import pymysql from opentelemetry.instrumentation.pymysql import PyMySQLInstrumentor # Alternatively, use instrument_connection for an individual connection cnx = pymysql.connect(database="MySQL_Database") instrumented_cnx = PyMySQLInstrumentor().instrument_connection( cnx, enable_commenter=True, commenter_options={ "db_driver": True, "mysql_client_version": True } ) cursor = instrumented_cnx.cursor() cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)") cursor.execute("INSERT INTO test (testField) VALUES (123)") instrumented_cnx.commit() cursor.close() instrumented_cnx.close()Usage
import pymysql from opentelemetry.instrumentation.pymysql import PyMySQLInstrumentor PyMySQLInstrumentor().instrument(enable_commenter=True, commenter_options={}) cnx = pymysql.connect(database="MySQL_Database") cursor = cnx.cursor() cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)") cursor.execute("INSERT INTO test (testField) VALUES (123)") cnx.commit() cursor.close() cnx.close()
For example,
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 the query will get appended with some configurable tags like "INSERT INTO test (testField) VALUES (123) /*tag=value*/;"API
Bases: BaseInstrumentor
Return a list of python packages with versions that the will be instrumented.
The format should be the same as used in requirements.txt or pyproject.toml.
For example, if an instrumentation instruments requests 1.x, this method should look like: :rtype: Collection
[str
]
- def instrumentation_dependencies(self) -> Collection[str]:
return [‘requests ~= 1.0’]
This will ensure that the instrumentation will only be used when the specified library is present in the environment.
Enable instrumentation in a PyMySQL connection.
connection – The existing PyMySQL connection instance that needs to be instrumented. This connection was typically created using pymysql.connect() and is wrapped with OpenTelemetry tracing.
tracer_provider – An optional TracerProvider instance that specifies which tracer provider should be used. If not provided, the globally configured OpenTelemetry tracer provider is automatically applied.
enable_commenter – A flag to enable the SQL Commenter feature. If True, query logs will be enriched with additional contextual metadata (e.g., database version, traceparent IDs, driver information).
commenter_options – A dictionary containing configuration options for the SQL Commenter feature. You can specify various options, such as enabling driver information, database version logging, traceparent propagation, and other customizable metadata enhancements. See SQLCommenter Configurations above for more information.
An instrumented connection.
Disable instrumentation in a PyMySQL connection.
connection – The connection to uninstrument.
An uninstrumented connection.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4