Skip to content

Hook for psycopg2 (_dbapi2) cursor not called when using with resources constructs #117

@Vaidic

Description

@Vaidic

While using opentracing-python-instrumentation for Postgres connection I noticed that the calls made inside the python with resources block are not being traced.

The DB calls made in the following code block is properly traced:

           conn = get_connection() // <--- this method returns the connection object already created at initialization
           cur = conn.cursor()
           cur.execute("SELECT 1")  
           cur.close()

However, if written with with resources the calls are not being traced:

            with get_connection() as conn:
                with conn.cursor() as cur:
                    cur.execute("SELECT 1")  

or even the following does not work :

            conn = get_connection()
            with conn.cursor() as cur:
                cur.execute("SELECT 1")  

Based on my debugging, I see that the call to ConnectionWrapper.cursor (in file _dbapi2.py) is not made when the cursor is called from within the with resources block, and then the subsequent calls to 'execute' hook (in psycopg2.py) are never made.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions