@@ -267,7 +267,7 @@ where
267
267
Ok ( ( ) )
268
268
}
269
269
270
- /// If `span_retrace` ensures that `new_span` is properly printed before an event
270
+ /// Ensures that `new_span` and all its ancestors are properly printed before an event
271
271
fn write_retrace_span < ' a , S > (
272
272
& self ,
273
273
new_span : & SpanRef < ' a , S > ,
@@ -276,61 +276,55 @@ where
276
276
) where
277
277
S : Subscriber + for < ' new_span > LookupSpan < ' new_span > ,
278
278
{
279
- let should_write = if self . config . deferred_spans {
280
- if let Some ( data) = new_span. extensions_mut ( ) . get_mut :: < Data > ( ) {
281
- !data. written
282
- } else {
283
- false
284
- }
285
- } else {
286
- false
287
- } ;
288
-
289
279
// Also handle deferred spans along with retrace since deferred spans may need to print
290
280
// multiple spans at once as a whole tree can be deferred
291
- if self . config . span_retrace || should_write {
292
- let old_span_id = bufs. current_span . replace ( ( new_span. id ( ) ) . clone ( ) ) ;
293
- let old_span_id = old_span_id. as_ref ( ) ;
294
-
295
- if Some ( & new_span. id ( ) ) != old_span_id {
296
- let old_span = old_span_id. as_ref ( ) . and_then ( |v| ctx. span ( v) ) ;
297
- let old_path = old_span. as_ref ( ) . map ( scope_path) . into_iter ( ) . flatten ( ) ;
298
-
299
- let new_path = scope_path ( new_span) ;
300
-
301
- // Print the path from the common base of the two spans
302
- let new_path = DifferenceIter :: new ( old_path, new_path, |v| v. id ( ) ) ;
303
-
304
- for ( i, span) in new_path. enumerate ( ) {
305
- // Mark traversed spans as *written*
306
- let was_written = if let Some ( data) = span. extensions_mut ( ) . get_mut :: < Data > ( ) {
307
- mem:: replace ( & mut data. written , true )
308
- } else {
309
- // `on_new_span` was not called, before
310
- // Consider if this should panic instead, which is *technically* correct but is
311
- // bad behavior for a logging layer in production.
312
- false
313
- } ;
314
-
315
- // Print the previous span before entering a new deferred or retraced span
316
- if i == 0 && self . config . verbose_entry {
317
- if let Some ( parent) = & span. parent ( ) {
318
- self . write_span_info ( parent, bufs, SpanMode :: PreOpen ) ;
319
- }
281
+ //
282
+ // If a another event occurs right after a previous event in the same span, this will
283
+ // simply print nothing since the path to the common lowest ancestor is empty
284
+ // if self.config.span_retrace || self.config.deferred_spans {
285
+ let old_span_id = bufs. current_span . replace ( ( new_span. id ( ) ) . clone ( ) ) ;
286
+ let old_span_id = old_span_id. as_ref ( ) ;
287
+ let new_span_id = new_span. id ( ) ;
288
+
289
+ if Some ( & new_span_id) != old_span_id {
290
+ let old_span = old_span_id. as_ref ( ) . and_then ( |v| ctx. span ( v) ) ;
291
+ let old_path = old_span. as_ref ( ) . map ( scope_path) . into_iter ( ) . flatten ( ) ;
292
+
293
+ let new_path = scope_path ( new_span) ;
294
+
295
+ // Print the path from the common base of the two spans
296
+ let new_path = DifferenceIter :: new ( old_path, new_path, |v| v. id ( ) ) ;
297
+
298
+ for ( i, span) in new_path. enumerate ( ) {
299
+ // Mark traversed spans as *written*
300
+ let was_written = if let Some ( data) = span. extensions_mut ( ) . get_mut :: < Data > ( ) {
301
+ mem:: replace ( & mut data. written , true )
302
+ } else {
303
+ // `on_new_span` was not called, before
304
+ // Consider if this should panic instead, which is *technically* correct but is
305
+ // bad behavior for a logging layer in production.
306
+ false
307
+ } ;
308
+
309
+ // Print the previous span before entering a new deferred or retraced span
310
+ if i == 0 && self . config . verbose_entry {
311
+ if let Some ( parent) = & span. parent ( ) {
312
+ self . write_span_info ( parent, bufs, SpanMode :: PreOpen ) ;
320
313
}
321
- let verbose = self . config . verbose_entry && i == 0 ;
322
-
323
- self . write_span_info (
324
- & span,
325
- bufs,
326
- if was_written {
327
- SpanMode :: Retrace { verbose }
328
- } else {
329
- SpanMode :: Open { verbose }
330
- } ,
331
- )
332
314
}
315
+ let verbose = self . config . verbose_entry && i == 0 ;
316
+
317
+ self . write_span_info (
318
+ & span,
319
+ bufs,
320
+ if was_written {
321
+ SpanMode :: Retrace { verbose }
322
+ } else {
323
+ SpanMode :: Open { verbose }
324
+ } ,
325
+ )
333
326
}
327
+ // }
334
328
}
335
329
}
336
330
@@ -491,22 +485,25 @@ where
491
485
492
486
let bufs = & mut * self . bufs . lock ( ) . unwrap ( ) ;
493
487
494
- // Store the most recently entered span
495
- bufs. current_span = Some ( span. id ( ) ) ;
496
-
497
488
if self . config . verbose_entry {
498
489
if let Some ( span) = span. parent ( ) {
499
490
self . write_span_info ( & span, bufs, SpanMode :: PreOpen ) ;
500
491
}
501
492
}
502
493
503
- self . write_span_info (
504
- & span,
505
- bufs,
506
- SpanMode :: Open {
507
- verbose : self . config . verbose_entry ,
508
- } ,
509
- ) ;
494
+ if self . config . span_retrace {
495
+ self . write_retrace_span ( & span, bufs, & ctx) ;
496
+ } else {
497
+ // Store the most recently entered span
498
+ bufs. current_span = Some ( span. id ( ) ) ;
499
+ self . write_span_info (
500
+ & span,
501
+ bufs,
502
+ SpanMode :: Open {
503
+ verbose : self . config . verbose_entry ,
504
+ } ,
505
+ ) ;
506
+ }
510
507
}
511
508
512
509
fn on_event ( & self , event : & Event < ' _ > , ctx : Context < S > ) {
@@ -518,7 +515,9 @@ where
518
515
let bufs = & mut * guard;
519
516
520
517
if let Some ( new_span) = & span {
521
- self . write_retrace_span ( new_span, bufs, & ctx) ;
518
+ if self . config . span_retrace || self . config . deferred_spans {
519
+ self . write_retrace_span ( new_span, bufs, & ctx) ;
520
+ }
522
521
}
523
522
524
523
let mut event_buf = & mut bufs. current_buf ;
0 commit comments