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
Prev Previous commit
Next Next commit
fix: remove mock from tests and improve get qualname
  • Loading branch information
emdneto committed Apr 6, 2024
commit a06a30cdad055a9338fcab94f0d96565c7d28b0a
7 changes: 4 additions & 3 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
@@ -997,12 +997,13 @@ def record_exception(
)
module = (
emdneto marked this conversation as resolved.
Show resolved Hide resolved
exception.__module__
if exception.__class__.__module__ != "builtins"
if type(exception).__module__ != "builtins"
else ""
)
qualname = type(exception).__qualname__
exc_type = (
f"{module}.{exception.__class__.__qualname__}"
if module else exception.__class__.__qualname__
f"{module}.{qualname}"
if module else qualname
)
_attributes: MutableMapping[str, types.AttributeValue] = {
"exception.type": exc_type,
11 changes: 4 additions & 7 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
@@ -1150,19 +1150,16 @@ def error_status_test(context):

def test_record_exception_fqn(self):
span = trace._Span("name", mock.Mock(spec=trace_api.SpanContext))
module_name = "dummy.module"
with mock.patch.object(DummyError, "__module__", module_name):
try:
raise DummyError("error")
except DummyError as err:
span.record_exception(err)
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(
f"{module_name}.DummyError",
exception_type,
exception_event.attributes["exception.type"],
)
self.assertIn(