Skip to content

Commit 28f35b8

Browse files
authored
Update Span.record_exception to to adhere to specs (#1314)
1 parent 4acad01 commit 28f35b8

File tree

4 files changed

+91
-7
lines changed

4 files changed

+91
-7
lines changed

Diff for: opentelemetry-api/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Add optional parameter to `record_exception` method ([#1314](https://github.com/open-telemetry/opentelemetry-python/pull/1314))
6+
57
## Version 0.15b0
68

79
Released 2020-11-02

Diff for: opentelemetry-sdk/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Add optional parameter to `record_exception` method ([#1314](https://github.com/open-telemetry/opentelemetry-python/pull/1314))
6+
57
## Version 0.15b0
68

79
Released 2020-11-02

Diff for: opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -694,14 +694,15 @@ def record_exception(
694694
# an AttributeError if the __context__ on
695695
# an exception is None
696696
stacktrace = "Exception occurred on stacktrace formatting"
697-
697+
_attributes = {
698+
"exception.type": exception.__class__.__name__,
699+
"exception.message": str(exception),
700+
"exception.stacktrace": stacktrace,
701+
}
702+
if attributes:
703+
_attributes.update(attributes)
698704
self.add_event(
699-
name="exception",
700-
attributes={
701-
"exception.type": exception.__class__.__name__,
702-
"exception.message": str(exception),
703-
"exception.stacktrace": stacktrace,
704-
},
705+
name="exception", attributes=_attributes, timestamp=timestamp
705706
)
706707

707708

Diff for: opentelemetry-sdk/tests/trace/test_trace.py

+79
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,85 @@ def test_record_exception(self):
874874
exception_event.attributes["exception.stacktrace"],
875875
)
876876

877+
def test_record_exception_with_attributes(self):
878+
span = trace._Span("name", mock.Mock(spec=trace_api.SpanContext))
879+
try:
880+
raise RuntimeError("error")
881+
except RuntimeError as err:
882+
attributes = {"has_additional_attributes": True}
883+
span.record_exception(err, attributes)
884+
exception_event = span.events[0]
885+
self.assertEqual("exception", exception_event.name)
886+
self.assertEqual(
887+
"error", exception_event.attributes["exception.message"]
888+
)
889+
self.assertEqual(
890+
"RuntimeError", exception_event.attributes["exception.type"]
891+
)
892+
self.assertIn(
893+
"RuntimeError: error",
894+
exception_event.attributes["exception.stacktrace"],
895+
)
896+
self.assertIn(
897+
"has_additional_attributes", exception_event.attributes,
898+
)
899+
self.assertEqual(
900+
True, exception_event.attributes["has_additional_attributes"],
901+
)
902+
903+
def test_record_exception_with_timestamp(self):
904+
span = trace._Span("name", mock.Mock(spec=trace_api.SpanContext))
905+
try:
906+
raise RuntimeError("error")
907+
except RuntimeError as err:
908+
timestamp = 1604238587112021089
909+
span.record_exception(err, timestamp=timestamp)
910+
exception_event = span.events[0]
911+
self.assertEqual("exception", exception_event.name)
912+
self.assertEqual(
913+
"error", exception_event.attributes["exception.message"]
914+
)
915+
self.assertEqual(
916+
"RuntimeError", exception_event.attributes["exception.type"]
917+
)
918+
self.assertIn(
919+
"RuntimeError: error",
920+
exception_event.attributes["exception.stacktrace"],
921+
)
922+
self.assertEqual(
923+
1604238587112021089, exception_event.timestamp,
924+
)
925+
926+
def test_record_exception_with_attributes_and_timestamp(self):
927+
span = trace._Span("name", mock.Mock(spec=trace_api.SpanContext))
928+
try:
929+
raise RuntimeError("error")
930+
except RuntimeError as err:
931+
attributes = {"has_additional_attributes": True}
932+
timestamp = 1604238587112021089
933+
span.record_exception(err, attributes, timestamp)
934+
exception_event = span.events[0]
935+
self.assertEqual("exception", exception_event.name)
936+
self.assertEqual(
937+
"error", exception_event.attributes["exception.message"]
938+
)
939+
self.assertEqual(
940+
"RuntimeError", exception_event.attributes["exception.type"]
941+
)
942+
self.assertIn(
943+
"RuntimeError: error",
944+
exception_event.attributes["exception.stacktrace"],
945+
)
946+
self.assertIn(
947+
"has_additional_attributes", exception_event.attributes,
948+
)
949+
self.assertEqual(
950+
True, exception_event.attributes["has_additional_attributes"],
951+
)
952+
self.assertEqual(
953+
1604238587112021089, exception_event.timestamp,
954+
)
955+
877956
def test_record_exception_context_manager(self):
878957
try:
879958
with self.tracer.start_as_current_span("span") as span:

0 commit comments

Comments
 (0)