Skip to content

Commit ab739dc

Browse files
committed
added tests for psycopg capture params
1 parent 0296ba8 commit ab739dc

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

instrumentation/opentelemetry-instrumentation-psycopg/tests/test_psycopg_integration.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,25 @@ def test_span_name(self):
264264
self.assertEqual(spans_list[6].name, "postgresql")
265265
self.assertEqual(spans_list[7].name, "--")
266266

267+
def test_span_params_attribute(self):
268+
PsycopgInstrumentor().instrument(capture_parameters=True)
269+
cnx = psycopg.connect(database="test")
270+
query = "SELECT * FROM mytable WHERE myparam1 = %s AND myparam2 = %s"
271+
params = ("test", 42)
272+
273+
cursor = cnx.cursor()
274+
275+
cursor.execute(query, params)
276+
spans_list = self.memory_exporter.get_finished_spans()
277+
self.assertEqual(len(spans_list), 1)
278+
self.assertEqual(spans_list[0].name, "SELECT")
279+
assert spans_list[0].attributes is not None
280+
self.assertEqual(spans_list[0].attributes["db.statement"], query)
281+
self.assertEqual(
282+
spans_list[0].attributes["db.statement.parameters"],
283+
str(params)
284+
)
285+
267286
# pylint: disable=unused-argument
268287
def test_not_recording(self):
269288
mock_tracer = mock.Mock()
@@ -479,6 +498,25 @@ async def test_span_name_async(self):
479498
self.assertEqual(spans_list[4].name, "query")
480499
self.assertEqual(spans_list[5].name, "query")
481500

501+
async def test_span_params_attribute(self):
502+
PsycopgInstrumentor().instrument(capture_parameters=True)
503+
cnx = await psycopg.AsyncConnection.connect("test")
504+
query = "SELECT * FROM mytable WHERE myparam1 = %s AND myparam2 = %s"
505+
params = ("test", 42)
506+
async with cnx.cursor() as cursor:
507+
508+
await cursor.execute(query, params)
509+
510+
spans_list = self.memory_exporter.get_finished_spans()
511+
self.assertEqual(len(spans_list), 1)
512+
self.assertEqual(spans_list[0].name, "SELECT")
513+
assert spans_list[0].attributes is not None
514+
self.assertEqual(spans_list[0].attributes["db.statement"], query)
515+
self.assertEqual(
516+
spans_list[0].attributes["db.statement.parameters"],
517+
str(params)
518+
)
519+
482520
# pylint: disable=unused-argument
483521
async def test_not_recording_async(self):
484522
mock_tracer = mock.Mock()

0 commit comments

Comments
 (0)