Skip to content

Commit

Permalink
Make record_exception more robust handling them outside the except sc…
Browse files Browse the repository at this point in the history
…ope (#3778)

Make the stacktrace formatting not rely on being inside the except
scope.

Fixes #3762

Suggested-by: Alex Hall <alex.mojaki@gmail.com>

Co-authored-by: Diego Hurtado <ocelotl@users.noreply.github.com>
Co-authored-by: Srikanth Chekuri <srikanth.chekuri92@gmail.com>
  • Loading branch information
3 people authored Mar 21, 2024
1 parent 2574707 commit 06b82aa
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#3648](https://github.com/open-telemetry/opentelemetry-python/pull/3648))
- Fix ValueError message for PeriodicExportingMetricsReader
([#3769](https://github.com/open-telemetry/opentelemetry-python/pull/3769))
- Make span.record_exception more robust
([#3778](https://github.com/open-telemetry/opentelemetry-python/pull/3778))

## Version 1.23.0/0.44b0 (2024-02-23)

Expand Down
13 changes: 6 additions & 7 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -988,13 +988,12 @@ def record_exception(
escaped: bool = False,
) -> None:
"""Records an exception as a span event."""
try:
stacktrace = traceback.format_exc()
except Exception: # pylint: disable=broad-except
# workaround for python 3.4, format_exc can raise
# an AttributeError if the __context__ on
# an exception is None
stacktrace = "Exception occurred on stacktrace formatting"
# TODO: keep only exception as first argument after baseline is 3.10
stacktrace = "".join(
traceback.format_exception(
type(exception), value=exception, tb=exception.__traceback__
)
)
_attributes = {
"exception.type": exception.__class__.__name__,
"exception.message": str(exception),
Expand Down
17 changes: 17 additions & 0 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,23 @@ def test_record_exception_context_manager(self):
finally:
self.assertEqual(len(span.events), 0)

def test_record_exception_out_of_scope(self):
span = trace._Span("name", mock.Mock(spec=trace_api.SpanContext))
out_of_scope_exception = ValueError("invalid")
span.record_exception(out_of_scope_exception)
exception_event = span.events[0]
self.assertEqual("exception", exception_event.name)
self.assertEqual(
"invalid", exception_event.attributes["exception.message"]
)
self.assertEqual(
"ValueError", exception_event.attributes["exception.type"]
)
self.assertIn(
"ValueError: invalid",
exception_event.attributes["exception.stacktrace"],
)


def span_event_start_fmt(span_processor_name, span_name):
return span_processor_name + ":" + span_name + ":start"
Expand Down

0 comments on commit 06b82aa

Please sign in to comment.