Skip to content

Commit 613d33a

Browse files
chloe-zh97xrmx
andauthored
add commenter_options in trace_integration (#3743)
Co-authored-by: Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com>
1 parent 02fedf3 commit 613d33a

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222

2323
- `opentelemetry-instrumentation-botocore`: Add support for AWS Secrets Manager semantic convention attribute
2424
([#3765](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3765))
25+
- `opentelemetry-instrumentation-dbapi`: Add support for `commenter_options` in `trace_integration` function to control SQLCommenter behavior
26+
([#3743](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3743))
2527
- Add `rstcheck` to pre-commit to stop introducing invalid RST
2628
([#3777](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3777))
2729
- `opentelemetry-exporter-credential-provider-gcp`: create this package which provides support for supplying your machine's Application Default

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ def trace_integration(
208208
enable_commenter: bool = False,
209209
db_api_integration_factory: type[DatabaseApiIntegration] | None = None,
210210
enable_attribute_commenter: bool = False,
211+
commenter_options: dict[str, Any] | None = None,
211212
):
212213
"""Integrate with DB API library.
213214
https://www.python.org/dev/peps/pep-0249/
@@ -226,6 +227,7 @@ def trace_integration(
226227
db_api_integration_factory: The `DatabaseApiIntegration` to use. If none is passed the
227228
default one is used.
228229
enable_attribute_commenter: Flag to enable/disable sqlcomment inclusion in `db.statement` span attribute. Only available if enable_commenter=True.
230+
commenter_options: Configurations for tags to be appended at the sql query.
229231
"""
230232
wrap_connect(
231233
__name__,
@@ -239,6 +241,7 @@ def trace_integration(
239241
enable_commenter=enable_commenter,
240242
db_api_integration_factory=db_api_integration_factory,
241243
enable_attribute_commenter=enable_attribute_commenter,
244+
commenter_options=commenter_options,
242245
)
243246

244247

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,45 @@ def test_suppress_instrumentation(self):
259259
spans_list = self.memory_exporter.get_finished_spans()
260260
self.assertEqual(len(spans_list), 0)
261261

262+
def test_commenter_options_propagation(self):
263+
db_integration = dbapi.DatabaseApiIntegration(
264+
"instrumenting_module_test_name",
265+
"testcomponent",
266+
commenter_options={"opentelemetry_values": False},
267+
)
268+
mock_connection = db_integration.wrapped_connection(
269+
mock_connect, {}, {}
270+
)
271+
cursor = mock_connection.cursor()
272+
with self.assertRaises(Exception):
273+
cursor.execute("SELECT 1", throw_exception=True)
274+
spans_list = self.memory_exporter.get_finished_spans()
275+
self.assertEqual(len(spans_list), 1)
276+
span = spans_list[0]
277+
self.assertEqual(span.attributes.get("db.system"), "testcomponent")
278+
self.assertIn("opentelemetry_values", db_integration.commenter_options)
279+
self.assertFalse(
280+
db_integration.commenter_options["opentelemetry_values"]
281+
)
282+
283+
@mock.patch("opentelemetry.instrumentation.dbapi.wrap_connect")
284+
def test_trace_integration_passes_commenter_options(
285+
self, mock_wrap_connect
286+
):
287+
fake_connect_module = mock.Mock()
288+
fake_options = {"opentelemetry_values": False, "foo": "bar"}
289+
dbapi.trace_integration(
290+
connect_module=fake_connect_module,
291+
connect_method_name="connect",
292+
database_system="testdb",
293+
commenter_options=fake_options,
294+
)
295+
mock_wrap_connect.assert_called_once()
296+
_, _, kwargs = mock_wrap_connect.mock_calls[0]
297+
298+
self.assertIn("commenter_options", kwargs)
299+
self.assertEqual(kwargs["commenter_options"], fake_options)
300+
262301
def test_executemany(self):
263302
db_integration = dbapi.DatabaseApiIntegration(
264303
"instrumenting_module_test_name", "testcomponent"

0 commit comments

Comments
 (0)