Skip to content

Commit 6f67b33

Browse files
Rm cursor_factory restore at uninstrument_connection
1 parent 238746e commit 6f67b33

File tree

2 files changed

+13
-50
lines changed

2 files changed

+13
-50
lines changed

instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/__init__.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,6 @@ class Psycopg2Instrumentor(BaseInstrumentor):
146146

147147
_DATABASE_SYSTEM = "postgresql"
148148

149-
_otel_cursor_factories = {}
150-
151149
def instrumentation_dependencies(self) -> Collection[str]:
152150
return _instruments
153151

@@ -176,8 +174,8 @@ def _uninstrument(self, **kwargs):
176174
dbapi.unwrap_connect(psycopg2, "connect")
177175

178176
# TODO(owais): check if core dbapi can do this for all dbapi implementations e.g, pymysql and mysql
177+
@staticmethod
179178
def instrument_connection(
180-
self,
181179
connection,
182180
tracer_provider: typing.Optional[trace_api.TracerProvider] = None,
183181
):
@@ -195,23 +193,17 @@ def instrument_connection(
195193
"""
196194
# TODO Add check for attempt to instrument a connection when already instrumented
197195
# https://github.com/open-telemetry/opentelemetry-python-contrib/issues/3138
198-
199-
# Save cursor_factory in instrumentor map because
200-
# psycopg2 connection type does not allow arbitrary attrs
201-
self._otel_cursor_factories[connection] = connection.cursor_factory
202196
connection.cursor_factory = _new_cursor_factory(
203197
base_factory=connection.cursor_factory,
204198
tracer_provider=tracer_provider,
205199
)
206-
_logger.warning("factories: %s", self._otel_cursor_factories)
207-
208200
return connection
209201

210202
# TODO(owais): check if core dbapi can do this for all dbapi implementations e.g, pymysql and mysql
211-
def uninstrument_connection(self, connection):
212-
connection.cursor_factory = self._otel_cursor_factories.pop(
213-
connection, None
214-
)
203+
@staticmethod
204+
def uninstrument_connection(connection):
205+
# TODO Add check for attempt to instrument a connection when already instrumented
206+
# https://github.com/open-telemetry/opentelemetry-python-contrib/issues/3138
215207
return connection
216208

217209

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

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,7 @@ def test_instrument_connection(self):
193193
self.assertEqual(len(spans_list), 0)
194194

195195
instrumentor = Psycopg2Instrumentor()
196-
original_cursor_factory = cnx.cursor_factory
197196
cnx = instrumentor.instrument_connection(cnx)
198-
self.assertEqual(
199-
instrumentor._otel_cursor_factories[cnx], original_cursor_factory
200-
)
201197
cursor = cnx.cursor()
202198
cursor.execute(query)
203199

@@ -218,17 +214,8 @@ def test_instrument_connection_with_instrument(self):
218214
instrumentor.instrument()
219215

220216
cnx = psycopg2.connect(database="test")
221-
original_cursor_factory = cnx.cursor_factory
222-
orig_cnx = cnx
223217
cnx = Psycopg2Instrumentor().instrument_connection(cnx)
224218

225-
# TODO Should not set cursor_factory if already instrumented
226-
# https://github.com/open-telemetry/opentelemetry-python-contrib/issues/3138
227-
# self.assertEqual(instrumentor._otel_cursor_factories[cnx], None)
228-
self.assertEqual(
229-
instrumentor._otel_cursor_factories.get(orig_cnx),
230-
original_cursor_factory,
231-
)
232219
cursor = cnx.cursor()
233220
cursor.execute(query)
234221

@@ -248,23 +235,11 @@ def test_instrument_connection_with_instrument_connection(self):
248235
self.assertEqual(len(spans_list), 0)
249236

250237
cnx = psycopg2.connect(database="test")
251-
original_cursor_factory = cnx.cursor_factory
252238
instrumentor = Psycopg2Instrumentor()
253-
orig_cnx = cnx
254239
cnx = instrumentor.instrument_connection(cnx)
255-
self.assertEqual(
256-
instrumentor._otel_cursor_factories.get(orig_cnx),
257-
original_cursor_factory,
258-
)
259240

260-
orig_cnx = cnx
261-
original_cursor_factory = cnx.cursor_factory
262241
instrumentor = Psycopg2Instrumentor()
263242
cnx = instrumentor.instrument_connection(cnx)
264-
self.assertEqual(
265-
instrumentor._otel_cursor_factories.get(orig_cnx),
266-
original_cursor_factory,
267-
)
268243
cursor = cnx.cursor()
269244
cursor.execute(query)
270245

@@ -290,13 +265,14 @@ def test_uninstrument_connection_with_instrument(self):
290265
cursor = cnx.cursor()
291266
cursor.execute(query)
292267

293-
spans_list = self.memory_exporter.get_finished_spans()
294-
self.assertEqual(len(spans_list), 1)
268+
# TODO Add check for attempt to instrument a connection when already instrumented
269+
# https://github.com/open-telemetry/opentelemetry-python-contrib/issues/3138
270+
# spans_list = self.memory_exporter.get_finished_spans()
271+
# self.assertEqual(len(spans_list), 1)
295272

296273
# pylint: disable=unused-argument
297274
def test_uninstrument_connection_with_instrument_connection(self):
298275
cnx = psycopg2.connect(database="test")
299-
original_cursor_factory = cnx.cursor_factory
300276
instrumentor = Psycopg2Instrumentor()
301277
instrumentor.instrument_connection(cnx)
302278
query = "SELECT * FROM test"
@@ -305,20 +281,15 @@ def test_uninstrument_connection_with_instrument_connection(self):
305281

306282
spans_list = self.memory_exporter.get_finished_spans()
307283
self.assertEqual(len(spans_list), 1)
308-
self.assertEqual(
309-
instrumentor._otel_cursor_factories[cnx], original_cursor_factory
310-
)
311284

312-
orig_cnx = cnx
313285
cnx = Psycopg2Instrumentor().uninstrument_connection(cnx)
314286
cursor = cnx.cursor()
315287
cursor.execute(query)
316288

317-
spans_list = self.memory_exporter.get_finished_spans()
318-
self.assertEqual(len(spans_list), 1)
319-
self.assertEqual(
320-
instrumentor._otel_cursor_factories.get(orig_cnx), None
321-
)
289+
# TODO Add check for attempt to instrument a connection when already instrumented
290+
# https://github.com/open-telemetry/opentelemetry-python-contrib/issues/3138
291+
# spans_list = self.memory_exporter.get_finished_spans()
292+
# self.assertEqual(len(spans_list), 1)
322293

323294
@mock.patch("opentelemetry.instrumentation.dbapi.wrap_connect")
324295
def test_sqlcommenter_enabled(self, event_mocked):

0 commit comments

Comments
 (0)