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

Update Span.record_exception to to adhere to specs #1314

Merged
merged 7 commits into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
14 changes: 12 additions & 2 deletions opentelemetry-api/src/opentelemetry/trace/span.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ def set_status(self, status: Status) -> None:
"""

@abc.abstractmethod
def record_exception(self, exception: Exception) -> None:
def record_exception(
self,
exception: Exception,
attributes: types.Attributes = None,
timestamp: typing.Optional[int] = None,
) -> None:
"""Records an exception as a span event."""

def __enter__(self) -> "Span":
Expand Down Expand Up @@ -263,7 +268,12 @@ def update_name(self, name: str) -> None:
def set_status(self, status: Status) -> None:
pass

def record_exception(self, exception: Exception) -> None:
def record_exception(
self,
exception: Exception,
attributes: types.Attributes = None,
timestamp: typing.Optional[int] = None,
) -> None:
pass


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
22 changes: 14 additions & 8 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,12 @@ def __exit__(

super().__exit__(exc_type, exc_val, exc_tb)

def record_exception(self, exception: Exception) -> None:
def record_exception(
self,
exception: Exception,
attributes: types.Attributes = None,
timestamp: Optional[int] = None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is timestamp defined in the specs?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From specs

To facilitate recording an exception languages SHOULD provide a RecordException method if the language uses exceptions. This is a specialized variant of AddEvent, so for anything not specified here, the same requirements as for AddEvent apply

An API to record a single Event where the Event properties are passed as arguments. This MAY be called AddEvent. This API takes the name of the event, optional Attributes and an optional Timestamp which can be used to specify the time at which the event occurred

) -> None:
"""Records an exception as a span event."""
try:
stacktrace = traceback.format_exc()
Expand All @@ -689,14 +694,15 @@ def record_exception(self, exception: Exception) -> None:
# 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