@@ -4,7 +4,7 @@ use crate::OtelData;
4
4
#[ cfg( feature = "activate_context" ) ]
5
5
use opentelemetry:: ContextGuard ;
6
6
use opentelemetry:: {
7
- trace:: { self as otel, noop, SpanBuilder , SpanKind , Status , TraceContextExt } ,
7
+ trace:: { self as otel, noop, Span , SpanBuilder , SpanKind , Status , TraceContextExt } ,
8
8
Context as OtelContext , Key , KeyValue , StringValue , Value ,
9
9
} ;
10
10
#[ cfg( feature = "activate_context" ) ]
@@ -1319,30 +1319,31 @@ where
1319
1319
end_time,
1320
1320
} ) = otel_data
1321
1321
{
1322
- let cx = if let Some ( builder) = builder {
1323
- let span = builder. start_with_context ( & self . tracer , & parent_cx) ;
1324
- parent_cx. with_span ( span)
1325
- } else {
1326
- parent_cx
1327
- } ;
1328
-
1329
- let span = cx. span ( ) ;
1330
1322
// Append busy/idle timings when enabled.
1331
- if let Some ( timings) = timings {
1323
+ let timings = timings. and_then ( |timings| {
1332
1324
let busy_ns = Key :: new ( "busy_ns" ) ;
1333
1325
let idle_ns = Key :: new ( "idle_ns" ) ;
1334
1326
1335
- span . set_attributes ( vec ! [
1327
+ Some ( vec ! [
1336
1328
KeyValue :: new( busy_ns, timings. busy) ,
1337
1329
KeyValue :: new( idle_ns, timings. idle) ,
1338
- ] ) ;
1339
- }
1330
+ ] )
1331
+ } ) ;
1340
1332
1341
- if let Some ( end_time) = end_time {
1342
- span. end_with_timestamp ( end_time) ;
1333
+ if let Some ( builder) = builder {
1334
+ // Don't create the context here just to get a SpanRef since it's costly
1335
+ let mut span = builder. start_with_context ( & self . tracer , & parent_cx) ;
1336
+ timings. map ( |timings| span. set_attributes ( timings) ) ;
1337
+ if let Some ( end_time) = end_time {
1338
+ span. end_with_timestamp ( end_time) ;
1339
+ } else {
1340
+ span. end ( ) ;
1341
+ }
1343
1342
} else {
1344
- span. end ( ) ;
1345
- }
1343
+ let span = parent_cx. span ( ) ;
1344
+ timings. map ( |timings| span. set_attributes ( timings) ) ;
1345
+ end_time. map_or_else ( || span. end ( ) , |end_time| span. end_with_timestamp ( end_time) ) ;
1346
+ } ;
1346
1347
}
1347
1348
}
1348
1349
0 commit comments