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
12 changes: 11 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,18 @@ def record_exception(
type(exception), value=exception, tb=exception.__traceback__
)
)
module = (
emdneto marked this conversation as resolved.
Show resolved Hide resolved
exception.__module__
if type(exception).__module__ != "builtins"
else ""
)
qualname = type(exception).__qualname__
exc_type = (
f"{module}.{qualname}"
if module else qualname
)
_attributes: MutableMapping[str, types.AttributeValue] = {
"exception.type": exception.__class__.__name__,
"exception.type": exc_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 = f"{exception.__module__}.{type(exception).__qualname__}"
emdneto marked this conversation as resolved.
Show resolved Hide resolved
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