OpenTelemetry Psycopg Integration

The integration with PostgreSQL supports the Psycopg library, it can be enabled by using Psycopg2Instrumentor.

Usage

import psycopg2
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.ext.psycopg2 import Psycopg2Instrumentor

trace.set_tracer_provider(TracerProvider())

Psycopg2Instrumentor().instrument()

cnx = psycopg2.connect(database='Database')
cursor = cnx.cursor()
cursor.execute("INSERT INTO test (testField) VALUES (123)")
cursor.close()
cnx.close()

API

class opentelemetry.ext.psycopg2.Psycopg2Instrumentor[source]

Bases: opentelemetry.instrumentation.instrumentor.BaseInstrumentor

instrument_connection(connection)[source]

Enable instrumentation in a Psycopg2 connection.

Parameters

connection – The connection to instrument.

Returns

An instrumented connection.

uninstrument_connection(connection)[source]

Disable instrumentation in a Psycopg2 connection.

Parameters

connection – The connection to uninstrument.

Returns

An uninstrumented connection.