Skip to content

Commit eea0796

Browse files
authored
psycopg/psycopg_async: fix handling of ServerCursor / AsyncServerCursor (#2489)
ServerCursor and AsyncServerCursor execute method has a different interface than Cursor / AsyncCursor. So implement one that works with both in our ProxyCursor.
1 parent 2e2e104 commit eea0796

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

elasticapm/instrumentation/packages/asyncio/psycopg_async.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ def _bake_sql(self, sql):
5555
def extract_signature(self, sql):
5656
return extract_signature(sql)
5757

58-
async def execute(self, query, params=None, *, prepare=None, binary=None, **kwargs):
59-
return await self._trace_sql(self.__wrapped__.execute, query, params, prepare=prepare, binary=binary, **kwargs)
58+
async def execute(self, query, params=None, **kwargs):
59+
return await self._trace_sql(self.__wrapped__.execute, query, params, **kwargs)
6060

6161
async def executemany(self, query, params_seq, **kwargs):
6262
return await self._trace_sql(self.__wrapped__.executemany, query, params_seq, **kwargs)

elasticapm/instrumentation/packages/psycopg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ def _bake_sql(self, sql):
5555
def extract_signature(self, sql):
5656
return extract_signature(sql)
5757

58-
def execute(self, query, params=None, *, prepare=None, binary=None, **kwargs):
59-
return self._trace_sql(self.__wrapped__.execute, query, params, prepare=prepare, binary=binary, **kwargs)
58+
def execute(self, query, params=None, **kwargs):
59+
return self._trace_sql(self.__wrapped__.execute, query, params, **kwargs)
6060

6161
def executemany(self, query, params_seq, **kwargs):
6262
return self._trace_sql(self.__wrapped__.executemany, query, params_seq, **kwargs)

tests/instrumentation/asyncio_tests/psycopg_tests.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,10 @@ async def test_executemany(instrument, postgres_connection, elasticapm_client):
119119
assert span["action"] == "query"
120120
assert span["sync"] == False
121121
assert span["name"] == "INSERT INTO test"
122+
123+
124+
async def test_server_cursor_execute(instrument, postgres_connection, elasticapm_client):
125+
cursor = postgres_connection.cursor(name="async_server_cursor")
126+
assert isinstance(cursor, psycopg.AsyncServerCursor)
127+
record = await cursor.execute(query="SELECT 1", params=None, binary=None)
128+
assert record

tests/instrumentation/psycopg_tests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,12 @@ def test_psycopg_connection(instrument, elasticapm_transaction, postgres_connect
279279
host = os.environ.get("POSTGRES_HOST", "localhost")
280280
assert span["name"] == f"psycopg.connect {host}:5432"
281281
assert span["action"] == "connect"
282+
283+
284+
@pytest.mark.integrationtest
285+
@pytest.mark.skipif(not has_postgres_configured, reason="PostgresSQL not configured")
286+
def test_server_cursor_execute(instrument, postgres_connection, elasticapm_client):
287+
cursor = postgres_connection.cursor(name="server_cursor")
288+
assert isinstance(cursor, psycopg.ServerCursor)
289+
record = cursor.execute(query="SELECT 1", params=None, binary=True)
290+
assert record

0 commit comments

Comments
 (0)