-
Notifications
You must be signed in to change notification settings - Fork 234
Description
Describe the bug:
If you make use of the function psycopg2.extensions.quote_ident docs, a TypeError exception is thrown. This is because the cursor object, when under instrumentation from ES-APM, is an instance of PGCursorProxy, not the actual cursor, and quote_ident does not allow this because the type is checked in the C code link. With the error message saying TypeError: argument 2 must be a connection or a cursor. Inspecting the cur object at a debug breakpoint we can see it is the proxy object:
>>> cur
<PGCursorProxy at 0x7fd7f70f9a88 for NamedTupleCursor at 0x7fd7f70f0148>
>>> type(cur)
<class 'elasticapm.instrumentation.packages.psycopg2.PGCursorProxy'>
>>> type(cur.__wrapped__)
<class 'psycopg2.extras.NamedTupleCursor'>
To Reproduce
from psycopg2.extensions import quote_ident
....
....
with psycopg2.connect(DSN) as conn:
with conn.cursor() as curs:
ident = quote_ident("column_name", cur)
curs.execute(f"SELECT {column_name} FROM data.table;")
data = curs.fetchall()passing the underlying wrapped cursor works:
from psycopg2.extensions import quote_ident
....
....
with psycopg2.connect(DSN) as conn:
with conn.cursor() as curs:
ident = quote_ident("column_name", cur.__wrapped__)
curs.execute(f"SELECT {column_name} FROM data.table;")
data = curs.fetchall()Environment (please complete the following information)
- OS: Linux
- Python version: 3.6.4
- Agent version: 5.2.2
Additional context
Looks like the same problem was encountered here DataDog/dd-trace-py#474, and was fixed by also patching quote_ident to pass the __wrapped__ object. Testing this out with a basic in-module proxy function worked, but obviously a patch right at the top level from the apm module would sort it out.
def quote_ident(string, cursor):
return psycopg2.extensions.quote_ident(string, cursor.__wrapped__)