Skip to content

Commit e148c9f

Browse files
Add psycopg tests
1 parent bf49c8c commit e148c9f

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

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

+95
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,74 @@ def test_sqlcommenter_enabled(self, event_mocked):
378378
kwargs = event_mocked.call_args[1]
379379
self.assertEqual(kwargs["enable_commenter"], True)
380380

381+
def test_sqlcommenter_enabled_instrument_connection_defaults(self):
382+
with mock.patch(
383+
"opentelemetry.instrumentation.psycopg.psycopg.__version__",
384+
"foobar",
385+
), mock.patch(
386+
"opentelemetry.instrumentation.psycopg.psycopg.pq.__build_version__",
387+
"foobaz",
388+
), mock.patch(
389+
"opentelemetry.instrumentation.psycopg.psycopg.threadsafety",
390+
"123",
391+
), mock.patch(
392+
"opentelemetry.instrumentation.psycopg.psycopg.apilevel",
393+
"123",
394+
), mock.patch(
395+
"opentelemetry.instrumentation.psycopg.psycopg.paramstyle",
396+
"test",
397+
):
398+
cnx = psycopg.connect(database="test")
399+
cnx = PsycopgInstrumentor().instrument_connection(
400+
cnx,
401+
enable_commenter=True,
402+
)
403+
query = "Select 1"
404+
cursor = cnx.cursor()
405+
cursor.execute(query)
406+
spans_list = self.memory_exporter.get_finished_spans()
407+
span = spans_list[0]
408+
span_id = format(span.get_span_context().span_id, "016x")
409+
trace_id = format(span.get_span_context().trace_id, "032x")
410+
self.assertEqual(
411+
MockCursor.execute.call_args[0][0],
412+
f"Select 1 /*db_driver='psycopg%%3Afoobar',dbapi_level='123',dbapi_threadsafety='123',driver_paramstyle='test',libpq_version='foobaz',traceparent='00-{trace_id}-{span_id}-01'*/",
413+
)
414+
415+
def test_sqlcommenter_enabled_instrument_connection_with_options(self):
416+
with mock.patch(
417+
"opentelemetry.instrumentation.psycopg.psycopg.__version__",
418+
"foobar",
419+
), mock.patch(
420+
"opentelemetry.instrumentation.psycopg.psycopg.pq.__build_version__",
421+
"foobaz",
422+
), mock.patch(
423+
"opentelemetry.instrumentation.psycopg.psycopg.threadsafety",
424+
"123",
425+
):
426+
cnx = psycopg.connect(database="test")
427+
cnx = PsycopgInstrumentor().instrument_connection(
428+
cnx,
429+
enable_commenter=True,
430+
commenter_options={
431+
"dbapi_level": False,
432+
"dbapi_threadsafety": True,
433+
"driver_paramstyle": False,
434+
"foo": "ignored",
435+
},
436+
)
437+
query = "Select 1"
438+
cursor = cnx.cursor()
439+
cursor.execute(query)
440+
spans_list = self.memory_exporter.get_finished_spans()
441+
span = spans_list[0]
442+
span_id = format(span.get_span_context().span_id, "016x")
443+
trace_id = format(span.get_span_context().trace_id, "032x")
444+
self.assertEqual(
445+
MockCursor.execute.call_args[0][0],
446+
f"Select 1 /*db_driver='psycopg%%3Afoobar',dbapi_threadsafety='123',libpq_version='foobaz',traceparent='00-{trace_id}-{span_id}-01'*/",
447+
)
448+
381449
@mock.patch("opentelemetry.instrumentation.dbapi.wrap_connect")
382450
def test_sqlcommenter_disabled(self, event_mocked):
383451
cnx = psycopg.connect(database="test")
@@ -388,6 +456,33 @@ def test_sqlcommenter_disabled(self, event_mocked):
388456
kwargs = event_mocked.call_args[1]
389457
self.assertEqual(kwargs["enable_commenter"], False)
390458

459+
def test_sqlcommenter_disabled_default_instrument_connection(self):
460+
cnx = psycopg.connect(database="test")
461+
cnx = PsycopgInstrumentor().instrument_connection(
462+
cnx,
463+
)
464+
query = "Select 1"
465+
cursor = cnx.cursor()
466+
cursor.execute(query)
467+
self.assertEqual(
468+
MockCursor.execute.call_args[0][0],
469+
"Select 1",
470+
)
471+
472+
def test_sqlcommenter_disabled_explicit_instrument_connection(self):
473+
cnx = psycopg.connect(database="test")
474+
cnx = PsycopgInstrumentor().instrument_connection(
475+
cnx,
476+
enable_commenter=False,
477+
)
478+
query = "Select 1"
479+
cursor = cnx.cursor()
480+
cursor.execute(query)
481+
self.assertEqual(
482+
MockCursor.execute.call_args[0][0],
483+
"Select 1",
484+
)
485+
391486

392487
class TestPostgresqlIntegrationAsync(
393488
PostgresqlIntegrationTestMixin, TestBase, IsolatedAsyncioTestCase

0 commit comments

Comments
 (0)