Skip to content

Commit 8356368

Browse files
toshokxrmx
andauthored
pymongo: for CommandFailedEvent use the errmsg as the status description instead of the _DocumentOut (#3904)
* use the errmsg as the status description instead of the _DocumentOut * fix the unit tests (in particular the test was mocking event.failure as a str, when that's not what the type is) * reformat, fix tests * add changelog --------- Co-authored-by: Riccardo Magliocchetti <[email protected]>
1 parent 4f89e75 commit 8356368

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4343
- `opentelemetry-instrumentation-aiohttp-server`: delay initialization of tracer, meter and excluded urls to instrumentation for testability
4444
([#3836](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3836))
4545
- `opentelemetry-instrumentation-elasticsearch`: Enhance elasticsearch query body sanitization
46-
([#3919](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3919))
46+
([#3919](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3919))
47+
- `opentelemetry-instrumentation-pymongo`: Fix span error descriptions
48+
([#3904](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3904))
4749
- build: bump ruff to 0.14.1
4850
([#3842](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3842))
4951

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

5354
### Fixed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,12 @@ def failed(self, event: monitoring.CommandFailedEvent):
198198
if span is None:
199199
return
200200
if span.is_recording():
201-
span.set_status(Status(StatusCode.ERROR, event.failure))
201+
span.set_status(
202+
Status(
203+
StatusCode.ERROR,
204+
event.failure.get("errmsg", "Unknown error"),
205+
)
206+
)
202207
try:
203208
self.failed_hook(span, event)
204209
except (

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ def test_failed(self):
127127
failed_hook=self.failed_callback,
128128
)
129129
command_tracer.started(event=mock_event)
130+
mock_event.mark_as_failed()
130131
command_tracer.failed(event=mock_event)
131132

132133
spans_list = self.memory_exporter.get_finished_spans()
@@ -137,7 +138,7 @@ def test_failed(self):
137138
span.status.status_code,
138139
trace_api.StatusCode.ERROR,
139140
)
140-
self.assertEqual(span.status.description, "failure")
141+
self.assertEqual(span.status.description, "operation failed")
141142
self.assertIsNotNone(span.end_time)
142143
self.start_callback.assert_called_once()
143144
self.failed_callback.assert_called_once()
@@ -149,6 +150,7 @@ def test_multiple_commands(self):
149150
command_tracer.started(event=first_mock_event)
150151
command_tracer.started(event=second_mock_event)
151152
command_tracer.succeeded(event=first_mock_event)
153+
second_mock_event.mark_as_failed()
152154
command_tracer.failed(event=second_mock_event)
153155

154156
spans_list = self.memory_exporter.get_finished_spans()
@@ -291,6 +293,16 @@ def __init__(self, command_attrs, connection_id=None, request_id=""):
291293
self.command_name = self.command.get("command_name")
292294
self.connection_id = connection_id
293295
self.request_id = request_id
296+
self.failure = None
297+
298+
def mark_as_failed(self):
299+
# CommandFailedEvent.failure is type _DocumentOut, which pymongo defines as:
300+
# ```
301+
# _DocumentOut = Union[MutableMapping[str, Any], "RawBSONDocument"]
302+
# ```
303+
# we go with the former, but both provide a `.get(key, default)` method.
304+
#
305+
self.failure = {"errmsg": "operation failed"}
294306

295307
def __getattr__(self, item):
296308
return item

0 commit comments

Comments
 (0)