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

Psycopg2 instrumentation does not create spans in Django 4.2.0 #1774

Closed
phillipuniverse opened this issue Apr 27, 2023 · 2 comments
Closed
Labels
bug Something isn't working

Comments

@phillipuniverse
Copy link
Contributor

Describe your environment

Recently upgraded to Django 4.2 and noticed that opentelemetry.instrumentation.psycopg2.client spans were not emitted. I am using Psycopg2 version 2.9.6.

The issue is that the connection's cursor_factory function is overridden by Django after otel attempts to override it on the connection. Here's the code that overrides the Psycopg2 cursor_factory function:

kwargs["cursor_factory"] = _new_cursor_factory(**new_factory_kwargs)
connection = connect_method(*args, **kwargs)
self.get_connection_attributes(connection)
return connection

The below screenshot illustrates the problem. There are a few things going on but I drew arrows to try to highlight the problematic areas:

Screenshot 2023-04-26 at 10 49 03 PM

This method is when Django creates the new connection. You can see the big blue line is where the debugger is paused (L272). At this point, the cursor_factory is the one from otel. But immediately after this, on lines 272 and 285 Django overrides the cursor_factory to be its own, effectively neutering otel. No database spans are emitted.

This is somewhat related to #1751 as this might need to be considered for psycopg3 support.

Describe any aspect of your environment relevant to the problem, including your Python version, platform, version numbers of installed dependencies, information about your cloud hosting provider, etc. If you're reporting a problem with a specific version of a library in this repo, please check whether the problem has been fixed on main.

Steps to reproduce

Use Django 4.2 and Psycopg2. The instrumentation will run and instrument the connection/cursor_factory but Django will override it. All other Django versions < 4.2 (including 4.1) work fine.

What is the expected behavior?

Psycopg2 spans create

What is the actual behavior?

No spans created.

Additional context
Add any other context about the problem here.

@phillipuniverse phillipuniverse added the bug Something isn't working label Apr 27, 2023
@phillipuniverse
Copy link
Contributor Author

I went to link the regression in Django, turns out this has already been fixed at django/django@73cbb37 and will be apart of 4.2.1, due to be released May 3 2023 (1 week).

I'll leave this open until I verify everything working with Django 4.2.1.

@phillipuniverse phillipuniverse changed the title Psycopg2 instrumentation does not create spans in Django 4.2 Psycopg2 instrumentation does not create spans in Django 4.2.0 Apr 27, 2023
@phillipuniverse
Copy link
Contributor Author

Confirmed fixed in Django 4.2.1

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

No branches or pull requests

1 participant