Skip to content

Commit 6d8becf

Browse files
nao23emdneto
andauthored
feat: respect supress_instrumentation functionality in dbapi instrumentation (#3460)
* respect supress_instrumentation * update CHANGELOG * fix link * update CHANGELOG --------- Co-authored-by: Emídio Neto <[email protected]>
1 parent 6c89a56 commit 6d8becf

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4141
([#3544](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3544))
4242
- `opentelemetry-instrumentation-botocore` Add type check when extracting tool use from Bedrock request message content
4343
([#3548](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3548))
44+
- `opentelemetry-instrumentation-dbapi` Respect suppress_instrumentation functionality ([#3460](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3460))
4445

4546
### Breaking changes
4647

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
from opentelemetry.instrumentation.sqlcommenter_utils import _add_sql_comment
5353
from opentelemetry.instrumentation.utils import (
5454
_get_opentelemetry_values,
55+
is_instrumentation_enabled,
5556
unwrap,
5657
)
5758
from opentelemetry.semconv.trace import SpanAttributes
@@ -561,6 +562,9 @@ def traced_execution(
561562
*args: tuple[Any, ...],
562563
**kwargs: dict[Any, Any],
563564
):
565+
if not is_instrumentation_enabled():
566+
return query_method(*args, **kwargs)
567+
564568
name = self.get_operation_name(cursor, args)
565569
if not name:
566570
name = (

instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from opentelemetry import context
2222
from opentelemetry import trace as trace_api
2323
from opentelemetry.instrumentation import dbapi
24+
from opentelemetry.instrumentation.utils import suppress_instrumentation
2425
from opentelemetry.sdk import resources
2526
from opentelemetry.semconv.trace import SpanAttributes
2627
from opentelemetry.test.test_base import TestBase
@@ -243,6 +244,21 @@ def test_no_op_tracer_provider(self):
243244
spans_list = self.memory_exporter.get_finished_spans()
244245
self.assertEqual(len(spans_list), 0)
245246

247+
def test_suppress_instrumentation(self):
248+
db_integration = dbapi.DatabaseApiIntegration(
249+
"instrumenting_module_test_name",
250+
"testcomponent",
251+
)
252+
mock_connection = db_integration.wrapped_connection(
253+
mock_connect, {}, {}
254+
)
255+
with suppress_instrumentation():
256+
cursor = mock_connection.cursor()
257+
cursor.execute("Test query", ("param1Value", False))
258+
259+
spans_list = self.memory_exporter.get_finished_spans()
260+
self.assertEqual(len(spans_list), 0)
261+
246262
def test_executemany(self):
247263
db_integration = dbapi.DatabaseApiIntegration(
248264
"instrumenting_module_test_name", "testcomponent"

0 commit comments

Comments
 (0)