Skip to content

Commit 5bd1e94

Browse files
Add and update tests
1 parent a0c9940 commit 5bd1e94

File tree

1 file changed

+59
-3
lines changed

1 file changed

+59
-3
lines changed

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

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
from unittest import mock
1717

1818
import psycopg2
19+
import pytest
1920

2021
import opentelemetry.instrumentation.psycopg2
2122
from opentelemetry import trace
22-
from opentelemetry.instrumentation.psycopg2 import Psycopg2Instrumentor
23+
from opentelemetry.instrumentation.psycopg2 import (
24+
Psycopg2Instrumentor,
25+
)
2326
from opentelemetry.sdk import resources
2427
from opentelemetry.test.test_base import TestBase
2528

@@ -66,6 +69,10 @@ def get_dsn_parameters(self): # pylint: disable=no-self-use
6669

6770

6871
class TestPostgresqlIntegration(TestBase):
72+
@pytest.fixture(autouse=True)
73+
def inject_fixtures(self, caplog):
74+
self.caplog = caplog # pylint: disable=attribute-defined-outside-init
75+
6976
def setUp(self):
7077
super().setUp()
7178
self.cursor_mock = mock.patch(
@@ -190,7 +197,13 @@ def test_instrument_connection(self):
190197
spans_list = self.memory_exporter.get_finished_spans()
191198
self.assertEqual(len(spans_list), 0)
192199

193-
cnx = Psycopg2Instrumentor().instrument_connection(cnx)
200+
instrumentor = Psycopg2Instrumentor()
201+
original_cursor_factory = cnx.cursor_factory
202+
cnx = instrumentor.instrument_connection(cnx)
203+
self.assertTrue(instrumentor._is_instrumented_by_opentelemetry)
204+
self.assertEqual(
205+
instrumentor._otel_cursor_factory, original_cursor_factory
206+
)
194207
cursor = cnx.cursor()
195208
cursor.execute(query)
196209

@@ -207,9 +220,52 @@ def test_instrument_connection_with_instrument(self):
207220
spans_list = self.memory_exporter.get_finished_spans()
208221
self.assertEqual(len(spans_list), 0)
209222

210-
Psycopg2Instrumentor().instrument()
223+
instrumentor = Psycopg2Instrumentor()
224+
instrumentor.instrument()
225+
self.assertTrue(instrumentor._is_instrumented_by_opentelemetry)
226+
211227
cnx = psycopg2.connect(database="test")
212228
cnx = Psycopg2Instrumentor().instrument_connection(cnx)
229+
self.assertEqual(
230+
self.caplog.records[0].getMessage(),
231+
"Attempting to instrument Psycopg2 connection while already instrumented",
232+
)
233+
self.assertTrue(instrumentor._is_instrumented_by_opentelemetry)
234+
# Will not set cursor_factory if already instrumented
235+
self.assertEqual(instrumentor._otel_cursor_factory, None)
236+
cursor = cnx.cursor()
237+
cursor.execute(query)
238+
239+
spans_list = self.memory_exporter.get_finished_spans()
240+
self.assertEqual(len(spans_list), 1)
241+
242+
def test_instrument_connection_with_instrument_connection(self):
243+
cnx = psycopg2.connect(database="test")
244+
query = "SELECT * FROM test"
245+
cursor = cnx.cursor()
246+
cursor.execute(query)
247+
248+
spans_list = self.memory_exporter.get_finished_spans()
249+
self.assertEqual(len(spans_list), 0)
250+
251+
cnx = psycopg2.connect(database="test")
252+
original_cursor_factory = cnx.cursor_factory
253+
instrumentor = Psycopg2Instrumentor()
254+
cnx = instrumentor.instrument_connection(cnx)
255+
self.assertTrue(instrumentor._is_instrumented_by_opentelemetry)
256+
self.assertEqual(
257+
instrumentor._otel_cursor_factory, original_cursor_factory
258+
)
259+
260+
cnx = instrumentor.instrument_connection(cnx)
261+
self.assertEqual(
262+
self.caplog.records[0].getMessage(),
263+
"Attempting to instrument Psycopg2 connection while already instrumented",
264+
)
265+
self.assertTrue(instrumentor._is_instrumented_by_opentelemetry)
266+
self.assertEqual(
267+
instrumentor._otel_cursor_factory, original_cursor_factory
268+
)
213269
cursor = cnx.cursor()
214270
cursor.execute(query)
215271

0 commit comments

Comments
 (0)