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 4 commits
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
99 changes: 99 additions & 0 deletions tracing-subscriber/src/filter/subscriber_filters/mod.rs
Expand Up @@ -478,6 +478,36 @@ macro_rules! filter_impl_body {
fn max_level_hint(&self) -> Option<LevelFilter> {
self.deref().max_level_hint()
}

#[inline]
fn event_enabled(&self, event: &Event<'_>, cx: &Context<'_, S>) -> bool {
self.deref().event_enabled(event, cx)
}

#[inline]
fn on_new_span(&self, attrs: &span::Attributes<'_>, id: &span::Id, ctx: Context<'_, S>) {
self.deref().on_new_span(attrs, id, ctx)
}

#[inline]
fn on_record(&self, id: &span::Id, values: &span::Record<'_>, ctx: Context<'_, S>) {
self.deref().on_record(id, values, ctx)
}

#[inline]
fn on_enter(&self, id: &span::Id, ctx: Context<'_, S>) {
self.deref().on_enter(id, ctx)
}

#[inline]
fn on_exit(&self, id: &span::Id, ctx: Context<'_, S>) {
self.deref().on_exit(id, ctx)
}

#[inline]
fn on_close(&self, id: span::Id, ctx: Context<'_, S>) {
self.deref().on_close(id, ctx)
}
};
}

Expand All @@ -493,6 +523,75 @@ impl<S> subscribe::Filter<S> for Box<dyn subscribe::Filter<S> + Send + Sync + 's
filter_impl_body!();
}

// Implement Filter for Option<Filter> where None => allow
#[cfg(feature = "registry")]
#[cfg_attr(docsrs, doc(cfg(feature = "registry")))]
impl<F, S> subscribe::Filter<S> for Option<F>
where
F: subscribe::Filter<S>,
{
#[inline]
fn enabled(&self, meta: &Metadata<'_>, ctx: &Context<'_, S>) -> bool {
self.as_ref()
.map(|inner| inner.enabled(meta, ctx))
.unwrap_or(true)
}

#[inline]
fn callsite_enabled(&self, meta: &'static Metadata<'static>) -> Interest {
self.as_ref()
.map(|inner| inner.callsite_enabled(meta))
.unwrap_or_else(Interest::sometimes)
}

#[inline]
fn max_level_hint(&self) -> Option<LevelFilter> {
self.as_ref().and_then(|inner| inner.max_level_hint())
}

#[inline]
fn event_enabled(&self, event: &Event<'_>, ctx: &Context<'_, S>) -> bool {
self.as_ref()
.map(|inner| inner.event_enabled(event, ctx))
.unwrap_or(true)
}

#[inline]
fn on_new_span(&self, attrs: &span::Attributes<'_>, id: &span::Id, ctx: Context<'_, S>) {
if let Some(inner) = self {
inner.on_new_span(attrs, id, ctx)
}
}

#[inline]
fn on_record(&self, id: &span::Id, values: &span::Record<'_>, ctx: Context<'_, S>) {
if let Some(inner) = self {
inner.on_record(id, values, ctx)
}
}

#[inline]
fn on_enter(&self, id: &span::Id, ctx: Context<'_, S>) {
if let Some(inner) = self {
inner.on_enter(id, ctx)
}
}

#[inline]
fn on_exit(&self, id: &span::Id, ctx: Context<'_, S>) {
if let Some(inner) = self {
inner.on_exit(id, ctx)
}
}

#[inline]
fn on_close(&self, id: span::Id, ctx: Context<'_, S>) {
if let Some(inner) = self {
inner.on_close(id, ctx)
}
}
}

// === impl Filtered ===

impl<S, F, C> Filtered<S, F, C> {
Expand Down
10 changes: 6 additions & 4 deletions tracing-subscriber/src/subscribe/mod.rs
Expand Up @@ -464,11 +464,13 @@
//!
//! This crate's [`filter`] module provides a number of types which implement
//! the [`Filter`] trait, such as [`LevelFilter`], [`Targets`], and
//! [`FilterFn`]. These [`Filter`]s provide ready-made implementations of
//! common forms of filtering. For custom filtering policies, the [`FilterFn`]
//! and [`DynFilterFn`] types allow implementing a [`Filter`] with a closure or
//! [`FilterFn`]. These [`Filter`]s provide ready-made implementations of common
//! 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.
//! trait may also be implemented for user-defined types. [`Option<Filter>`]
//! also implements [`Filter`], to allow for a (surprise!) optional filter -
//! [`None`](Option::None) filters nothing (that is, allows everything through).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//! trait may also be implemented for user-defined types. [`Option<Filter>`]
//! also implements [`Filter`], to allow for a (surprise!) optional filter -
//! [`None`](Option::None) filters 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 filter where
//! [`None`](Option::None) filters out _nothing_ (that is, allows everything through).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aww, i like the (surprise!) :(

//!
//! <div class="example-wrap" style="display:inline-block">
//! <pre class="compile_fail" style="white-space:normal;font:inherit;">
Expand Down
1 change: 1 addition & 0 deletions tracing-subscriber/tests/subscriber_filters/main.rs
@@ -1,5 +1,6 @@
#![cfg(feature = "registry")]
mod filter_scopes;
mod option;
mod per_event;
mod targets;
mod trees;
Expand Down
40 changes: 40 additions & 0 deletions tracing-subscriber/tests/subscriber_filters/option.rs
@@ -0,0 +1,40 @@
use super::*;
use tracing_subscriber::{filter, prelude::*, Subscribe};

fn filter<S>() -> filter::DynFilterFn<S> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small nit, since it took me a second to understand what was happening here:

Suggested change
fn filter<S>() -> filter::DynFilterFn<S> {
fn filter_out_everything<S>() -> filter::DynFilterFn<S> {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto copied from boxed.rs.

// 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 = Box::new(subscribe.with_filter(Some(filter())));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: i'm not sure if Box is necessary, but if it is, then that's fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied boxed.rs as a starting point. I can unbox.


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 = Box::new(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();
}