Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
alexmojaki opened this issue May 13, 2024 · 5 comments · May be fixed by #2795
Labels
bug Something isn't working

Comments

@alexmojaki
Copy link
Contributor

alexmojaki commented May 13, 2024

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.

@xrmx
Copy link
Contributor

xrmx commented May 13, 2024

@alexmojaki would be great if you can open a PR

@qiuge615
Copy link
Contributor

@xrmx regarding this issue, shall I reference mysql/sqlite3 to fix this issue? Tested this approach locally, the instrument works and the trace can be displayed.
Shall I create another issue to add the two parameters _otel_orig_cursor_factory and _is_instrumented_by_opentelemetry in connection object? The same approach may applies for psycopg.

@xrmx
Copy link
Contributor

xrmx commented Jul 31, 2024

@qiuge615 If you have already some code open a PR so we can discuss looking at it :) Thanks!

@qiuge615
Copy link
Contributor

@xrmx Appreciate your quick reply, I will create a PR with the changes, thank you!

@qiuge615 qiuge615 linked a pull request Aug 13, 2024 that will close this issue
7 tasks
@qiuge615
Copy link
Contributor

@xrmx Created PR with the fix, please review, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants