Skip to content

Commit

Permalink
Update Span.record_exception to to adhere to specs (#1314)
Browse files Browse the repository at this point in the history
  • Loading branch information
srikanthccv authored Nov 3, 2020
1 parent 4acad01 commit 28f35b8
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 7 deletions.
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

0 comments on commit 28f35b8

Please sign in to comment.