Skip to content

Psycopg2Instrumentor doesn't work for cursors with non-default cursor_factory #578

Open
@r-daysleeper

Description

@r-daysleeper

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions