Skip to content

Commit bf49c8c

Browse files
Add psycopg2 tests
1 parent 4783f3a commit bf49c8c

1 file changed

Lines changed: 95 additions & 0 deletions

File tree

instrumentation/opentelemetry-instrumentation-psycopg2/tests/test_psycopg2_integration.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,74 @@ def test_sqlcommenter_enabled(self, event_mocked):
261261
kwargs = event_mocked.call_args[1]
262262
self.assertEqual(kwargs["enable_commenter"], True)
263263

264+
def test_sqlcommenter_enabled_instrument_connection_defaults(self):
265+
with mock.patch(
266+
"opentelemetry.instrumentation.psycopg2.psycopg2.__version__",
267+
"foobar",
268+
), mock.patch(
269+
"opentelemetry.instrumentation.psycopg2.psycopg2.__libpq_version__",
270+
"foobaz",
271+
), mock.patch(
272+
"opentelemetry.instrumentation.psycopg2.psycopg2.threadsafety",
273+
"123",
274+
), mock.patch(
275+
"opentelemetry.instrumentation.psycopg2.psycopg2.apilevel",
276+
"123",
277+
), mock.patch(
278+
"opentelemetry.instrumentation.psycopg2.psycopg2.paramstyle",
279+
"test",
280+
):
281+
cnx = psycopg2.connect(database="test")
282+
cnx = Psycopg2Instrumentor().instrument_connection(
283+
cnx,
284+
enable_commenter=True,
285+
)
286+
query = "Select 1"
287+
cursor = cnx.cursor()
288+
cursor.execute(query)
289+
spans_list = self.memory_exporter.get_finished_spans()
290+
span = spans_list[0]
291+
span_id = format(span.get_span_context().span_id, "016x")
292+
trace_id = format(span.get_span_context().trace_id, "032x")
293+
self.assertEqual(
294+
MockCursor.execute.call_args[0][0],
295+
f"Select 1 /*db_driver='psycopg2%%3Afoobar',dbapi_level='123',dbapi_threadsafety='123',driver_paramstyle='test',libpq_version='foobaz',traceparent='00-{trace_id}-{span_id}-01'*/",
296+
)
297+
298+
def test_sqlcommenter_enabled_instrument_connection_with_options(self):
299+
with mock.patch(
300+
"opentelemetry.instrumentation.psycopg2.psycopg2.__version__",
301+
"foobar",
302+
), mock.patch(
303+
"opentelemetry.instrumentation.psycopg2.psycopg2.__libpq_version__",
304+
"foobaz",
305+
), mock.patch(
306+
"opentelemetry.instrumentation.psycopg2.psycopg2.threadsafety",
307+
"123",
308+
):
309+
cnx = psycopg2.connect(database="test")
310+
cnx = Psycopg2Instrumentor().instrument_connection(
311+
cnx,
312+
enable_commenter=True,
313+
commenter_options={
314+
"dbapi_level": False,
315+
"dbapi_threadsafety": True,
316+
"driver_paramstyle": False,
317+
"foo": "ignored",
318+
},
319+
)
320+
query = "Select 1"
321+
cursor = cnx.cursor()
322+
cursor.execute(query)
323+
spans_list = self.memory_exporter.get_finished_spans()
324+
span = spans_list[0]
325+
span_id = format(span.get_span_context().span_id, "016x")
326+
trace_id = format(span.get_span_context().trace_id, "032x")
327+
self.assertEqual(
328+
MockCursor.execute.call_args[0][0],
329+
f"Select 1 /*db_driver='psycopg2%%3Afoobar',dbapi_threadsafety='123',libpq_version='foobaz',traceparent='00-{trace_id}-{span_id}-01'*/",
330+
)
331+
264332
@mock.patch("opentelemetry.instrumentation.dbapi.wrap_connect")
265333
def test_sqlcommenter_disabled(self, event_mocked):
266334
cnx = psycopg2.connect(database="test")
@@ -271,6 +339,33 @@ def test_sqlcommenter_disabled(self, event_mocked):
271339
kwargs = event_mocked.call_args[1]
272340
self.assertEqual(kwargs["enable_commenter"], False)
273341

342+
def test_sqlcommenter_disabled_default_instrument_connection(self):
343+
cnx = psycopg2.connect(database="test")
344+
cnx = Psycopg2Instrumentor().instrument_connection(
345+
cnx,
346+
)
347+
query = "Select 1"
348+
cursor = cnx.cursor()
349+
cursor.execute(query)
350+
self.assertEqual(
351+
MockCursor.execute.call_args[0][0],
352+
"Select 1",
353+
)
354+
355+
def test_sqlcommenter_disabled_explicit_instrument_connection(self):
356+
cnx = psycopg2.connect(database="test")
357+
cnx = Psycopg2Instrumentor().instrument_connection(
358+
cnx,
359+
enable_commenter=False,
360+
)
361+
query = "Select 1"
362+
cursor = cnx.cursor()
363+
cursor.execute(query)
364+
self.assertEqual(
365+
MockCursor.execute.call_args[0][0],
366+
"Select 1",
367+
)
368+
274369
def test_no_op_tracer_provider(self):
275370
Psycopg2Instrumentor().instrument(
276371
tracer_provider=trace.NoOpTracerProvider()

0 commit comments

Comments
 (0)