-
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.
The `Layer::on_layer` method on `Layer` was added in PR #1523. PR #1536, which added `Layer` implementations to `Box<dyn Layer<...> + ...>` and `Arc<dyn Layer<...> + ...>`, merged prior to #1523. However, when merging #1523, I didn't think to update the `Layer` impl for `Box`ed and `Arc`ed layers to forward `on_layer` to the inner `Layer`. This means that when a `Layer` is wrapped in an `Arc` or a `Box`, the `on_layer` method never gets called. When per-layer filters are in use, the `on_layer` method is necessary to ensure the filter is registered with the inner subscriber and has a valid ID. This bug means that when per-layer filters are wrapped in a `Box` or `Arc`, they won't be registered, and per-layer filtering breaks. This PR fixes the bug by adding `on_layer` implementations to the `Layer` impls for `Arc`ed and `Box`ed layers. I also added some tests --- thanks to @Noah-Kennedy for the original repro that these were based on (#1563 (comment)). I also added a nicer debug assertion to `Filtered` for cases where `Layer` impls don't call `on_layer`, so that this fails less confusingly in the future. Signed-off-by: Eliza Weisman <[email protected]>
- Loading branch information
Showing
4 changed files
with
114 additions
and
1 deletion.
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
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,73 @@ | ||
use super::*; | ||
use std::sync::Arc; | ||
use tracing_subscriber::{filter, prelude::*, Layer}; | ||
|
||
fn layer() -> (ExpectLayer, subscriber::MockHandle) { | ||
layer::mock().done().run_with_handle() | ||
} | ||
|
||
fn filter<S>() -> filter::DynFilterFn<S> { | ||
// Use dynamic filter fn to disable interest caching and max-level hints, | ||
// allowing us to put all of these tests in the same file. | ||
filter::dynamic_filter_fn(|_, _| false) | ||
} | ||
|
||
/// reproduces https://github.com/tokio-rs/tracing/issues/1563#issuecomment-921363629 | ||
#[test] | ||
fn box_works() { | ||
let (layer, handle) = layer(); | ||
let layer = Box::new(layer.with_filter(filter())); | ||
|
||
let _guard = tracing_subscriber::registry().with(layer).set_default(); | ||
|
||
for i in 0..2 { | ||
tracing::info!(i); | ||
} | ||
|
||
handle.assert_finished(); | ||
} | ||
|
||
/// the same as `box_works` but with a type-erased `Box`. | ||
#[test] | ||
fn dyn_box_works() { | ||
let (layer, handle) = layer(); | ||
let layer: Box<dyn Layer<_> + Send + Sync + 'static> = Box::new(layer.with_filter(filter())); | ||
|
||
let _guard = tracing_subscriber::registry().with(layer).set_default(); | ||
|
||
for i in 0..2 { | ||
tracing::info!(i); | ||
} | ||
|
||
handle.assert_finished(); | ||
} | ||
|
||
/// the same as `box_works` but with an `Arc`. | ||
#[test] | ||
fn arc_works() { | ||
let (layer, handle) = layer(); | ||
let layer = Box::new(layer.with_filter(filter())); | ||
|
||
let _guard = tracing_subscriber::registry().with(layer).set_default(); | ||
|
||
for i in 0..2 { | ||
tracing::info!(i); | ||
} | ||
|
||
handle.assert_finished(); | ||
} | ||
|
||
/// the same as `box_works` but with a type-erased `Arc`. | ||
#[test] | ||
fn dyn_arc_works() { | ||
let (layer, handle) = layer(); | ||
let layer: Arc<dyn Layer<_> + Send + Sync + 'static> = Arc::new(layer.with_filter(filter())); | ||
|
||
let _guard = tracing_subscriber::registry().with(layer).set_default(); | ||
|
||
for i in 0..2 { | ||
tracing::info!(i); | ||
} | ||
|
||
handle.assert_finished(); | ||
} |
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