-
Notifications
You must be signed in to change notification settings - Fork 734
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement subscribe::Filter for Option<Filter> #2407
Changes from 11 commits
8919fa0
00e304b
5540321
38f5416
74f94ee
bca61af
b447e17
7a6bb31
8568d96
1b7b671
59cba8c
dfd857a
29d15d4
883c594
01be443
6507da9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// A separate test crate for `Option<Filter>` for isolation from other tests | ||
// that may influence the interest cache. | ||
|
||
use std::sync::{ | ||
atomic::{AtomicUsize, Ordering}, | ||
Arc, | ||
}; | ||
use tracing_mock::{expect, subscriber}; | ||
use tracing_subscriber::{filter, prelude::*, Subscribe}; | ||
|
||
/// A `None` filter should always be interested in events, and it should not | ||
/// needlessly degrade the caching of other filters. | ||
#[test] | ||
fn none_interest_cache() { | ||
let (subscribe_none, handle_none) = subscriber::mock() | ||
.event(expect::event()) | ||
.event(expect::event()) | ||
.only() | ||
.run_with_handle(); | ||
let subscribe_none = subscribe_none.with_filter(None::<filter::DynFilterFn<_>>); | ||
|
||
let times_filtered = Arc::new(AtomicUsize::new(0)); | ||
let (subscribe_filter_fn, handle_filter_fn) = subscriber::mock() | ||
.event(expect::event()) | ||
.event(expect::event()) | ||
.only() | ||
.run_with_handle(); | ||
let subscribe_filter_fn = subscribe_filter_fn.with_filter(filter::filter_fn({ | ||
let times_filtered = Arc::clone(×_filtered); | ||
move |_| { | ||
times_filtered.fetch_add(1, Ordering::Relaxed); | ||
true | ||
} | ||
})); | ||
|
||
let subscriber = tracing_subscriber::registry() | ||
.with(subscribe_none) | ||
.with(subscribe_filter_fn); | ||
|
||
let _guard = subscriber.set_default(); | ||
for _ in 0..2 { | ||
tracing::debug!(target: "always_interesting", x="bar"); | ||
} | ||
|
||
// The `None` filter is unchanging and performs no filtering, so it should | ||
// be cacheable and always be interested in events. The filter fn is a | ||
// non-dynamic filter fn, which means the result can be cached per callsite. | ||
// The filter fn should only need to be called once, and the `Option` filter | ||
// should not interfere in the caching of that result. | ||
assert_eq!(times_filtered.load(Ordering::Relaxed), 1); | ||
handle_none.assert_finished(); | ||
handle_filter_fn.assert_finished(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#![cfg(feature = "registry")] | ||
mod filter_scopes; | ||
mod option; | ||
mod per_event; | ||
mod targets; | ||
mod trees; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
use super::*; | ||
use tracing::Collect; | ||
use tracing_subscriber::{ | ||
filter::{self, LevelFilter}, | ||
prelude::*, | ||
Subscribe, | ||
}; | ||
|
||
fn filter_out_everything<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) | ||
} | ||
|
||
#[test] | ||
fn option_some() { | ||
let (subscribe, handle) = subscriber::mock().only().run_with_handle(); | ||
let subscribe = subscribe.with_filter(Some(filter_out_everything())); | ||
|
||
let _guard = tracing_subscriber::registry().with(subscribe).set_default(); | ||
|
||
for i in 0..2 { | ||
tracing::info!(i); | ||
} | ||
|
||
handle.assert_finished(); | ||
} | ||
|
||
#[test] | ||
fn option_none() { | ||
let (subscribe, handle) = subscriber::mock() | ||
.event(expect::event()) | ||
.event(expect::event()) | ||
.only() | ||
.run_with_handle(); | ||
let subscribe = subscribe.with_filter(None::<filter::DynFilterFn<_>>); | ||
|
||
let _guard = tracing_subscriber::registry().with(subscribe).set_default(); | ||
|
||
for i in 0..2 { | ||
tracing::info!(i); | ||
} | ||
|
||
handle.assert_finished(); | ||
} | ||
|
||
#[test] | ||
fn option_mixed() { | ||
let (subscribe, handle) = subscriber::mock() | ||
.event(expect::event()) | ||
.only() | ||
.run_with_handle(); | ||
let subscribe = subscribe | ||
.with_filter(filter::dynamic_filter_fn(|meta, _ctx| { | ||
meta.target() == "interesting" | ||
})) | ||
.with_filter(None::<filter::DynFilterFn<_>>); | ||
Comment on lines
+49
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is worth having, but the scenario i really wanted to test was not a single subscriber with two different filters, but two different subscribers with separate filters, where one subscriber's filter was a we also really want to test that the correct max level hint is used, since that's something we've had bugs with in the past. |
||
|
||
let _guard = tracing_subscriber::registry().with(subscribe).set_default(); | ||
|
||
tracing::info!(target: "interesting", x="foo"); | ||
tracing::info!(target: "boring", x="bar"); | ||
|
||
handle.assert_finished(); | ||
} | ||
|
||
/// The lack of a max level hint from a `None` filter should result in no max | ||
/// level hint when combined with other filters/subscribers. | ||
#[test] | ||
fn none_max_level_hint() { | ||
let (subscribe_none, handle_none) = subscriber::mock() | ||
.event(expect::event()) | ||
.event(expect::event()) | ||
.only() | ||
.run_with_handle(); | ||
let subscribe_none = subscribe_none.with_filter(None::<filter::DynFilterFn<_>>); | ||
assert!(subscribe_none.max_level_hint().is_none()); | ||
|
||
let (subscribe_filter_fn, handle_filter_fn) = subscriber::mock() | ||
.event(expect::event()) | ||
.only() | ||
.run_with_handle(); | ||
let max_level = Level::INFO; | ||
let subscribe_filter_fn = subscribe_filter_fn.with_filter( | ||
filter::dynamic_filter_fn(move |meta, _| return meta.level() <= &max_level) | ||
.with_max_level_hint(max_level), | ||
); | ||
assert_eq!( | ||
subscribe_filter_fn.max_level_hint(), | ||
Some(LevelFilter::INFO) | ||
); | ||
|
||
let subscriber = tracing_subscriber::registry() | ||
.with(subscribe_none) | ||
.with(subscribe_filter_fn); | ||
// The absence of a hint from the `None` filter upgrades the `INFO` hint | ||
// from the filter fn subscriber. | ||
assert!(subscriber.max_level_hint().is_none()); | ||
|
||
let _guard = subscriber.set_default(); | ||
tracing::info!(target: "interesting", x="foo"); | ||
tracing::debug!(target: "sometimes_interesting", x="bar"); | ||
|
||
handle_none.assert_finished(); | ||
handle_filter_fn.assert_finished(); | ||
} | ||
|
||
/// The max level hint from inside a `Some(filter)` filter should be propagated | ||
/// and combined with other filters/subscribers. | ||
#[test] | ||
fn some_max_level_hint() { | ||
let (subscribe_some, handle_some) = subscriber::mock() | ||
.event(expect::event()) | ||
.event(expect::event()) | ||
.only() | ||
.run_with_handle(); | ||
let subscribe_some = subscribe_some.with_filter(Some( | ||
filter::dynamic_filter_fn(move |meta, _| return meta.level() <= &Level::DEBUG) | ||
.with_max_level_hint(Level::DEBUG), | ||
)); | ||
assert_eq!(subscribe_some.max_level_hint(), Some(LevelFilter::DEBUG)); | ||
|
||
let (subscribe_filter_fn, handle_filter_fn) = subscriber::mock() | ||
.event(expect::event()) | ||
.only() | ||
.run_with_handle(); | ||
let subscribe_filter_fn = subscribe_filter_fn.with_filter( | ||
filter::dynamic_filter_fn(move |meta, _| return meta.level() <= &Level::INFO) | ||
.with_max_level_hint(Level::INFO), | ||
); | ||
assert_eq!( | ||
subscribe_filter_fn.max_level_hint(), | ||
Some(LevelFilter::INFO) | ||
); | ||
|
||
let subscriber = tracing_subscriber::registry() | ||
.with(subscribe_some) | ||
.with(subscribe_filter_fn); | ||
// The `DEBUG` hint from the `Some` filter upgrades the `INFO` hint from the | ||
// filter fn subscriber. | ||
assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::DEBUG)); | ||
|
||
let _guard = subscriber.set_default(); | ||
tracing::info!(target: "interesting", x="foo"); | ||
tracing::debug!(target: "sometimes_interesting", x="bar"); | ||
|
||
handle_some.assert_finished(); | ||
handle_filter_fn.assert_finished(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit, not a blocker: it could be nice to link to the implementation of
Filter
forOption<Filter>
in the docs here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the rustdoc syntax to link to a trait implementation?