@@ -265,6 +265,84 @@ where
265
265
Ok ( ( ) )
266
266
}
267
267
268
+ /// If `span_retrace` ensures that `new_span` is properly printed before an event
269
+ fn write_retrace_span < ' a , S > (
270
+ & self ,
271
+ new_span : & SpanRef < ' a , S > ,
272
+ bufs : & mut Buffers ,
273
+ ctx : & ' a Context < S > ,
274
+ ) where
275
+ S : Subscriber + for < ' new_span > LookupSpan < ' new_span > ,
276
+ {
277
+ {
278
+ // let was_written = if let Some(data) = new_span.extensions_mut().get_mut::<Data>() {
279
+ // let written = data.written;
280
+ // data.written = true;
281
+ // written
282
+ // } else {
283
+ // eprintln!("Span not written");
284
+ // false
285
+ // };
286
+
287
+ let should_write = if self . config . deferred_spans {
288
+ if let Some ( data) = new_span. extensions_mut ( ) . get_mut :: < Data > ( ) {
289
+ !data. written
290
+ } else {
291
+ false
292
+ }
293
+ } else {
294
+ false
295
+ } ;
296
+
297
+ if self . config . span_retrace || should_write {
298
+ let was_written = if let Some ( data) = new_span. extensions_mut ( ) . get_mut :: < Data > ( ) {
299
+ mem:: replace ( & mut data. written , true )
300
+ } else {
301
+ // `on_new_span` was not called, before
302
+ // Consider if this should panic instead, which is *technically* correct but is
303
+ // bad behavior for a logging layer in production.
304
+ false
305
+ } ;
306
+
307
+ let old_span_id = bufs. current_span . replace ( ( new_span. id ( ) ) . clone ( ) ) ;
308
+ let old_span_id = old_span_id. as_ref ( ) ;
309
+
310
+ if Some ( & new_span. id ( ) ) != old_span_id {
311
+ let old_span = old_span_id. as_ref ( ) . and_then ( |v| ctx. span ( v) ) ;
312
+
313
+ // Print the previous span before entering a new deferred or retraced span
314
+ if self . config . verbose_entry {
315
+ if let Some ( old_span) = & old_span {
316
+ self . write_span_info ( old_span, bufs, ctx, SpanMode :: PreOpen ) ;
317
+ }
318
+ }
319
+
320
+ let old_path = old_span. as_ref ( ) . map ( scope_path) . into_iter ( ) . flatten ( ) ;
321
+
322
+ let new_path = scope_path ( new_span) ;
323
+
324
+ // Print the path from the common base of the two spans
325
+ let new_path = DifferenceIter :: new ( old_path, new_path, |v| v. id ( ) ) ;
326
+
327
+ for span in new_path {
328
+ self . write_span_info (
329
+ & span,
330
+ bufs,
331
+ ctx,
332
+ if was_written {
333
+ SpanMode :: Retrace
334
+ } else {
335
+ SpanMode :: Open {
336
+ verbose : self . config . verbose_entry ,
337
+ }
338
+ } ,
339
+ )
340
+ }
341
+ }
342
+ }
343
+ }
344
+ }
345
+
268
346
fn write_span_info < S > (
269
347
& self ,
270
348
span : & SpanRef < S > ,
@@ -274,10 +352,6 @@ where
274
352
) where
275
353
S : Subscriber + for < ' span > LookupSpan < ' span > ,
276
354
{
277
- // let span = ctx
278
- // .span(id)
279
- // .expect("in on_enter/on_exit but span does not exist");
280
-
281
355
let ext = span. extensions ( ) ;
282
356
let data = ext. get :: < Data > ( ) . expect ( "span does not have data" ) ;
283
357
@@ -294,7 +368,8 @@ where
294
368
// Print the parent of a new span again before entering the child
295
369
SpanMode :: PreOpen if self . config . verbose_entry => true ,
296
370
SpanMode :: Close { verbose } => verbose,
297
- SpanMode :: Retrace { verbose } => verbose,
371
+ // Generated if `span_retrace` is enabled
372
+ SpanMode :: Retrace => true ,
298
373
// Generated if `verbose_exit` is enabled
299
374
SpanMode :: PostClose => true ,
300
375
_ => false ,
@@ -388,51 +463,13 @@ where
388
463
fn on_event ( & self , event : & Event < ' _ > , ctx : Context < S > ) {
389
464
let span = ctx. current_span ( ) ;
390
465
let span_id = span. id ( ) ;
391
- let span = span_id. and_then ( |id| ctx. span ( id) . map ( |span| ( id , span ) ) ) ;
466
+ let span = span_id. and_then ( |id| ctx. span ( id) ) ;
392
467
393
468
let mut guard = self . bufs . lock ( ) . unwrap ( ) ;
394
469
let bufs = & mut * guard;
395
470
396
- if let Some ( ( span_id, current_span) ) = & span {
397
- if self . config . span_retrace
398
- || current_span
399
- . extensions ( )
400
- . get :: < Data > ( )
401
- . is_some_and ( |v| !v. written )
402
- {
403
- if let Some ( data) = current_span. extensions_mut ( ) . get_mut :: < Data > ( ) {
404
- data. written = true ;
405
- }
406
-
407
- let old_span_id = bufs. current_span . replace ( ( * span_id) . clone ( ) ) ;
408
-
409
- if Some ( * span_id) != old_span_id. as_ref ( ) {
410
- let old_span = old_span_id. as_ref ( ) . and_then ( |v| ctx. span ( v) ) ;
411
-
412
- // eprintln!(
413
- // "concurrent old: {:?}, new: {:?}",
414
- // old_span.as_ref().map(|v| v.metadata().name()),
415
- // current_span.metadata().name()
416
- // );
417
-
418
- let old_path = old_span. as_ref ( ) . map ( scope_path) . into_iter ( ) . flatten ( ) ;
419
- let new_path = scope_path ( current_span) ;
420
-
421
- // Print the path from the common base of the two spans
422
- let new_path = DifferenceIter :: new ( old_path, new_path, |v| v. id ( ) ) ;
423
-
424
- for span in new_path {
425
- self . write_span_info (
426
- & span,
427
- bufs,
428
- & ctx,
429
- SpanMode :: Retrace {
430
- verbose : self . config . span_retrace ,
431
- } ,
432
- )
433
- }
434
- }
435
- }
471
+ if let Some ( new_span) = & span {
472
+ self . write_retrace_span ( new_span, bufs, & ctx) ;
436
473
}
437
474
438
475
let mut event_buf = & mut bufs. current_buf ;
@@ -461,7 +498,7 @@ where
461
498
// check if this event occurred in the context of a span.
462
499
// if it has, get the start time of this span.
463
500
let start = match span {
464
- Some ( ( id , span) ) => {
501
+ Some ( span) => {
465
502
// if the event is in a span, get the span's starting point.
466
503
let ext = span. extensions ( ) ;
467
504
let data = ext
@@ -534,7 +571,7 @@ where
534
571
535
572
// Store the most recently entered span
536
573
if bufs. current_span . as_ref ( ) == Some ( & id) {
537
- bufs. current_span = None ;
574
+ // bufs.current_span = None;
538
575
}
539
576
540
577
let span = ctx. span ( & id) . expect ( "invalid span in on_close" ) ;
@@ -544,6 +581,8 @@ where
544
581
&& span. extensions ( ) . get :: < Data > ( ) . map ( |v| v. written ) != Some ( true )
545
582
{ }
546
583
584
+ self . write_retrace_span ( & span, bufs, & ctx) ;
585
+
547
586
self . write_span_info (
548
587
& span,
549
588
bufs,
0 commit comments