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 doesn't work for cursors with non-default cursor_factory #578

Open
r-daysleeper opened this issue Jul 13, 2021 · 5 comments
Labels
bug Something isn't working help wanted Extra attention is needed instrumentation triaged

Comments

@r-daysleeper
Copy link

class DatabaseApiIntegration(dbapi.DatabaseApiIntegration):
def wrapped_connection(
self,
connect_method: typing.Callable[..., typing.Any],
args: typing.Tuple[typing.Any, typing.Any],
kwargs: typing.Dict[typing.Any, typing.Any],
):
"""Add object proxy to connection object."""
base_cursor_factory = kwargs.pop("cursor_factory", None)
new_factory_kwargs = {"db_api": self}
if base_cursor_factory:
new_factory_kwargs["base_factory"] = base_cursor_factory
kwargs["cursor_factory"] = _new_cursor_factory(**new_factory_kwargs)
connection = connect_method(*args, **kwargs)
self.get_connection_attributes(connection)
return connection

When wrapping the connection, the default cursor_factory is set for the connection, but the connection is returned as is rather than a proxy. Therefore, when a cursor is created with the connection_factory argument passed, the instrumentation does not happen.

Steps to reproduce
Set up the Psycopg2Instrumentor, create a cursor passing the cursor_factory argument, and run a query.

What is the expected behavior?
Spans produced/exported

What is the actual behavior?
No spans produced/exported

@r-daysleeper r-daysleeper added the bug Something isn't working label Jul 13, 2021
@github-actions
Copy link

This issue was marked stale due to lack of activity. It will be closed in 30 days.

@RaguRamG
Copy link

Is there a fix for this issue?

@srikanthccv srikanthccv added help wanted Extra attention is needed instrumentation labels Mar 19, 2022
@AlexanderLW
Copy link

I am also facing this same issue but for creating a cursor using the RealDictCursos cursor factory.
cur = client.cursor(cursor_factory=psycopg2.extras.RealDictCursor)

No tracing is being submitted, but when I turn this off for the whole app (and comment out the breaking code now that it's getting tuples) I start seeing my traces appear.

Is there any fix for this or can anyone comment on this issue?

@RaguRamG
Copy link

RaguRamG commented Apr 16, 2022

@AlexanderLW, @srikanthccv I have gone through the code. We have to add the {'cursor_factory': psycopg2.extras.RealDictCursor} in connection params.

you must have had conn_params in your code like below.
self._conn_parms = {'host': host, 'post': port, 'username': username, 'password': password, 'cursor_factory': psycopg2.extras.RealDictCursor}

It will fix the issue.

For more details, understand wrapped_connection method in python3.9/site-packages/opentelemetry/instrumentation/psycopg2/init.py

@AlexanderLW
Copy link

Thanks @RaguRamG !

For anyone else that runs into this, I moved the cursor_factory to the connection, and then I had to remove the cursor_factory from any and all cursor declarations. Doing that allowed my traces to appear as I would expect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed instrumentation triaged
Projects
None yet
Development

No branches or pull requests

4 participants