From 05a69e6985155d183a3a39fe05f34f0265a2c594 Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Thu, 3 Jul 2025 17:08:29 +0200 Subject: [PATCH 1/2] psycopg2: fix cursor execute and executemany signatures The sql parameter is called query and the parameters are called vars and vars_list. --- .../instrumentation/packages/psycopg2.py | 6 ++++++ tests/instrumentation/psycopg2_tests.py | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/elasticapm/instrumentation/packages/psycopg2.py b/elasticapm/instrumentation/packages/psycopg2.py index a058597da..850d849e3 100644 --- a/elasticapm/instrumentation/packages/psycopg2.py +++ b/elasticapm/instrumentation/packages/psycopg2.py @@ -59,6 +59,12 @@ def _bake_sql(self, sql): def extract_signature(self, sql): return extract_signature(sql) + def execute(self, query, vars=None): + return self._trace_sql(self.__wrapped__.execute, query, vars) + + def executemany(self, query, vars_list): + return self._trace_sql(self.__wrapped__.executemany, query, vars_list) + def __enter__(self): return PGCursorProxy(self.__wrapped__.__enter__(), destination_info=self._self_destination_info) diff --git a/tests/instrumentation/psycopg2_tests.py b/tests/instrumentation/psycopg2_tests.py index 70c0d6329..59fe211a6 100644 --- a/tests/instrumentation/psycopg2_tests.py +++ b/tests/instrumentation/psycopg2_tests.py @@ -266,6 +266,25 @@ def test_fully_qualified_table_name(): assert "SELECT FROM db.schema.mytable" == actual +@pytest.mark.integrationtest +@pytest.mark.skipif(not has_postgres_configured, reason="PostgresSQL not configured") +def test_cursor_execute_signature(instrument, postgres_connection, elasticapm_client): + cursor = postgres_connection.cursor() + res = cursor.execute(query="SELECT 1", vars=None) + assert res + + +@pytest.mark.integrationtest +@pytest.mark.skipif(not has_postgres_configured, reason="PostgresSQL not configured") +def test_cursor_executemany_signature(instrument, postgres_connection, elasticapm_client): + cursor = postgres_connection.cursor() + res = cursor.executemany( + query="INSERT INTO test VALUES (%s, %s)", + vars_list=((4, "four"),), + ) + assert res is None + + @pytest.mark.integrationtest @pytest.mark.skipif(not has_postgres_configured, reason="PostgresSQL not configured") def test_destination(instrument, postgres_connection, elasticapm_client): From 1756304925a8b2b5d6cad3113e9e61cb0d86107c Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Fri, 4 Jul 2025 09:13:32 +0200 Subject: [PATCH 2/2] Fix execute test --- tests/instrumentation/psycopg2_tests.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/instrumentation/psycopg2_tests.py b/tests/instrumentation/psycopg2_tests.py index 59fe211a6..1cee5b269 100644 --- a/tests/instrumentation/psycopg2_tests.py +++ b/tests/instrumentation/psycopg2_tests.py @@ -270,8 +270,10 @@ def test_fully_qualified_table_name(): @pytest.mark.skipif(not has_postgres_configured, reason="PostgresSQL not configured") def test_cursor_execute_signature(instrument, postgres_connection, elasticapm_client): cursor = postgres_connection.cursor() - res = cursor.execute(query="SELECT 1", vars=None) - assert res + cursor.execute(query="SELECT 1", vars=None) + row = cursor.fetchone() + + assert row @pytest.mark.integrationtest