Skip to content
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

Merged
merged 16 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 19 additions & 4 deletions tracing-subscriber/src/subscribe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,10 +468,25 @@
//! forms of filtering. For custom filtering policies, the [`FilterFn`] and
//! [`DynFilterFn`] types allow implementing a [`Filter`] with a closure or
//! function pointer. In addition, when more control is required, the [`Filter`]
//! trait may also be implemented for user-defined types. [`Option<Filter>`]
//! also implements [`Filter`], which allows for an optional filter where
//! [`None`](Option::None) filters out _nothing_ (that is, allows everything
//! through).
//! trait may also be implemented for user-defined types.
//!
//! [`Option<Filter>`] also implements [`Filter`], which allows for an optional
Copy link
Member

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 for Option<Filter> in the docs here?

Copy link
Contributor Author

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?

//! filter. [`None`](Option::None) filters out _nothing_ (that is, allows
//! everything through). For example:
//!
//! ```rust
//! # use tracing_subscriber::{filter::filter_fn, Subscribe};
//! # use tracing_core::{Metadata, collect::Collect};
//! # struct MySubscriber<C>(std::marker::PhantomData<C>);
//! # impl<C> MySubscriber<C> { fn new() -> Self { Self(std::marker::PhantomData)} }
//! # impl<C: Collect> Subscribe<C> for MySubscriber<C> {}
//! # fn my_filter(_: &str) -> impl Fn(&Metadata) -> bool { |_| true }
//! fn setup_tracing<C: Collect>(filter_config: Option<&str>) {
//! let layer = MySubscriber::<C>::new()
//! .with_filter(filter_config.map(|config| filter_fn(my_filter(config))));
//! //...
//! }
//! ```
//!
//! <div class="example-wrap" style="display:inline-block">
//! <pre class="compile_fail" style="white-space:normal;font:inherit;">
Expand Down
22 changes: 21 additions & 1 deletion tracing-subscriber/tests/subscriber_filters/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn option_none() {
.event(expect::event())
.only()
.run_with_handle();
let subscribe = Box::new(subscribe.with_filter(None::<filter::DynFilterFn<_>>));
let subscribe = subscribe.with_filter(None::<filter::DynFilterFn<_>>);

let _guard = tracing_subscriber::registry().with(subscribe).set_default();

Expand All @@ -38,3 +38,23 @@ fn option_none() {

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
Copy link
Member

Choose a reason for hiding this comment

The 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 None. in particular, we want to assert that the None filter does not inadvertantly enable anything that the other subscriber's filter would enable.

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();
}