Skip to content

Commit fcbe18d

Browse files
authored
opentelemetry-instrumentation-pymongo: fix invalid mongodb collection attribute type (#3942)
* opentelemetry-instrumentation-pymongo: fix invalid mongodb collection attribute type * update CHANGELOG.md
1 parent 3b97e36 commit fcbe18d

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5050
([#3904](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3904))
5151
- build: bump ruff to 0.14.1
5252
([#3842](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3842))
53+
- `opentelemetry-instrumentation-pymongo`: Fix invalid mongodb collection attribute type
54+
([#3942](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3942))
5355

5456
## Version 1.38.0/0.59b0 (2025-10-16)
5557

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def started(self, event: monitoring.CommandStartedEvent):
138138
command_name = event.command_name
139139
span_name = f"{event.database_name}.{command_name}"
140140
statement = self._get_statement_by_command_name(command_name, event)
141-
collection = event.command.get(event.command_name)
141+
collection = _get_command_collection_name(event)
142142

143143
try:
144144
span = self._tracer.start_span(span_name, kind=SpanKind.CLIENT)
@@ -226,6 +226,13 @@ def _get_statement_by_command_name(
226226
return statement
227227

228228

229+
def _get_command_collection_name(event: CommandEvent) -> str | None:
230+
collection_name = event.command.get(event.command_name)
231+
if not collection_name or not isinstance(collection_name, str):
232+
return None
233+
return collection_name
234+
235+
229236
def _get_span_dict_key(
230237
event: CommandEvent,
231238
) -> int | tuple[int, tuple[str, int | None]]:

instrumentation/opentelemetry-instrumentation-pymongo/tests/test_pymongo.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,43 @@ def test_capture_statement_disabled_aggregate(self):
278278
span.attributes[SpanAttributes.DB_STATEMENT], "aggregate"
279279
)
280280

281+
def test_collection_name_attribute(self):
282+
scenarios = [
283+
(
284+
{
285+
"command_name": "find",
286+
"find": "test_collection",
287+
},
288+
"test_collection",
289+
),
290+
({"command_name": "find"}, None),
291+
({"command_name": "find", "find": b"invalid"}, None),
292+
]
293+
for command_attrs, expected in scenarios:
294+
with self.subTest(command_attrs=command_attrs, expected=expected):
295+
mock_event = MockEvent(command_attrs)
296+
297+
command_tracer = CommandTracer(
298+
self.tracer, capture_statement=True
299+
)
300+
command_tracer.started(event=mock_event)
301+
command_tracer.succeeded(event=mock_event)
302+
303+
spans_list = self.memory_exporter.get_finished_spans()
304+
305+
self.assertEqual(len(spans_list), 1)
306+
span = spans_list[0]
307+
308+
self.assertEqual(
309+
span.attributes[SpanAttributes.DB_STATEMENT], "find"
310+
)
311+
312+
self.assertEqual(
313+
span.attributes.get(SpanAttributes.DB_MONGODB_COLLECTION),
314+
expected,
315+
)
316+
self.memory_exporter.clear()
317+
281318

282319
class MockCommand:
283320
def __init__(self, command_attrs):

0 commit comments

Comments
 (0)