uber-common / opentracing-python-instrumentation

A collection of Python instrumentation tools for the OpenTracing API
MIT License
164 stars 58 forks source link

Psycopg2 cursor wrapper doesn't support Composable as sql statement #81

Closed condorcet closed 5 years ago

condorcet commented 5 years ago

In Psycopg2 documentation mentioned that execute method allows Composable object as well as a string (http://initd.org/psycopg/docs/sql.html):

The objects exposed by the sql module can be used to compose a query as a Python string (using the as_string() method) or passed directly to cursor methods such as execute(), executemany(), copy_expert().

Current psycopg2 wrapper based on _dbapi2 that provide common implementation of wrappers for connections / cursors, that's why it doesn't (and maybe shouldn't) supporting something different from the string sql statement.

I made one test to illustrate the problem: https://github.com/condorcet/opentracing-python-instrumentation/blob/psycopg2-sql-as-composable-error/tests/opentracing_instrumentation/test_postgres.py#L149 And you can see results on CI: https://www.travis-ci.org/condorcet/opentracing-python-instrumentation/jobs/533795695#L525

____________________________ test_execute_sql[SQL] _____________________________
tests/opentracing_instrumentation/test_postgres.py:161: in test_execute_sql
    cur.execute(query, (trace_id, ))
opentracing_instrumentation/client_hooks/_dbapi2.py:204: in execute
    cursor_params=self._cursor_params):
opentracing_instrumentation/client_hooks/_dbapi2.py:56: in db_span
    statement = sql_statement.strip()
E   AttributeError: 'SQL' object has no attribute 'strip'
--------------------------- Captured stdout teardown ---------------------------
F
__________________________ test_execute_sql[Composed] __________________________
tests/opentracing_instrumentation/test_postgres.py:161: in test_execute_sql
    cur.execute(query, (trace_id, ))
opentracing_instrumentation/client_hooks/_dbapi2.py:204: in execute
    cursor_params=self._cursor_params):
opentracing_instrumentation/client_hooks/_dbapi2.py:56: in db_span
    statement = sql_statement.strip()
E   AttributeError: 'Composed' object has no attribute 'strip'
--------------------------- Captured stdout teardown ---------------------------
F