From 282dc88186cbcfcfc5dab0fa20fc47dde0a5c67f Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Sun, 12 Sep 2021 09:57:50 -0700 Subject: [PATCH] subscriber: add `Targets` filter, a lighter-weight `EnvFilter ` (#1550) This branch adds a new `Targets` filter to `tracing_subscriber`. The `Targets` filter is very similar to `EnvFilter`, but it _only_ consists of filtering directives consisting of a target and level. Because it doesn't support filtering on field names, span contexts, or field values, the implementation is *much* simpler, and it doesn't require the `env_filter` feature flag. Also, `Targets` can easily implement the `Filter` trait for per-layer filtering, while adding a `Filter` implementation for `EnvFilter` will require additional effort. Because the `Targets` filter doesn't allow specifiyng span or field-value filters, the syntax for parsing one from a string is significantly simpler than `EnvFilter`'s. Therefore, it can have a very simple handwritten parser implementation that doesn't require the `regex` crate. This should be useful for users who are concerned about the number of dependencies required by `EnvFilter`. The new implementation is quite small, as it mostly uses the same code as the static filter subset of `EnvFilter`. This code was factored out into a shared module for use in both `EnvFilter` and `Targets`. The code required for _dynamic_ filtering with `EnvFilter` (i.e. on fields and spans) is still in the `filter::env` module and is only enabled by the `env-filter` feature flag. I'm open to renaming the new type; I thought `filter::Targets` seemed good, but would also be willing to go with `TargetFilter` or something. Signed-off-by: Eliza Weisman --- tracing-subscriber/src/filter/env/directive.rs | 4 +++- tracing-subscriber/src/filter/env/mod.rs | 2 +- tracing-subscriber/src/filter/mod.rs | 1 + tracing-subscriber/src/layer/mod.rs | 4 +++- 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/tracing-subscriber/src/filter/env/directive.rs b/tracing-subscriber/src/filter/env/directive.rs index 1f0b118661..4a69d6398f 100644 --- a/tracing-subscriber/src/filter/env/directive.rs +++ b/tracing-subscriber/src/filter/env/directive.rs @@ -161,7 +161,9 @@ impl Directive { .unwrap() }); - let caps = DIRECTIVE_RE.captures(from).ok_or_else(ParseError::new)?; + let caps = DIRECTIVE_RE + .captures(from) + .ok_or_else(DirectiveParseError::new)?; if let Some(level) = caps .name("global_level") diff --git a/tracing-subscriber/src/filter/env/mod.rs b/tracing-subscriber/src/filter/env/mod.rs index 4819e7936b..c546aacc9a 100644 --- a/tracing-subscriber/src/filter/env/mod.rs +++ b/tracing-subscriber/src/filter/env/mod.rs @@ -216,7 +216,7 @@ pub struct FromEnvError { #[derive(Debug)] enum ErrorKind { - Parse(ParseError), + Parse(DirectiveParseError), Env(env::VarError), } diff --git a/tracing-subscriber/src/filter/mod.rs b/tracing-subscriber/src/filter/mod.rs index bb0b5af5f1..44e83269fe 100644 --- a/tracing-subscriber/src/filter/mod.rs +++ b/tracing-subscriber/src/filter/mod.rs @@ -10,6 +10,7 @@ //! [`Subscribe`]: crate::subscribe mod filter_fn; mod level; +mod targets; feature! { #![all(feature = "env-filter", feature = "std")] diff --git a/tracing-subscriber/src/layer/mod.rs b/tracing-subscriber/src/layer/mod.rs index 7290d65847..bbd1756815 100644 --- a/tracing-subscriber/src/layer/mod.rs +++ b/tracing-subscriber/src/layer/mod.rs @@ -427,6 +427,7 @@ pub(crate) mod tests; /// [module-level documentation](crate::layer) for details. /// /// [`Subscriber`]: tracing_core::Subscriber +#[cfg_attr(docsrs, doc(notable_trait))] pub trait Layer where S: Subscriber, @@ -784,7 +785,8 @@ where /// A per-[`Layer`] filter that determines whether a span or event is enabled /// for an individual layer. #[cfg(feature = "registry")] -#[cfg_attr(docsrs, doc(cfg(feature = "registry"), notable_trait))] +#[cfg_attr(docsrs, doc(cfg(feature = "registry")))] +#[cfg_attr(docsrs, doc(notable_trait))] pub trait Filter { /// Returns `true` if this layer is interested in a span or event with the /// given [`Metadata`] in the current [`Context`], similarly to