@@ -1104,25 +1104,31 @@ where
1104
1104
/// [`Span`]: opentelemetry::trace::Span
1105
1105
fn on_close ( & self , id : span:: Id , ctx : Context < ' _ , S > ) {
1106
1106
let span = ctx. span ( & id) . expect ( "Span not found, this is a bug" ) ;
1107
- let mut extensions = span. extensions_mut ( ) ;
1107
+ let ( otel_data, timings) = {
1108
+ let mut extensions = span. extensions_mut ( ) ;
1109
+ let timings = if self . tracked_inactivity {
1110
+ extensions. remove :: < Timings > ( )
1111
+ } else {
1112
+ None
1113
+ } ;
1114
+ ( extensions. remove :: < OtelData > ( ) , timings)
1115
+ } ;
1108
1116
1109
1117
if let Some ( OtelData {
1110
1118
mut builder,
1111
1119
parent_cx,
1112
- } ) = extensions . remove :: < OtelData > ( )
1120
+ } ) = otel_data
1113
1121
{
1114
- if self . tracked_inactivity {
1115
- // Append busy/idle timings when enabled.
1116
- if let Some ( timings) = extensions. get_mut :: < Timings > ( ) {
1117
- let busy_ns = Key :: new ( "busy_ns" ) ;
1118
- let idle_ns = Key :: new ( "idle_ns" ) ;
1122
+ // Append busy/idle timings when enabled.
1123
+ if let Some ( timings) = timings {
1124
+ let busy_ns = Key :: new ( "busy_ns" ) ;
1125
+ let idle_ns = Key :: new ( "idle_ns" ) ;
1119
1126
1120
- let attributes = builder
1121
- . attributes
1122
- . get_or_insert_with ( || Vec :: with_capacity ( 2 ) ) ;
1123
- attributes. push ( KeyValue :: new ( busy_ns, timings. busy ) ) ;
1124
- attributes. push ( KeyValue :: new ( idle_ns, timings. idle ) ) ;
1125
- }
1127
+ let attributes = builder
1128
+ . attributes
1129
+ . get_or_insert_with ( || Vec :: with_capacity ( 2 ) ) ;
1130
+ attributes. push ( KeyValue :: new ( busy_ns, timings. busy ) ) ;
1131
+ attributes. push ( KeyValue :: new ( idle_ns, timings. idle ) ) ;
1126
1132
}
1127
1133
1128
1134
// Build and start span, drop span to export
0 commit comments