Skip to content

Commit 06b82aa

Browse files
xrmxocelotlsrikanthccv
authored
Make record_exception more robust handling them outside the except scope (open-telemetry#3778)
Make the stacktrace formatting not rely on being inside the except scope. Fixes open-telemetry#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>
1 parent 2574707 commit 06b82aa

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
([#3648](https://github.com/open-telemetry/opentelemetry-python/pull/3648))
1818
- Fix ValueError message for PeriodicExportingMetricsReader
1919
([#3769](https://github.com/open-telemetry/opentelemetry-python/pull/3769))
20+
- Make span.record_exception more robust
21+
([#3778](https://github.com/open-telemetry/opentelemetry-python/pull/3778))
2022

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

opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -988,13 +988,12 @@ def record_exception(
988988
escaped: bool = False,
989989
) -> None:
990990
"""Records an exception as a span event."""
991-
try:
992-
stacktrace = traceback.format_exc()
993-
except Exception: # pylint: disable=broad-except
994-
# workaround for python 3.4, format_exc can raise
995-
# an AttributeError if the __context__ on
996-
# an exception is None
997-
stacktrace = "Exception occurred on stacktrace formatting"
991+
# TODO: keep only exception as first argument after baseline is 3.10
992+
stacktrace = "".join(
993+
traceback.format_exception(
994+
type(exception), value=exception, tb=exception.__traceback__
995+
)
996+
)
998997
_attributes = {
999998
"exception.type": exception.__class__.__name__,
1000999
"exception.message": str(exception),

opentelemetry-sdk/tests/trace/test_trace.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,23 @@ def test_record_exception_context_manager(self):
12921292
finally:
12931293
self.assertEqual(len(span.events), 0)
12941294

1295+
def test_record_exception_out_of_scope(self):
1296+
span = trace._Span("name", mock.Mock(spec=trace_api.SpanContext))
1297+
out_of_scope_exception = ValueError("invalid")
1298+
span.record_exception(out_of_scope_exception)
1299+
exception_event = span.events[0]
1300+
self.assertEqual("exception", exception_event.name)
1301+
self.assertEqual(
1302+
"invalid", exception_event.attributes["exception.message"]
1303+
)
1304+
self.assertEqual(
1305+
"ValueError", exception_event.attributes["exception.type"]
1306+
)
1307+
self.assertIn(
1308+
"ValueError: invalid",
1309+
exception_event.attributes["exception.stacktrace"],
1310+
)
1311+
12951312

12961313
def span_event_start_fmt(span_processor_name, span_name):
12971314
return span_processor_name + ":" + span_name + ":start"

0 commit comments

Comments
 (0)