Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#3904](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3904))
- build: bump ruff to 0.14.1
([#3842](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3842))
- `opentelemetry-instrumentation-pymongo`: Fix invalid mongodb collection attribute type
([#3942](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3942))

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def started(self, event: monitoring.CommandStartedEvent):
command_name = event.command_name
span_name = f"{event.database_name}.{command_name}"
statement = self._get_statement_by_command_name(command_name, event)
collection = event.command.get(event.command_name)
collection = _get_command_collection_name(event)

try:
span = self._tracer.start_span(span_name, kind=SpanKind.CLIENT)
Expand Down Expand Up @@ -226,6 +226,13 @@ def _get_statement_by_command_name(
return statement


def _get_command_collection_name(event: CommandEvent) -> str | None:
collection_name = event.command.get(event.command_name)
if not collection_name or not isinstance(collection_name, str):
return None
return collection_name


def _get_span_dict_key(
event: CommandEvent,
) -> int | tuple[int, tuple[str, int | None]]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,43 @@ def test_capture_statement_disabled_aggregate(self):
span.attributes[SpanAttributes.DB_STATEMENT], "aggregate"
)

def test_collection_name_attribute(self):
scenarios = [
(
{
"command_name": "find",
"find": "test_collection",
},
"test_collection",
),
({"command_name": "find"}, None),
({"command_name": "find", "find": b"invalid"}, None),
]
for command_attrs, expected in scenarios:
with self.subTest(command_attrs=command_attrs, expected=expected):
mock_event = MockEvent(command_attrs)

command_tracer = CommandTracer(
self.tracer, capture_statement=True
)
command_tracer.started(event=mock_event)
command_tracer.succeeded(event=mock_event)

spans_list = self.memory_exporter.get_finished_spans()

self.assertEqual(len(spans_list), 1)
span = spans_list[0]

self.assertEqual(
span.attributes[SpanAttributes.DB_STATEMENT], "find"
)

self.assertEqual(
span.attributes.get(SpanAttributes.DB_MONGODB_COLLECTION),
expected,
)
self.memory_exporter.clear()


class MockCommand:
def __init__(self, command_attrs):
Expand Down