-
Notifications
You must be signed in to change notification settings - Fork 734
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: don't use NoCollector as local placeholder (#2001)
## Motivation Currently, it is not actually possible to use `set_default(NoCollector)` or similar to temporarily disable the global default collector (see #1999). This is because `NoCollector` is currently used as a placeholder value when the thread-local cell that stores the current scoped default collector is initialized. Therefore, we currently check if the current scoped collector is `NoCollector`, and if it is, we fall back to returning the global default instead. This was fine, _when `NoCollector` was a private internal type only_. However, PR #1549 makes `NoCollector` into a public API type. When users can publicly construct `NoCollector` instances, it makes sense to want to be able to use `NoCollector` to disable the current collector. This is not possible when there is a global default set, because the local default being `NoCollector` will cause the global default to be returned. ## Solution This branch changes the thread-local cell to store an `Option<Dispatch>` instead, and use the `None` case to indicate no local default is set. This way, when the local default is explicitly set to `NoCollector`, we will return `NoCollector` rather than falling back. This may also be a slight performance improvement, because we now check if there's no global default by checking if the `Option` is `None`, rather than downcasting it to a `NoCollector`. I've also added a test reproducing #1999. Fixes #1999 Signed-off-by: Eliza Weisman <[email protected]>
- Loading branch information
Showing
2 changed files
with
57 additions
and
17 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,19 @@ | ||
#![cfg(feature = "std")] | ||
|
||
use tracing::collect; | ||
|
||
mod support; | ||
|
||
use self::support::*; | ||
|
||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] | ||
#[test] | ||
fn no_collector_disables_global() { | ||
// Reproduces https://github.com/tokio-rs/tracing/issues/1999 | ||
let (collector, handle) = collector::mock().done().run_with_handle(); | ||
collect::set_global_default(collector).expect("setting global default must succeed"); | ||
collect::with_default(collect::NoCollector::default(), || { | ||
tracing::info!("this should not be recorded"); | ||
}); | ||
handle.assert_finished(); | ||
} |