Skip to content

Update Span.record_exception to to adhere to specs #1314

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

Merged
merged 7 commits into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions opentelemetry-api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Add optional parameter to `record_exception` method ([#1314](https://github.com/open-telemetry/opentelemetry-python/pull/1314))

## Version 0.15b0

Released 2020-11-02
Expand Down
2 changes: 2 additions & 0 deletions opentelemetry-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Add optional parameter to `record_exception` method ([#1314](https://github.com/open-telemetry/opentelemetry-python/pull/1314))

## Version 0.15b0

Released 2020-11-02
Expand Down
15 changes: 8 additions & 7 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,14 +694,15 @@ def record_exception(
# an AttributeError if the __context__ on
# an exception is None
stacktrace = "Exception occurred on stacktrace formatting"

_attributes = {
"exception.type": exception.__class__.__name__,
"exception.message": str(exception),
"exception.stacktrace": stacktrace,
}
if attributes:
_attributes.update(attributes)
self.add_event(
name="exception",
attributes={
"exception.type": exception.__class__.__name__,
"exception.message": str(exception),
"exception.stacktrace": stacktrace,
},
name="exception", attributes=_attributes, timestamp=timestamp
)


Expand Down
79 changes: 79 additions & 0 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,85 @@ def test_record_exception(self):
exception_event.attributes["exception.stacktrace"],
)

def test_record_exception_with_attributes(self):
span = trace._Span("name", mock.Mock(spec=trace_api.SpanContext))
try:
raise RuntimeError("error")
except RuntimeError as err:
attributes = {"has_additional_attributes": True}
span.record_exception(err, attributes)
exception_event = span.events[0]
self.assertEqual("exception", exception_event.name)
self.assertEqual(
"error", exception_event.attributes["exception.message"]
)
self.assertEqual(
"RuntimeError", exception_event.attributes["exception.type"]
)
self.assertIn(
"RuntimeError: error",
exception_event.attributes["exception.stacktrace"],
)
self.assertIn(
"has_additional_attributes", exception_event.attributes,
)
self.assertEqual(
True, exception_event.attributes["has_additional_attributes"],
)

def test_record_exception_with_timestamp(self):
span = trace._Span("name", mock.Mock(spec=trace_api.SpanContext))
try:
raise RuntimeError("error")
except RuntimeError as err:
timestamp = 1604238587112021089
span.record_exception(err, timestamp=timestamp)
exception_event = span.events[0]
self.assertEqual("exception", exception_event.name)
self.assertEqual(
"error", exception_event.attributes["exception.message"]
)
self.assertEqual(
"RuntimeError", exception_event.attributes["exception.type"]
)
self.assertIn(
"RuntimeError: error",
exception_event.attributes["exception.stacktrace"],
)
self.assertEqual(
1604238587112021089, exception_event.timestamp,
)

def test_record_exception_with_attributes_and_timestamp(self):
span = trace._Span("name", mock.Mock(spec=trace_api.SpanContext))
try:
raise RuntimeError("error")
except RuntimeError as err:
attributes = {"has_additional_attributes": True}
timestamp = 1604238587112021089
span.record_exception(err, attributes, timestamp)
exception_event = span.events[0]
self.assertEqual("exception", exception_event.name)
self.assertEqual(
"error", exception_event.attributes["exception.message"]
)
self.assertEqual(
"RuntimeError", exception_event.attributes["exception.type"]
)
self.assertIn(
"RuntimeError: error",
exception_event.attributes["exception.stacktrace"],
)
self.assertIn(
"has_additional_attributes", exception_event.attributes,
)
self.assertEqual(
True, exception_event.attributes["has_additional_attributes"],
)
self.assertEqual(
1604238587112021089, exception_event.timestamp,
)

def test_record_exception_context_manager(self):
try:
with self.tracer.start_as_current_span("span") as span:
Expand Down