Skip to content

Commit b30ad9c

Browse files
committed
added tests for psycopg capture params
1 parent cf8670b commit b30ad9c

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
@@ -258,6 +258,25 @@ def test_span_name(self):
258258
self.assertEqual(spans_list[6].name, "postgresql")
259259
self.assertEqual(spans_list[7].name, "--")
260260

261+
def test_span_params_attribute(self):
262+
PsycopgInstrumentor().instrument(capture_parameters=True)
263+
cnx = psycopg.connect(database="test")
264+
query = "SELECT * FROM mytable WHERE myparam1 = %s AND myparam2 = %s"
265+
params = ("test", 42)
266+
267+
cursor = cnx.cursor()
268+
269+
cursor.execute(query, params)
270+
spans_list = self.memory_exporter.get_finished_spans()
271+
self.assertEqual(len(spans_list), 1)
272+
self.assertEqual(spans_list[0].name, "SELECT")
273+
assert spans_list[0].attributes is not None
274+
self.assertEqual(spans_list[0].attributes["db.statement"], query)
275+
self.assertEqual(
276+
spans_list[0].attributes["db.statement.parameters"],
277+
str(params)
278+
)
279+
261280
# pylint: disable=unused-argument
262281
def test_not_recording(self):
263282
mock_tracer = mock.Mock()
@@ -473,6 +492,25 @@ async def test_span_name_async(self):
473492
self.assertEqual(spans_list[4].name, "query")
474493
self.assertEqual(spans_list[5].name, "query")
475494

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

0 commit comments

Comments
 (0)