OpenTelemetry Database API Integration¶
The trace integration with Database API supports libraries that follow the Python Database API Specification v2.0. https://www.python.org/dev/peps/pep-0249/
Usage¶
import mysql.connector
import pyodbc
from opentelemetry import trace
from opentelemetry.ext.dbapi import trace_integration
from opentelemetry.trace import TracerProvider
trace.set_tracer_provider(TracerProvider())
# Ex: mysql.connector
trace_integration(mysql.connector, "connect", "mysql", "sql")
# Ex: pyodbc
trace_integration(pyodbc, "Connection", "odbc", "sql")
API¶
-
opentelemetry.ext.dbapi.trace_integration(connect_module, connect_method_name, database_component, database_type='', connection_attributes=None, tracer_provider=None)[source]¶ Integrate with DB API library. https://www.python.org/dev/peps/pep-0249/
- Parameters
connect_module (
Callable[…,Any]) – Module name where connect method is available.connect_method_name (
str) – The connect method name.database_component (
str) – Database driver name or database name “JDBI”, “jdbc”, “odbc”, “postgreSQL”.database_type (
str) – The Database type. For any SQL database, “sql”.connection_attributes (
Optional[Dict]) – Attribute names for database, port, host and user in Connection object.tracer_provider (
Optional[TracerProvider]) – Theopentelemetry.trace.TracerProviderto use. If ommited the current configured one is used.
-
opentelemetry.ext.dbapi.wrap_connect(tracer, connect_module, connect_method_name, database_component, database_type='', connection_attributes=None)[source]¶ Integrate with DB API library. https://www.python.org/dev/peps/pep-0249/
- Parameters
tracer (
Tracer) – Theopentelemetry.trace.Tracerto use.connect_module (
Callable[…,Any]) – Module name where connect method is available.connect_method_name (
str) – The connect method name.database_component (
str) – Database driver name or database name “JDBI”, “jdbc”, “odbc”, “postgreSQL”.database_type (
str) – The Database type. For any SQL database, “sql”.connection_attributes (
Optional[Dict]) – Attribute names for database, port, host and user in Connection object.
-
opentelemetry.ext.dbapi.unwrap_connect(connect_module, connect_method_name)[source]¶ Disable integration with DB API library. https://www.python.org/dev/peps/pep-0249/
- Parameters
connect_module (
Callable[…,Any]) – Module name where the connect method is available.connect_method_name (
str) – The connect method name.
-
opentelemetry.ext.dbapi.instrument_connection(tracer, connection, database_component, database_type='', connection_attributes=None)[source]¶ Enable instrumentation in a database connection.
- Parameters
tracer – The
opentelemetry.trace.Tracerto use.connection – The connection to instrument.
database_component (
str) – Database driver name or database name “JDBI”, “jdbc”, “odbc”, “postgreSQL”.database_type (
str) – The Database type. For any SQL database, “sql”.connection_attributes (
Optional[Dict]) – Attribute names for database, port, host and user in a connection object.
- Returns
An instrumented connection.
-
opentelemetry.ext.dbapi.uninstrument_connection(connection)[source]¶ Disable instrumentation in a database connection.
- Parameters
connection – The connection to uninstrument.
- Returns
An uninstrumented connection.
-
class
opentelemetry.ext.dbapi.DatabaseApiIntegration(tracer, database_component, database_type='sql', connection_attributes=None)[source]¶ Bases:
object
-
opentelemetry.ext.dbapi.get_traced_connection_proxy(connection, db_api_integration, *args, **kwargs)[source]¶