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
17
17
([ #3648 ] ( https://github.com/open-telemetry/opentelemetry-python/pull/3648 ) )
18
18
- Fix ValueError message for PeriodicExportingMetricsReader
19
19
([ #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 ) )
20
22
21
23
## Version 1.23.0/0.44b0 (2024-02-23)
22
24
Original file line number Diff line number Diff line change @@ -988,13 +988,12 @@ def record_exception(
988
988
escaped : bool = False ,
989
989
) -> None :
990
990
"""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
+ )
998
997
_attributes = {
999
998
"exception.type" : exception .__class__ .__name__ ,
1000
999
"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):
1292
1292
finally :
1293
1293
self .assertEqual (len (span .events ), 0 )
1294
1294
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
+
1295
1312
1296
1313
def span_event_start_fmt (span_processor_name , span_name ):
1297
1314
return span_processor_name + ":" + span_name + ":start"
You can’t perform that action at this time.
0 commit comments