opentracing-python-instrumentation icon indicating copy to clipboard operation
opentracing-python-instrumentation copied to clipboard

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

Open Vaidic opened this issue 4 years ago • 0 comments

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.

Vaidic avatar Jul 19 '21 08:07 Vaidic