From d2da8cda4e1c9f12f8b9ca787f0817abc30fa6d8 Mon Sep 17 00:00:00 2001 From: tammy-baylis-swi Date: Thu, 5 Dec 2024 15:27:18 -0800 Subject: [PATCH 01/11] enable_commenter for instrument_connection psycopg(2) --- .../instrumentation/psycopg/__init__.py | 23 ++++++++++++++++--- .../instrumentation/psycopg2/__init__.py | 23 ++++++++++++++++--- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py b/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py index e986ec0d46..50913e2703 100644 --- a/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py @@ -114,6 +114,7 @@ ) from psycopg.sql import Composed # pylint: disable=no-name-in-module +from opentelemetry import trace as trace_api from opentelemetry.instrumentation import dbapi from opentelemetry.instrumentation.instrumentor import BaseInstrumentor from opentelemetry.instrumentation.psycopg.package import _instruments @@ -195,7 +196,12 @@ def _uninstrument(self, **kwargs): # TODO(owais): check if core dbapi can do this for all dbapi implementations e.g, pymysql and mysql @staticmethod - def instrument_connection(connection, tracer_provider=None): + def instrument_connection( + connection, + tracer_provider=None, + enable_commenter: bool = False, + commenter_options: dict = None, + ): if not hasattr(connection, "_is_instrumented_by_opentelemetry"): connection._is_instrumented_by_opentelemetry = False @@ -204,7 +210,9 @@ def instrument_connection(connection, tracer_provider=None): connection, _OTEL_CURSOR_FACTORY_KEY, connection.cursor_factory ) connection.cursor_factory = _new_cursor_factory( - tracer_provider=tracer_provider + tracer_provider=tracer_provider, + enable_commenter=enable_commenter, + commenter_options=commenter_options, ) connection._is_instrumented_by_opentelemetry = True else: @@ -288,7 +296,13 @@ def get_statement(self, cursor, args): return statement -def _new_cursor_factory(db_api=None, base_factory=None, tracer_provider=None): +def _new_cursor_factory( + db_api: dbapi.DatabaseApiIntegration = None, + base_factory: pg_cursor = None, + tracer_provider: typing.Optional[trace_api.TracerProvider] = None, + enable_commenter: bool = False, + commenter_options: dict = None, +): if not db_api: db_api = DatabaseApiIntegration( __name__, @@ -296,6 +310,9 @@ def _new_cursor_factory(db_api=None, base_factory=None, tracer_provider=None): connection_attributes=PsycopgInstrumentor._CONNECTION_ATTRIBUTES, version=__version__, tracer_provider=tracer_provider, + enable_commenter=enable_commenter, + commenter_options=commenter_options, + connect_module=psycopg, ) base_factory = base_factory or pg_cursor diff --git a/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/__init__.py b/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/__init__.py index de2e49f4c3..600616a0b7 100644 --- a/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/__init__.py @@ -111,6 +111,7 @@ ) from psycopg2.sql import Composed # pylint: disable=no-name-in-module +from opentelemetry import trace as trace_api from opentelemetry.instrumentation import dbapi from opentelemetry.instrumentation.instrumentor import BaseInstrumentor from opentelemetry.instrumentation.psycopg2.package import _instruments @@ -159,7 +160,12 @@ def _uninstrument(self, **kwargs): # TODO(owais): check if core dbapi can do this for all dbapi implementations e.g, pymysql and mysql @staticmethod - def instrument_connection(connection, tracer_provider=None): + def instrument_connection( + connection, + tracer_provider=None, + enable_commenter: bool = False, + commenter_options: dict = None, + ): if not hasattr(connection, "_is_instrumented_by_opentelemetry"): connection._is_instrumented_by_opentelemetry = False @@ -168,7 +174,9 @@ def instrument_connection(connection, tracer_provider=None): connection, _OTEL_CURSOR_FACTORY_KEY, connection.cursor_factory ) connection.cursor_factory = _new_cursor_factory( - tracer_provider=tracer_provider + tracer_provider=tracer_provider, + enable_commenter=enable_commenter, + commenter_options=commenter_options, ) connection._is_instrumented_by_opentelemetry = True else: @@ -231,7 +239,13 @@ def get_statement(self, cursor, args): return statement -def _new_cursor_factory(db_api=None, base_factory=None, tracer_provider=None): +def _new_cursor_factory( + db_api: dbapi.DatabaseApiIntegration = None, + base_factory: pg_cursor = None, + tracer_provider: typing.Optional[trace_api.TracerProvider] = None, + enable_commenter: bool = False, + commenter_options: dict = None, +): if not db_api: db_api = DatabaseApiIntegration( __name__, @@ -239,6 +253,9 @@ def _new_cursor_factory(db_api=None, base_factory=None, tracer_provider=None): connection_attributes=Psycopg2Instrumentor._CONNECTION_ATTRIBUTES, version=__version__, tracer_provider=tracer_provider, + enable_commenter=enable_commenter, + commenter_options=commenter_options, + connect_module=psycopg2, ) base_factory = base_factory or pg_cursor From 4783f3afefa72d122f86a774705331d5f9fac75d Mon Sep 17 00:00:00 2001 From: tammy-baylis-swi Date: Thu, 5 Dec 2024 15:32:04 -0800 Subject: [PATCH 02/11] Changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 33f83bb0ae..0bd0435380 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#2942](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2942)) - `opentelemetry-instrumentation-click`: new instrumentation to trace click commands ([#2994](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2994)) +- `opentelemetry-instrumentation-psycopg2`, `opentelemetry-instrumentation-psycopg`: Add sqlcommenter support for `instrument_connection` + ([#3071](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3071)) ### Fixed From bf49c8c0e5d2d88e09af1daf0bc79d1f7a91a4a6 Mon Sep 17 00:00:00 2001 From: tammy-baylis-swi Date: Fri, 6 Dec 2024 17:27:55 -0800 Subject: [PATCH 03/11] Add psycopg2 tests --- .../tests/test_psycopg2_integration.py | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/instrumentation/opentelemetry-instrumentation-psycopg2/tests/test_psycopg2_integration.py b/instrumentation/opentelemetry-instrumentation-psycopg2/tests/test_psycopg2_integration.py index 9a6a5ff2fa..6d38b1c34c 100644 --- a/instrumentation/opentelemetry-instrumentation-psycopg2/tests/test_psycopg2_integration.py +++ b/instrumentation/opentelemetry-instrumentation-psycopg2/tests/test_psycopg2_integration.py @@ -261,6 +261,74 @@ def test_sqlcommenter_enabled(self, event_mocked): kwargs = event_mocked.call_args[1] self.assertEqual(kwargs["enable_commenter"], True) + def test_sqlcommenter_enabled_instrument_connection_defaults(self): + with mock.patch( + "opentelemetry.instrumentation.psycopg2.psycopg2.__version__", + "foobar", + ), mock.patch( + "opentelemetry.instrumentation.psycopg2.psycopg2.__libpq_version__", + "foobaz", + ), mock.patch( + "opentelemetry.instrumentation.psycopg2.psycopg2.threadsafety", + "123", + ), mock.patch( + "opentelemetry.instrumentation.psycopg2.psycopg2.apilevel", + "123", + ), mock.patch( + "opentelemetry.instrumentation.psycopg2.psycopg2.paramstyle", + "test", + ): + cnx = psycopg2.connect(database="test") + cnx = Psycopg2Instrumentor().instrument_connection( + cnx, + enable_commenter=True, + ) + query = "Select 1" + cursor = cnx.cursor() + cursor.execute(query) + spans_list = self.memory_exporter.get_finished_spans() + span = spans_list[0] + span_id = format(span.get_span_context().span_id, "016x") + trace_id = format(span.get_span_context().trace_id, "032x") + self.assertEqual( + MockCursor.execute.call_args[0][0], + 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'*/", + ) + + def test_sqlcommenter_enabled_instrument_connection_with_options(self): + with mock.patch( + "opentelemetry.instrumentation.psycopg2.psycopg2.__version__", + "foobar", + ), mock.patch( + "opentelemetry.instrumentation.psycopg2.psycopg2.__libpq_version__", + "foobaz", + ), mock.patch( + "opentelemetry.instrumentation.psycopg2.psycopg2.threadsafety", + "123", + ): + cnx = psycopg2.connect(database="test") + cnx = Psycopg2Instrumentor().instrument_connection( + cnx, + enable_commenter=True, + commenter_options={ + "dbapi_level": False, + "dbapi_threadsafety": True, + "driver_paramstyle": False, + "foo": "ignored", + }, + ) + query = "Select 1" + cursor = cnx.cursor() + cursor.execute(query) + spans_list = self.memory_exporter.get_finished_spans() + span = spans_list[0] + span_id = format(span.get_span_context().span_id, "016x") + trace_id = format(span.get_span_context().trace_id, "032x") + self.assertEqual( + MockCursor.execute.call_args[0][0], + f"Select 1 /*db_driver='psycopg2%%3Afoobar',dbapi_threadsafety='123',libpq_version='foobaz',traceparent='00-{trace_id}-{span_id}-01'*/", + ) + @mock.patch("opentelemetry.instrumentation.dbapi.wrap_connect") def test_sqlcommenter_disabled(self, event_mocked): cnx = psycopg2.connect(database="test") @@ -271,6 +339,33 @@ def test_sqlcommenter_disabled(self, event_mocked): kwargs = event_mocked.call_args[1] self.assertEqual(kwargs["enable_commenter"], False) + def test_sqlcommenter_disabled_default_instrument_connection(self): + cnx = psycopg2.connect(database="test") + cnx = Psycopg2Instrumentor().instrument_connection( + cnx, + ) + query = "Select 1" + cursor = cnx.cursor() + cursor.execute(query) + self.assertEqual( + MockCursor.execute.call_args[0][0], + "Select 1", + ) + + def test_sqlcommenter_disabled_explicit_instrument_connection(self): + cnx = psycopg2.connect(database="test") + cnx = Psycopg2Instrumentor().instrument_connection( + cnx, + enable_commenter=False, + ) + query = "Select 1" + cursor = cnx.cursor() + cursor.execute(query) + self.assertEqual( + MockCursor.execute.call_args[0][0], + "Select 1", + ) + def test_no_op_tracer_provider(self): Psycopg2Instrumentor().instrument( tracer_provider=trace.NoOpTracerProvider() From e148c9fe721765b8422fdcf400a26f003f95dfaf Mon Sep 17 00:00:00 2001 From: tammy-baylis-swi Date: Fri, 6 Dec 2024 17:42:14 -0800 Subject: [PATCH 04/11] Add psycopg tests --- .../tests/test_psycopg_integration.py | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/instrumentation/opentelemetry-instrumentation-psycopg/tests/test_psycopg_integration.py b/instrumentation/opentelemetry-instrumentation-psycopg/tests/test_psycopg_integration.py index 4ddaad9174..4081070aeb 100644 --- a/instrumentation/opentelemetry-instrumentation-psycopg/tests/test_psycopg_integration.py +++ b/instrumentation/opentelemetry-instrumentation-psycopg/tests/test_psycopg_integration.py @@ -378,6 +378,74 @@ def test_sqlcommenter_enabled(self, event_mocked): kwargs = event_mocked.call_args[1] self.assertEqual(kwargs["enable_commenter"], True) + def test_sqlcommenter_enabled_instrument_connection_defaults(self): + with mock.patch( + "opentelemetry.instrumentation.psycopg.psycopg.__version__", + "foobar", + ), mock.patch( + "opentelemetry.instrumentation.psycopg.psycopg.pq.__build_version__", + "foobaz", + ), mock.patch( + "opentelemetry.instrumentation.psycopg.psycopg.threadsafety", + "123", + ), mock.patch( + "opentelemetry.instrumentation.psycopg.psycopg.apilevel", + "123", + ), mock.patch( + "opentelemetry.instrumentation.psycopg.psycopg.paramstyle", + "test", + ): + cnx = psycopg.connect(database="test") + cnx = PsycopgInstrumentor().instrument_connection( + cnx, + enable_commenter=True, + ) + query = "Select 1" + cursor = cnx.cursor() + cursor.execute(query) + spans_list = self.memory_exporter.get_finished_spans() + span = spans_list[0] + span_id = format(span.get_span_context().span_id, "016x") + trace_id = format(span.get_span_context().trace_id, "032x") + self.assertEqual( + MockCursor.execute.call_args[0][0], + 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'*/", + ) + + def test_sqlcommenter_enabled_instrument_connection_with_options(self): + with mock.patch( + "opentelemetry.instrumentation.psycopg.psycopg.__version__", + "foobar", + ), mock.patch( + "opentelemetry.instrumentation.psycopg.psycopg.pq.__build_version__", + "foobaz", + ), mock.patch( + "opentelemetry.instrumentation.psycopg.psycopg.threadsafety", + "123", + ): + cnx = psycopg.connect(database="test") + cnx = PsycopgInstrumentor().instrument_connection( + cnx, + enable_commenter=True, + commenter_options={ + "dbapi_level": False, + "dbapi_threadsafety": True, + "driver_paramstyle": False, + "foo": "ignored", + }, + ) + query = "Select 1" + cursor = cnx.cursor() + cursor.execute(query) + spans_list = self.memory_exporter.get_finished_spans() + span = spans_list[0] + span_id = format(span.get_span_context().span_id, "016x") + trace_id = format(span.get_span_context().trace_id, "032x") + self.assertEqual( + MockCursor.execute.call_args[0][0], + f"Select 1 /*db_driver='psycopg%%3Afoobar',dbapi_threadsafety='123',libpq_version='foobaz',traceparent='00-{trace_id}-{span_id}-01'*/", + ) + @mock.patch("opentelemetry.instrumentation.dbapi.wrap_connect") def test_sqlcommenter_disabled(self, event_mocked): cnx = psycopg.connect(database="test") @@ -388,6 +456,33 @@ def test_sqlcommenter_disabled(self, event_mocked): kwargs = event_mocked.call_args[1] self.assertEqual(kwargs["enable_commenter"], False) + def test_sqlcommenter_disabled_default_instrument_connection(self): + cnx = psycopg.connect(database="test") + cnx = PsycopgInstrumentor().instrument_connection( + cnx, + ) + query = "Select 1" + cursor = cnx.cursor() + cursor.execute(query) + self.assertEqual( + MockCursor.execute.call_args[0][0], + "Select 1", + ) + + def test_sqlcommenter_disabled_explicit_instrument_connection(self): + cnx = psycopg.connect(database="test") + cnx = PsycopgInstrumentor().instrument_connection( + cnx, + enable_commenter=False, + ) + query = "Select 1" + cursor = cnx.cursor() + cursor.execute(query) + self.assertEqual( + MockCursor.execute.call_args[0][0], + "Select 1", + ) + class TestPostgresqlIntegrationAsync( PostgresqlIntegrationTestMixin, TestBase, IsolatedAsyncioTestCase From 58daeb60efdc170932fb97ea14e3f91c90531112 Mon Sep 17 00:00:00 2001 From: tammy-baylis-swi Date: Fri, 6 Dec 2024 18:16:20 -0800 Subject: [PATCH 05/11] Update typehints --- .../src/opentelemetry/instrumentation/psycopg/__init__.py | 7 +++++-- .../src/opentelemetry/instrumentation/psycopg2/__init__.py | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py b/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py index 50913e2703..88ae8be0d3 100644 --- a/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py @@ -109,6 +109,9 @@ from psycopg import ( AsyncCursor as pg_async_cursor, # pylint: disable=import-self,no-name-in-module ) +from psycopg import ( + Connection as pg_connection, # pylint: disable=no-name-in-module +) from psycopg import ( Cursor as pg_cursor, # pylint: disable=no-name-in-module,import-self ) @@ -197,8 +200,8 @@ def _uninstrument(self, **kwargs): # TODO(owais): check if core dbapi can do this for all dbapi implementations e.g, pymysql and mysql @staticmethod def instrument_connection( - connection, - tracer_provider=None, + connection: pg_connection, + tracer_provider: typing.Optional[trace_api.TracerProvider] = None, enable_commenter: bool = False, commenter_options: dict = None, ): diff --git a/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/__init__.py b/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/__init__.py index 600616a0b7..9db5700256 100644 --- a/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/__init__.py @@ -106,6 +106,9 @@ from typing import Collection import psycopg2 +from psycopg2.extensions import ( + connection as pg_connection, # pylint: disable=no-name-in-module +) from psycopg2.extensions import ( cursor as pg_cursor, # pylint: disable=no-name-in-module ) @@ -161,8 +164,8 @@ def _uninstrument(self, **kwargs): # TODO(owais): check if core dbapi can do this for all dbapi implementations e.g, pymysql and mysql @staticmethod def instrument_connection( - connection, - tracer_provider=None, + connection: pg_connection, + tracer_provider: typing.Optional[trace_api.TracerProvider] = None, enable_commenter: bool = False, commenter_options: dict = None, ): From 8e8b7597dfc0fb9a6e70333f52c8e5361fa48967 Mon Sep 17 00:00:00 2001 From: tammy-baylis-swi Date: Fri, 6 Dec 2024 18:22:29 -0800 Subject: [PATCH 06/11] Document psycopg(2) instrument_connection --- .../instrumentation/psycopg/__init__.py | 12 ++++++++++++ .../instrumentation/psycopg2/__init__.py | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py b/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py index 88ae8be0d3..ad8632b88a 100644 --- a/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py @@ -205,6 +205,18 @@ def instrument_connection( enable_commenter: bool = False, commenter_options: dict = None, ): + """Enable instrumentation of a Psycopg connection. + + Args: + connection: The connection to instrument. + tracer_provider: Optional tracer provider to use. If omitted + the current globally configured one is used. + enable_commenter: Optional flag to enable/disable sqlcommenter (default disabled). + commenter_options: Optional configurations for tags to be appended at the sql query. + + Returns: + An instrumented connection. + """ if not hasattr(connection, "_is_instrumented_by_opentelemetry"): connection._is_instrumented_by_opentelemetry = False diff --git a/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/__init__.py b/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/__init__.py index 9db5700256..9235a994bf 100644 --- a/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/__init__.py @@ -169,6 +169,18 @@ def instrument_connection( enable_commenter: bool = False, commenter_options: dict = None, ): + """Enable instrumentation of a Psycopg2 connection. + + Args: + connection: The connection to instrument. + tracer_provider: Optional tracer provider to use. If omitted + the current globally configured one is used. + enable_commenter: Optional flag to enable/disable sqlcommenter (default disabled). + commenter_options: Optional configurations for tags to be appended at the sql query. + + Returns: + An instrumented connection. + """ if not hasattr(connection, "_is_instrumented_by_opentelemetry"): connection._is_instrumented_by_opentelemetry = False From e3d7941bfaa8cad7c9e5bcec13c681c84bc49e60 Mon Sep 17 00:00:00 2001 From: tammy-baylis-swi Date: Tue, 10 Dec 2024 16:28:36 -0800 Subject: [PATCH 07/11] Consistent pg_connection alias usage --- .../src/opentelemetry/instrumentation/psycopg/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py b/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py index ad8632b88a..984decd986 100644 --- a/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py @@ -162,7 +162,7 @@ def _instrument(self, **kwargs): dbapi.wrap_connect( __name__, - psycopg.Connection, # pylint: disable=no-member + pg_connection, # pylint: disable=no-member "connect", self._DATABASE_SYSTEM, self._CONNECTION_ATTRIBUTES, @@ -189,7 +189,7 @@ def _uninstrument(self, **kwargs): """ "Disable Psycopg instrumentation""" dbapi.unwrap_connect(psycopg, "connect") # pylint: disable=no-member dbapi.unwrap_connect( - psycopg.Connection, + pg_connection, "connect", # pylint: disable=no-member ) dbapi.unwrap_connect( From a94a59121d8af86c602fc26f7ce90a0bb1a1d4ab Mon Sep 17 00:00:00 2001 From: tammy-baylis-swi Date: Tue, 10 Dec 2024 16:32:15 -0800 Subject: [PATCH 08/11] Revert "Consistent pg_connection alias usage" This reverts commit e3d7941bfaa8cad7c9e5bcec13c681c84bc49e60. --- .../src/opentelemetry/instrumentation/psycopg/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py b/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py index 984decd986..ad8632b88a 100644 --- a/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py @@ -162,7 +162,7 @@ def _instrument(self, **kwargs): dbapi.wrap_connect( __name__, - pg_connection, # pylint: disable=no-member + psycopg.Connection, # pylint: disable=no-member "connect", self._DATABASE_SYSTEM, self._CONNECTION_ATTRIBUTES, @@ -189,7 +189,7 @@ def _uninstrument(self, **kwargs): """ "Disable Psycopg instrumentation""" dbapi.unwrap_connect(psycopg, "connect") # pylint: disable=no-member dbapi.unwrap_connect( - pg_connection, + psycopg.Connection, "connect", # pylint: disable=no-member ) dbapi.unwrap_connect( From 0e24d6d94015503f9df9bea6a9a185291f77b96b Mon Sep 17 00:00:00 2001 From: tammy-baylis-swi Date: Tue, 10 Dec 2024 16:35:21 -0800 Subject: [PATCH 09/11] Use qualified import not alias --- .../src/opentelemetry/instrumentation/psycopg/__init__.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py b/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py index ad8632b88a..af66aa6d02 100644 --- a/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py @@ -109,9 +109,6 @@ from psycopg import ( AsyncCursor as pg_async_cursor, # pylint: disable=import-self,no-name-in-module ) -from psycopg import ( - Connection as pg_connection, # pylint: disable=no-name-in-module -) from psycopg import ( Cursor as pg_cursor, # pylint: disable=no-name-in-module,import-self ) @@ -200,7 +197,7 @@ def _uninstrument(self, **kwargs): # TODO(owais): check if core dbapi can do this for all dbapi implementations e.g, pymysql and mysql @staticmethod def instrument_connection( - connection: pg_connection, + connection: psycopg.Connection, tracer_provider: typing.Optional[trace_api.TracerProvider] = None, enable_commenter: bool = False, commenter_options: dict = None, From da529bdbb8c37dbab833ea47359ecb3002f348e4 Mon Sep 17 00:00:00 2001 From: tammy-baylis-swi Date: Tue, 10 Dec 2024 17:28:11 -0800 Subject: [PATCH 10/11] Fix psycopg2/3 docs --- docs-requirements.txt | 1 + docs/conf.py | 2 ++ .../src/opentelemetry/instrumentation/psycopg/__init__.py | 4 ++-- .../opentelemetry/instrumentation/psycopg2/__init__.py | 8 ++++---- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/docs-requirements.txt b/docs-requirements.txt index d547e806a3..9274aeef8d 100644 --- a/docs-requirements.txt +++ b/docs-requirements.txt @@ -32,6 +32,7 @@ mysqlclient~=2.1.1 openai >= 1.26.0 psutil>=5 psycopg~=3.1.17 +psycopg2~=2.9.9 pika>=0.12.0 pymongo~=4.6.3 PyMySQL~=1.1.1 diff --git a/docs/conf.py b/docs/conf.py index 8233fccb15..3dca3d05f5 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -122,6 +122,8 @@ "https://opentelemetry-python.readthedocs.io/en/latest/", None, ), + "psycopg": ("https://www.psycopg.org/psycopg3/docs/", None), + "psycopg2": ("https://www.psycopg.org/docs/", None), } # http://www.sphinx-doc.org/en/master/config.html#confval-nitpicky diff --git a/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py b/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py index af66aa6d02..35c0720173 100644 --- a/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py @@ -16,7 +16,7 @@ The integration with PostgreSQL supports the `Psycopg`_ library, it can be enabled by using ``PsycopgInstrumentor``. -.. _Psycopg: http://initd.org/psycopg/ +.. _Psycopg: https://www.psycopg.org/psycopg3/docs/ SQLCOMMENTER ***************************************** @@ -139,7 +139,7 @@ def instrumentation_dependencies(self) -> Collection[str]: def _instrument(self, **kwargs): """Integrate with PostgreSQL Psycopg library. - Psycopg: http://initd.org/psycopg/ + Psycopg: https://www.psycopg.org/psycopg3/docs/ """ tracer_provider = kwargs.get("tracer_provider") enable_sqlcommenter = kwargs.get("enable_commenter", False) diff --git a/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/__init__.py b/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/__init__.py index 9235a994bf..e8bd63e4ab 100644 --- a/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/__init__.py @@ -13,10 +13,10 @@ # limitations under the License. """ -The integration with PostgreSQL supports the `Psycopg`_ library, it can be enabled by +The integration with PostgreSQL supports the `Psycopg2`_ library, it can be enabled by using ``Psycopg2Instrumentor``. -.. _Psycopg: http://initd.org/psycopg/ +.. _Psycopg2: https://www.psycopg.org/docs/ SQLCOMMENTER ***************************************** @@ -138,8 +138,8 @@ def instrumentation_dependencies(self) -> Collection[str]: return _instruments def _instrument(self, **kwargs): - """Integrate with PostgreSQL Psycopg library. - Psycopg: http://initd.org/psycopg/ + """Integrate with PostgreSQL Psycopg2 library. + Psycopg2: https://www.psycopg.org/docs/ """ tracer_provider = kwargs.get("tracer_provider") enable_sqlcommenter = kwargs.get("enable_commenter", False) From 84d82d03c56f2691e02609e4a8e9111997561e01 Mon Sep 17 00:00:00 2001 From: tammy-baylis-swi Date: Thu, 2 Jan 2025 10:34:32 -0800 Subject: [PATCH 11/11] Fix --- .../src/opentelemetry/instrumentation/psycopg2/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/__init__.py b/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/__init__.py index 262f639295..341ddb1b7d 100644 --- a/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/__init__.py @@ -178,6 +178,7 @@ def _uninstrument(self, **kwargs): dbapi.unwrap_connect(psycopg2, "connect") # TODO(owais): check if core dbapi can do this for all dbapi implementations e.g, pymysql and mysql + @staticmethod def instrument_connection( connection: pg_connection, tracer_provider: typing.Optional[trace_api.TracerProvider] = None,