Skip to content

Psycopg2Instrumentor().instrument_connection raises AttributeError: 'psycopg2.extensions.connection' object has no attribute '_is_instrumented_by_opentelemetry' #2522

@alexmojaki

Description

@alexmojaki

Describe your environment

opentelemetry-instrumentation-psycopg2==0.45b0
psycopg2-binary==2.9.9

Steps to reproduce

Use Psycopg2Instrumentor().instrument_connection. For example:

import psycopg2
from opentelemetry.instrumentation.psycopg2 import Psycopg2Instrumentor

connection = psycopg2.connect(database='database', user='user', password='secret', host='0.0.0.0', port=5432)

Psycopg2Instrumentor().instrument_connection(connection)

and run a database with:

docker run --name postgres \
    -e POSTGRES_USER=user \
    -e POSTGRES_PASSWORD=secret \
    -e POSTGRES_DB=database \
    -p 5432:5432 -d postgres

What is the actual behavior?

Traceback (most recent call last):
  File "test.py", line 6, in <module>
    Psycopg2Instrumentor().instrument_connection(connection)
  File "/home/alex/work/logfire/.venv/lib/python3.12/site-packages/opentelemetry/instrumentation/psycopg2/__init__.py", line 164, in instrument_connection
    connection._is_instrumented_by_opentelemetry = False
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'psycopg2.extensions.connection' object has no attribute '_is_instrumented_by_opentelemetry'

Additional context

Here's where the error happens:

The code seems tested here:

but it doesn't get the error because it's a mock connection.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions