1616from unittest import mock
1717
1818import psycopg2
19+ import pytest
1920
2021import opentelemetry .instrumentation .psycopg2
2122from opentelemetry import trace
22- from opentelemetry .instrumentation .psycopg2 import Psycopg2Instrumentor
23+ from opentelemetry .instrumentation .psycopg2 import (
24+ Psycopg2Instrumentor ,
25+ )
2326from opentelemetry .sdk import resources
2427from opentelemetry .test .test_base import TestBase
2528
@@ -66,6 +69,10 @@ def get_dsn_parameters(self): # pylint: disable=no-self-use
6669
6770
6871class 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+
227+ cnx = psycopg2 .connect (database = "test" )
228+ 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 ):
211243 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+
212260 cnx = Psycopg2Instrumentor ().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
@@ -218,39 +274,50 @@ def test_instrument_connection_with_instrument(self):
218274
219275 # pylint: disable=unused-argument
220276 def test_uninstrument_connection_with_instrument (self ):
221- Psycopg2Instrumentor ().instrument ()
277+ instrumentor = Psycopg2Instrumentor ()
278+ instrumentor .instrument ()
222279 cnx = psycopg2 .connect (database = "test" )
223280 query = "SELECT * FROM test"
224281 cursor = cnx .cursor ()
225282 cursor .execute (query )
226283
227284 spans_list = self .memory_exporter .get_finished_spans ()
228285 self .assertEqual (len (spans_list ), 1 )
286+ self .assertTrue (instrumentor ._is_instrumented_by_opentelemetry )
229287
230288 cnx = Psycopg2Instrumentor ().uninstrument_connection (cnx )
231289 cursor = cnx .cursor ()
232290 cursor .execute (query )
233291
234292 spans_list = self .memory_exporter .get_finished_spans ()
235293 self .assertEqual (len (spans_list ), 1 )
294+ self .assertFalse (instrumentor ._is_instrumented_by_opentelemetry )
236295
237296 # pylint: disable=unused-argument
238297 def test_uninstrument_connection_with_instrument_connection (self ):
239298 cnx = psycopg2 .connect (database = "test" )
240- Psycopg2Instrumentor ().instrument_connection (cnx )
299+ original_cursor_factory = cnx .cursor_factory
300+ instrumentor = Psycopg2Instrumentor ()
301+ instrumentor .instrument_connection (cnx )
241302 query = "SELECT * FROM test"
242303 cursor = cnx .cursor ()
243304 cursor .execute (query )
244305
245306 spans_list = self .memory_exporter .get_finished_spans ()
246307 self .assertEqual (len (spans_list ), 1 )
308+ self .assertTrue (instrumentor ._is_instrumented_by_opentelemetry )
309+ self .assertEqual (
310+ instrumentor ._otel_cursor_factory , original_cursor_factory
311+ )
247312
248313 cnx = Psycopg2Instrumentor ().uninstrument_connection (cnx )
249314 cursor = cnx .cursor ()
250315 cursor .execute (query )
251316
252317 spans_list = self .memory_exporter .get_finished_spans ()
253318 self .assertEqual (len (spans_list ), 1 )
319+ self .assertFalse (instrumentor ._is_instrumented_by_opentelemetry )
320+ self .assertEqual (instrumentor ._otel_cursor_factory , None )
254321
255322 @mock .patch ("opentelemetry.instrumentation.dbapi.wrap_connect" )
256323 def test_sqlcommenter_enabled (self , event_mocked ):
0 commit comments