From aee42318f833756cdef8898617842ca3877b4b61 Mon Sep 17 00:00:00 2001 From: Chris Toshok Date: Wed, 29 Oct 2025 11:09:38 -0700 Subject: [PATCH 1/4] use the errmsg as the status description instead of the _DocumentOut --- .../src/opentelemetry/instrumentation/pymongo/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/__init__.py b/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/__init__.py index 7ada8d789a..60f8181847 100644 --- a/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/__init__.py @@ -198,7 +198,7 @@ def failed(self, event: monitoring.CommandFailedEvent): if span is None: return if span.is_recording(): - span.set_status(Status(StatusCode.ERROR, event.failure)) + span.set_status(Status(StatusCode.ERROR, event.failure.get("errmsg", "Unknown error"))) try: self.failed_hook(span, event) except ( From 5fab60bb5c050d9a87a72a8e7a46eb3ac0472596 Mon Sep 17 00:00:00 2001 From: Chris Toshok Date: Wed, 29 Oct 2025 11:36:44 -0700 Subject: [PATCH 2/4] fix the unit tests (in particular the test was mocking event.failure as a str, when that's not what the type is) --- .../tests/test_pymongo.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry-instrumentation-pymongo/tests/test_pymongo.py b/instrumentation/opentelemetry-instrumentation-pymongo/tests/test_pymongo.py index 8b082a4a14..94f21ec3ef 100644 --- a/instrumentation/opentelemetry-instrumentation-pymongo/tests/test_pymongo.py +++ b/instrumentation/opentelemetry-instrumentation-pymongo/tests/test_pymongo.py @@ -127,6 +127,7 @@ def test_failed(self): failed_hook=self.failed_callback, ) command_tracer.started(event=mock_event) + mock_event.mark_as_failed() command_tracer.failed(event=mock_event) spans_list = self.memory_exporter.get_finished_spans() @@ -137,7 +138,7 @@ def test_failed(self): span.status.status_code, trace_api.StatusCode.ERROR, ) - self.assertEqual(span.status.description, "failure") + self.assertEqual(span.status.description, "operation failed") self.assertIsNotNone(span.end_time) self.start_callback.assert_called_once() self.failed_callback.assert_called_once() @@ -149,6 +150,7 @@ def test_multiple_commands(self): command_tracer.started(event=first_mock_event) command_tracer.started(event=second_mock_event) command_tracer.succeeded(event=first_mock_event) + second_mock_event.mark_as_failed() command_tracer.failed(event=second_mock_event) spans_list = self.memory_exporter.get_finished_spans() @@ -292,5 +294,16 @@ def __init__(self, command_attrs, connection_id=None, request_id=""): self.connection_id = connection_id self.request_id = request_id + def mark_as_failed(self): + # CommandFailedEvent.failure is type _DocumentOut, which pymongo defines as: + # ``` + # _DocumentOut = Union[MutableMapping[str, Any], "RawBSONDocument"] + # ``` + # we go with the former, but both provide a `.get(key, default)` method. + # + self.failure = { + "errmsg": "operation failed" + } + def __getattr__(self, item): return item From b2daad1ecbceac1798ae64d9ff0bc63393b25325 Mon Sep 17 00:00:00 2001 From: Chris Toshok Date: Thu, 6 Nov 2025 07:54:15 -0700 Subject: [PATCH 3/4] reformat, fix tests --- .../src/opentelemetry/instrumentation/pymongo/__init__.py | 7 ++++++- .../tests/test_pymongo.py | 5 ++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/__init__.py b/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/__init__.py index 60f8181847..af2a3f7f02 100644 --- a/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/__init__.py @@ -198,7 +198,12 @@ def failed(self, event: monitoring.CommandFailedEvent): if span is None: return if span.is_recording(): - span.set_status(Status(StatusCode.ERROR, event.failure.get("errmsg", "Unknown error"))) + span.set_status( + Status( + StatusCode.ERROR, + event.failure.get("errmsg", "Unknown error"), + ) + ) try: self.failed_hook(span, event) except ( diff --git a/instrumentation/opentelemetry-instrumentation-pymongo/tests/test_pymongo.py b/instrumentation/opentelemetry-instrumentation-pymongo/tests/test_pymongo.py index 94f21ec3ef..9d9f817466 100644 --- a/instrumentation/opentelemetry-instrumentation-pymongo/tests/test_pymongo.py +++ b/instrumentation/opentelemetry-instrumentation-pymongo/tests/test_pymongo.py @@ -293,6 +293,7 @@ def __init__(self, command_attrs, connection_id=None, request_id=""): self.command_name = self.command.get("command_name") self.connection_id = connection_id self.request_id = request_id + self.failure = None def mark_as_failed(self): # CommandFailedEvent.failure is type _DocumentOut, which pymongo defines as: @@ -301,9 +302,7 @@ def mark_as_failed(self): # ``` # we go with the former, but both provide a `.get(key, default)` method. # - self.failure = { - "errmsg": "operation failed" - } + self.failure = {"errmsg": "operation failed"} def __getattr__(self, item): return item From 859d1f494255cb9ab27692006bb9fbacfc75cd8f Mon Sep 17 00:00:00 2001 From: Chris Toshok Date: Thu, 6 Nov 2025 08:00:12 -0700 Subject: [PATCH 4/4] add changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b36b689b60..0382235e90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,7 +44,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#3836](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3836)) - `opentelemetry-instrumentation-elasticsearch`: Enhance elasticsearch query body sanitization ([#3919](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3919)) - +- `opentelemetry-instrumentation-pymongo`: Fix span error descriptions + ([#3904](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3904)) ## Version 1.38.0/0.59b0 (2025-10-16)