Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add capture the fully qualified type name for raised exceptions in spans #3837

Merged
merged 13 commits into from
Apr 11, 2024
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#3798](https://github.com/open-telemetry/opentelemetry-python/pull/3798))
- Fix otlp exporter to export log_record.observed_timestamp
([#3785](https://github.com/open-telemetry/opentelemetry-python/pull/3785))
- Add capture the fully qualified type name for raised exceptions in spans
([#3837](https://github.com/open-telemetry/opentelemetry-python/pull/3837))

## Version 1.24.0/0.45b0 (2024-03-28)

Expand Down
9 changes: 8 additions & 1 deletion opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -995,8 +995,15 @@ def record_exception(
type(exception), value=exception, tb=exception.__traceback__
)
)
module = type(exception).__module__
qualname = type(exception).__qualname__
exception_type = (
f"{module}.{qualname}"
if module and module != "builtins"
lzchen marked this conversation as resolved.
Show resolved Hide resolved
else qualname
)
_attributes: MutableMapping[str, types.AttributeValue] = {
"exception.type": exception.__class__.__name__,
"exception.type": exception_type,
"exception.message": str(exception),
"exception.stacktrace": stacktrace,
"exception.escaped": str(escaped),
Expand Down
23 changes: 23 additions & 0 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,10 @@ def test_events(self):
self.assertEqual(span.events, tuple(events))


class DummyError(Exception):
pass


class TestSpan(unittest.TestCase):
# pylint: disable=too-many-public-methods

Expand Down Expand Up @@ -1144,6 +1148,25 @@ def error_status_test(context):
.start_as_current_span("root")
)

def test_record_exception_fqn(self):
span = trace._Span("name", mock.Mock(spec=trace_api.SpanContext))
exception = DummyError("error")
exception_type = "tests.trace.test_trace.DummyError"
span.record_exception(exception)
exception_event = span.events[0]
self.assertEqual("exception", exception_event.name)
self.assertEqual(
"error", exception_event.attributes["exception.message"]
)
self.assertEqual(
exception_type,
exception_event.attributes["exception.type"],
)
self.assertIn(
"DummyError: error",
exception_event.attributes["exception.stacktrace"],
)

def test_record_exception(self):
span = trace._Span("name", mock.Mock(spec=trace_api.SpanContext))
try:
Expand Down
Loading