From bd6be9cef94816995dcdd82cfd44416f4cebccaa Mon Sep 17 00:00:00 2001 From: tammy-baylis-swi Date: Tue, 19 Nov 2024 17:39:43 -0800 Subject: [PATCH 1/4] DbApi instrument_connection accepts optional connect_module --- .../src/opentelemetry/instrumentation/dbapi/__init__.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py b/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py index fc3911f744..992068d570 100644 --- a/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py @@ -190,6 +190,7 @@ def instrument_connection( capture_parameters: bool = False, enable_commenter: bool = False, commenter_options: dict = None, + connect_module: typing.Callable[..., typing.Any] = None, ): """Enable instrumentation in a database connection. @@ -204,6 +205,7 @@ def instrument_connection( capture_parameters: Configure if db.statement.parameters should be captured. enable_commenter: Flag to enable/disable sqlcommenter. commenter_options: Configurations for tags to be appended at the sql query. + connect_module: Module name where connect method is available. Returns: An instrumented connection. @@ -221,6 +223,7 @@ def instrument_connection( capture_parameters=capture_parameters, enable_commenter=enable_commenter, commenter_options=commenter_options, + connect_module=connect_module, ) db_integration.get_connection_attributes(connection) return get_traced_connection_proxy(connection, db_integration) From 388ccf0018845b87b7af634605796c1ffbef72a9 Mon Sep 17 00:00:00 2001 From: tammy-baylis-swi Date: Tue, 19 Nov 2024 17:43:17 -0800 Subject: [PATCH 2/4] Changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 92e366402a..61395bff98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#2937](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2937)) - `opentelemetry-instrumentation-dbapi` Add sqlcomment to `db.statement` attribute ([#2935](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2935)) +- `opentelemetry-instrumentation-dbapi` instrument_connection accepts optional connect_module + ([#3027](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3027)) ### Fixed From 916bf5e883cb296b9d3a9378a69cdc07bf3d753e Mon Sep 17 00:00:00 2001 From: tammy-baylis-swi Date: Wed, 20 Nov 2024 09:39:36 -0800 Subject: [PATCH 3/4] Add test --- .../tests/test_dbapi_integration.py | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py b/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py index ae595fb430..1236750ec7 100644 --- a/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py +++ b/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py @@ -592,6 +592,49 @@ def test_instrument_connection(self): connection2 = dbapi.instrument_connection(self.tracer, connection, "-") self.assertIs(connection2.__wrapped__, connection) + @mock.patch("opentelemetry.instrumentation.dbapi.DatabaseApiIntegration") + def test_instrument_connection_kwargs_defaults(self, mock_dbapiint): + connection = mock.Mock() + # Avoid get_attributes failing because can't concatenate mock + connection.database = "-" + dbapi.instrument_connection(self.tracer, connection, "-") + kwargs = mock_dbapiint.call_args[1] + self.assertEqual(kwargs["connection_attributes"], None) + self.assertEqual(kwargs["version"], "") + self.assertEqual(kwargs["tracer_provider"], None) + self.assertEqual(kwargs["capture_parameters"], False) + self.assertEqual(kwargs["enable_commenter"], False) + self.assertEqual(kwargs["commenter_options"], None) + self.assertEqual(kwargs["connect_module"], None) + + @mock.patch("opentelemetry.instrumentation.dbapi.DatabaseApiIntegration") + def test_instrument_connection_kwargs_provided(self, mock_dbapiint): + mock_tracer_provider = mock.MagicMock() + mock_connect_module = mock.MagicMock() + connection = mock.Mock() + # Avoid get_attributes failing because can't concatenate mock + connection.database = "-" + dbapi.instrument_connection( + self.tracer, + connection, + "-", + connection_attributes={"foo": "bar"}, + version="test", + tracer_provider=mock_tracer_provider, + capture_parameters=True, + enable_commenter=True, + commenter_options={"foo": "bar"}, + connect_module=mock_connect_module, + ) + kwargs = mock_dbapiint.call_args[1] + self.assertEqual(kwargs["connection_attributes"], {"foo": "bar"}) + self.assertEqual(kwargs["version"], "test") + self.assertEqual(kwargs["tracer_provider"], mock_tracer_provider) + self.assertEqual(kwargs["capture_parameters"], True) + self.assertEqual(kwargs["enable_commenter"], True) + self.assertEqual(kwargs["commenter_options"], {"foo": "bar"}) + self.assertEqual(kwargs["connect_module"], mock_connect_module) + def test_uninstrument_connection(self): connection = mock.Mock() # Set connection.database to avoid a failure because mock can't From ce86c1e3a1080ed574e136fe5cd525af072cc12b Mon Sep 17 00:00:00 2001 From: tammy-baylis-swi Date: Wed, 20 Nov 2024 11:37:28 -0800 Subject: [PATCH 4/4] Adjust tests --- .../tests/test_dbapi_integration.py | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py b/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py index 1236750ec7..2ffa2f3d5b 100644 --- a/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py +++ b/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py @@ -594,10 +594,7 @@ def test_instrument_connection(self): @mock.patch("opentelemetry.instrumentation.dbapi.DatabaseApiIntegration") def test_instrument_connection_kwargs_defaults(self, mock_dbapiint): - connection = mock.Mock() - # Avoid get_attributes failing because can't concatenate mock - connection.database = "-" - dbapi.instrument_connection(self.tracer, connection, "-") + dbapi.instrument_connection(self.tracer, mock.Mock(), "foo") kwargs = mock_dbapiint.call_args[1] self.assertEqual(kwargs["connection_attributes"], None) self.assertEqual(kwargs["version"], "") @@ -611,13 +608,10 @@ def test_instrument_connection_kwargs_defaults(self, mock_dbapiint): def test_instrument_connection_kwargs_provided(self, mock_dbapiint): mock_tracer_provider = mock.MagicMock() mock_connect_module = mock.MagicMock() - connection = mock.Mock() - # Avoid get_attributes failing because can't concatenate mock - connection.database = "-" dbapi.instrument_connection( self.tracer, - connection, - "-", + mock.Mock(), + "foo", connection_attributes={"foo": "bar"}, version="test", tracer_provider=mock_tracer_provider, @@ -629,11 +623,11 @@ def test_instrument_connection_kwargs_provided(self, mock_dbapiint): kwargs = mock_dbapiint.call_args[1] self.assertEqual(kwargs["connection_attributes"], {"foo": "bar"}) self.assertEqual(kwargs["version"], "test") - self.assertEqual(kwargs["tracer_provider"], mock_tracer_provider) + self.assertIs(kwargs["tracer_provider"], mock_tracer_provider) self.assertEqual(kwargs["capture_parameters"], True) self.assertEqual(kwargs["enable_commenter"], True) self.assertEqual(kwargs["commenter_options"], {"foo": "bar"}) - self.assertEqual(kwargs["connect_module"], mock_connect_module) + self.assertIs(kwargs["connect_module"], mock_connect_module) def test_uninstrument_connection(self): connection = mock.Mock()