Skip to content

Commit

Permalink
subscriber: add Targets filter, a lighter-weight EnvFilter (toki…
Browse files Browse the repository at this point in the history
…o-rs#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 <eliza@buoyant.io>
  • Loading branch information
hawkw authored and kaffarell committed May 22, 2024
1 parent 20500bc commit 282dc88
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 3 deletions.
4 changes: 3 additions & 1 deletion tracing-subscriber/src/filter/env/directive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion tracing-subscriber/src/filter/env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ pub struct FromEnvError {

#[derive(Debug)]
enum ErrorKind {
Parse(ParseError),
Parse(DirectiveParseError),
Env(env::VarError),
}

Expand Down
1 change: 1 addition & 0 deletions tracing-subscriber/src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//! [`Subscribe`]: crate::subscribe
mod filter_fn;
mod level;
mod targets;

feature! {
#![all(feature = "env-filter", feature = "std")]
Expand Down
4 changes: 3 additions & 1 deletion tracing-subscriber/src/layer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S>
where
S: Subscriber,
Expand Down Expand Up @@ -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<S> {
/// Returns `true` if this layer is interested in a span or event with the
/// given [`Metadata`] in the current [`Context`], similarly to
Expand Down

0 comments on commit 282dc88

Please sign in to comment.