diff --git a/ddtrace/internal/opentelemetry/span.py b/ddtrace/internal/opentelemetry/span.py index 321368d0d81..87fff1869f5 100644 --- a/ddtrace/internal/opentelemetry/span.py +++ b/ddtrace/internal/opentelemetry/span.py @@ -259,16 +259,17 @@ def record_exception(self, exception, attributes=None, timestamp=None, escaped=F attrs.update(attributes) # Set the error type, error message and error stacktrace tags on the span - self._ddspan._meta[ERROR_MSG] = attrs["exception.message"] - self._ddspan._meta[ERROR_TYPE] = attrs["exception.type"] - if "exception.stacktrace" in attrs: - self._ddspan._meta[ERROR_STACK] = attrs["exception.stacktrace"] - else: - self._ddspan._meta[ERROR_STACK] = "".join( - traceback.format_exception( - type(exception), exception, exception.__traceback__, limit=config._span_traceback_max_size + if escaped: + self._ddspan._meta[ERROR_MSG] = attrs["exception.message"] + self._ddspan._meta[ERROR_TYPE] = attrs["exception.type"] + if "exception.stacktrace" in attrs: + self._ddspan._meta[ERROR_STACK] = attrs["exception.stacktrace"] + else: + self._ddspan._meta[ERROR_STACK] = "".join( + traceback.format_exception( + type(exception), exception, exception.__traceback__, limit=config._span_traceback_max_size + ) ) - ) self.add_event(name="exception", attributes=attrs, timestamp=timestamp) def __enter__(self): diff --git a/releasenotes/notes/fix-otel-record-exception-api-17f3a4b23948ec79.yaml b/releasenotes/notes/fix-otel-record-exception-api-17f3a4b23948ec79.yaml new file mode 100644 index 00000000000..ec09758ea79 --- /dev/null +++ b/releasenotes/notes/fix-otel-record-exception-api-17f3a4b23948ec79.yaml @@ -0,0 +1,4 @@ +--- +fixes: + - | + opentelemetry: Ensures ``Span.record_exception(...)`` sets the span error metadata only if escaped is True. \ No newline at end of file diff --git a/tests/snapshots/tests.opentelemetry.test_trace.test_otel_start_span_record_exception.json b/tests/snapshots/tests.opentelemetry.test_trace.test_otel_start_span_record_exception.json index a98e078f41f..db978e7306d 100644 --- a/tests/snapshots/tests.opentelemetry.test_trace.test_otel_start_span_record_exception.json +++ b/tests/snapshots/tests.opentelemetry.test_trace.test_otel_start_span_record_exception.json @@ -40,9 +40,6 @@ "meta": { "_dd.p.dm": "-0", "_dd.p.tid": "6658897400000000", - "error.message": "MoonEar Fire!!!", - "error.stack": "Fake traceback", - "error.type": "RandoException", "events": "[{\"name\": \"exception\", \"time_unix_nano\": 1716560281337739000, \"attributes\": {\"exception.type\": \"builtins.IndexError\", \"exception.message\": \"Invalid Operation 2\", \"exception.escaped\": \"False\", \"exception.stuff\": \"thing 2\"}}, {\"name\": \"exception\", \"time_unix_nano\": 1716560271237812000, \"attributes\": {\"exception.type\": \"RandoException\", \"exception.message\": \"MoonEar Fire!!!\", \"exception.escaped\": \"False\", \"exception.stacktrace\": \"Fake traceback\", \"exception.details\": \"This is FAKE, I overwrote the real exception details\"}}]", "language": "python", "runtime-id": "d0950ce7bda6498183acde9036abb131"