From 6d8becf9ade7cd9a50090d01c7478fe5beff626d Mon Sep 17 00:00:00 2001 From: Naofumi MURATA Date: Sat, 31 May 2025 00:49:29 +0900 Subject: [PATCH] feat: respect supress_instrumentation functionality in dbapi instrumentation (#3460) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * respect supress_instrumentation * update CHANGELOG * fix link * update CHANGELOG --------- Co-authored-by: Emídio Neto <9735060+emdneto@users.noreply.github.com> --- CHANGELOG.md | 1 + .../instrumentation/dbapi/__init__.py | 4 ++++ .../tests/test_dbapi_integration.py | 16 ++++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 876c7cd3c..52c5eed71 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#3544](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3544)) - `opentelemetry-instrumentation-botocore` Add type check when extracting tool use from Bedrock request message content ([#3548](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3548)) +- `opentelemetry-instrumentation-dbapi` Respect suppress_instrumentation functionality ([#3460](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3460)) ### Breaking changes 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 f87401ff3..0d1a228be 100644 --- a/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py @@ -52,6 +52,7 @@ from opentelemetry.instrumentation.dbapi.version import __version__ from opentelemetry.instrumentation.sqlcommenter_utils import _add_sql_comment from opentelemetry.instrumentation.utils import ( _get_opentelemetry_values, + is_instrumentation_enabled, unwrap, ) from opentelemetry.semconv.trace import SpanAttributes @@ -561,6 +562,9 @@ class CursorTracer(Generic[CursorT]): *args: tuple[Any, ...], **kwargs: dict[Any, Any], ): + if not is_instrumentation_enabled(): + return query_method(*args, **kwargs) + name = self.get_operation_name(cursor, args) if not name: name = ( diff --git a/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py b/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py index 3f30f3a89..9a7a4a7d1 100644 --- a/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py +++ b/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py @@ -21,6 +21,7 @@ from unittest import mock from opentelemetry import context from opentelemetry import trace as trace_api from opentelemetry.instrumentation import dbapi +from opentelemetry.instrumentation.utils import suppress_instrumentation from opentelemetry.sdk import resources from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.test.test_base import TestBase @@ -243,6 +244,21 @@ class TestDBApiIntegration(TestBase): spans_list = self.memory_exporter.get_finished_spans() self.assertEqual(len(spans_list), 0) + def test_suppress_instrumentation(self): + db_integration = dbapi.DatabaseApiIntegration( + "instrumenting_module_test_name", + "testcomponent", + ) + mock_connection = db_integration.wrapped_connection( + mock_connect, {}, {} + ) + with suppress_instrumentation(): + cursor = mock_connection.cursor() + cursor.execute("Test query", ("param1Value", False)) + + spans_list = self.memory_exporter.get_finished_spans() + self.assertEqual(len(spans_list), 0) + def test_executemany(self): db_integration = dbapi.DatabaseApiIntegration( "instrumenting_module_test_name", "testcomponent"