Skip to content

Commit 4534249

Browse files
authored
psycopg2: fix cursor execute and executemany signatures (#2331)
* psycopg2: fix cursor execute and executemany signatures The sql parameter is called query and the parameters are called vars and vars_list. * Fix execute test
1 parent ea902ae commit 4534249

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

elasticapm/instrumentation/packages/psycopg2.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ def _bake_sql(self, sql):
5959
def extract_signature(self, sql):
6060
return extract_signature(sql)
6161

62+
def execute(self, query, vars=None):
63+
return self._trace_sql(self.__wrapped__.execute, query, vars)
64+
65+
def executemany(self, query, vars_list):
66+
return self._trace_sql(self.__wrapped__.executemany, query, vars_list)
67+
6268
def __enter__(self):
6369
return PGCursorProxy(self.__wrapped__.__enter__(), destination_info=self._self_destination_info)
6470

tests/instrumentation/psycopg2_tests.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,27 @@ def test_fully_qualified_table_name():
266266
assert "SELECT FROM db.schema.mytable" == actual
267267

268268

269+
@pytest.mark.integrationtest
270+
@pytest.mark.skipif(not has_postgres_configured, reason="PostgresSQL not configured")
271+
def test_cursor_execute_signature(instrument, postgres_connection, elasticapm_client):
272+
cursor = postgres_connection.cursor()
273+
cursor.execute(query="SELECT 1", vars=None)
274+
row = cursor.fetchone()
275+
276+
assert row
277+
278+
279+
@pytest.mark.integrationtest
280+
@pytest.mark.skipif(not has_postgres_configured, reason="PostgresSQL not configured")
281+
def test_cursor_executemany_signature(instrument, postgres_connection, elasticapm_client):
282+
cursor = postgres_connection.cursor()
283+
res = cursor.executemany(
284+
query="INSERT INTO test VALUES (%s, %s)",
285+
vars_list=((4, "four"),),
286+
)
287+
assert res is None
288+
289+
269290
@pytest.mark.integrationtest
270291
@pytest.mark.skipif(not has_postgres_configured, reason="PostgresSQL not configured")
271292
def test_destination(instrument, postgres_connection, elasticapm_client):

0 commit comments

Comments
 (0)