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

Psyopg2 Instrumentation doesn't work with RealDictCursor #381

Closed
bllchmbrs opened this issue Aug 16, 2024 · 3 comments
Closed

Psyopg2 Instrumentation doesn't work with RealDictCursor #381

bllchmbrs opened this issue Aug 16, 2024 · 3 comments
Assignees
Labels
bug Bug related to the Logfire Python SDK

Comments

@bllchmbrs
Copy link
Contributor

Description

I'm still trying to get the exact repro, but I cannot instrument psycopg2 and get results when I use from psycopg2.extras import RealDictCursor

I am using the instrumentation, BUT when I go to run it, no values show up for cursors that go through that path.

import logfire
import psycopg2 as pg
from psycopg2.extras import RealDictCursor

logfire.instrument_psycopg()

conn = pg.connect()

def get_plain_cursor(conn):
    return conn.cursor()

def get_cursor(conn):
    return conn.cursor(cursor_factory=RealDictCursor)


def run_query(cur, query: str, args=None):
    try:
        if isinstance(args, tuple):
            cur.execute(query, args)
        elif isinstance(args, list):
            cur.executemany(query, args)
        else:
            cur.execute(query)
    except Exception as e:
        logger.error("Failed to run query", error=e)
        raise e

with get_plain_cursor(conn) as pc:
    run_query(pc, "SELECT 1")
# this shows the select query in logfire

with get_cursor(conn) as pc:
    run_query(pc, "SELECT 1")
# this does NOT show the select query

Python, Logfire & OS Versions, related packages (not required)

No response

@bllchmbrs bllchmbrs added the bug Bug related to the Logfire Python SDK label Aug 16, 2024
@Kludex
Copy link
Member

Kludex commented Dec 24, 2024

Hey @bllchmbrs,

Sorry for the late reply here.

It seems this is an issue with OpenTelemetry itself: open-telemetry/opentelemetry-python-contrib#578

I'll see if I can open a PR on their side, but for the time being, you can move the cursor_factory parameter up to the connect(cursor_factory=...) and remove it from the cursor, as per this comment.

@Kludex Kludex self-assigned this Dec 24, 2024
@Kludex
Copy link
Member

Kludex commented Dec 30, 2024

I tried a bit, but that code source is giving me a hard time. I'll close this as it's really an OpenTelemetry issue, as per last message.

But I'll try to get back to it when my PRs about improving type hint on their side are merged, specifically this one: open-telemetry/opentelemetry-python-contrib#3068

@Kludex Kludex closed this as not planned Won't fix, can't repro, duplicate, stale Dec 30, 2024
@Kludex
Copy link
Member

Kludex commented Dec 30, 2024

Let me know if you can use the workaround mentioned here:

I'll see if I can open a PR on their side, but for the time being, you can move the cursor_factory parameter up to the connect(cursor_factory=...) and remove it from the cursor, as per open-telemetry/opentelemetry-python-contrib#578 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug related to the Logfire Python SDK
Projects
None yet
Development

No branches or pull requests

2 participants