File tree Expand file tree Collapse file tree 3 files changed +25
-7
lines changed
src/opentelemetry/sdk/trace Expand file tree Collapse file tree 3 files changed +25
-7
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 ),
Original file line number Diff line number Diff 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
12961313def span_event_start_fmt (span_processor_name , span_name ):
12971314 return span_processor_name + ":" + span_name + ":start"
You can’t perform that action at this time.
0 commit comments