-
Notifications
You must be signed in to change notification settings - Fork 747
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fmt: Track duplicate spans in fmt context (#361)
## Motivation Entering twice in the same span causes the span to be lost after the second span got droped, see #358 ## Solution Duplicate spans are tracked in the `CONTEXT` thread local stack and ignored when finding the current span id. Spans are always pushed on the `CONTEXT` stack. This way, when a span is dropped, its ref count reflects the content of the thread local Fixes #358
- Loading branch information
Showing
2 changed files
with
104 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
mod support; | ||
use tracing::{self, subscriber::with_default, Span}; | ||
use tracing_subscriber::{filter::EnvFilter, FmtSubscriber}; | ||
|
||
#[test] | ||
fn duplicate_spans() { | ||
let subscriber = FmtSubscriber::builder() | ||
.with_env_filter(EnvFilter::new("[root]=debug")) | ||
.finish(); | ||
|
||
with_default(subscriber, || { | ||
let root = tracing::debug_span!("root"); | ||
root.in_scope(|| { | ||
// root: | ||
assert_eq!(root, Span::current(), "Current span must be 'root'"); | ||
let leaf = tracing::debug_span!("leaf"); | ||
leaf.in_scope(|| { | ||
// root:leaf: | ||
assert_eq!(leaf, Span::current(), "Current span must be 'leaf'"); | ||
root.in_scope(|| { | ||
// root:leaf: | ||
assert_eq!( | ||
leaf, | ||
Span::current(), | ||
"Current span must be 'leaf' after entering twice the 'root' span" | ||
); | ||
}) | ||
}); | ||
// root: | ||
assert_eq!( | ||
root, | ||
Span::current(), | ||
"Current span must be root ('leaf' exited, nested 'root' exited)" | ||
); | ||
|
||
root.in_scope(|| { | ||
assert_eq!(root, Span::current(), "Current span must be root"); | ||
}); | ||
// root: | ||
assert_eq!( | ||
root, | ||
Span::current(), | ||
"Current span must still be root after exiting nested 'root'" | ||
); | ||
}); | ||
}); | ||
} |