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

subscriber: add combinators for combining filters #1578

Merged
merged 12 commits into from
Jan 24, 2022

Conversation

hawkw
Copy link
Member

@hawkw hawkw commented Sep 18, 2021

This isn't quite done yet, but I thought it was kinda cool.

@hawkw hawkw self-assigned this Sep 18, 2021
}

fn max_level_hint(&self) -> Option<LevelFilter> {
// TODO(eliza): figure this out???
Copy link
Member Author

Choose a reason for hiding this comment

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

this is the main reason this PR is still a draft

Copy link
Member

Choose a reason for hiding this comment

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

jeez, yeah, that really fucks with my brain. i'd maybe return None for now

Copy link
Member Author

Choose a reason for hiding this comment

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

The reason this is confusing is that I don't think we can simply return the "opposite" of the inner filter's hint, since the filter might be enabling things based on a particular characteristic other than levels, and we might still enable things of the same level. I think maybe we should just return None and have this break caching...though honestly, I'm not totally sure how necessary the Not filter even is...

Comment on lines +7 to +42
#[test]
fn and() {
let (layer, handle) = layer::mock()
.event(
event::msg("a very interesting event")
.at_level(tracing::Level::INFO)
.with_target("interesting_target"),
)
.done()
.run_with_handle();

// Enables spans and events with targets starting with `interesting_target`:
let target_filter = filter::filter_fn(|meta| meta.target().starts_with("interesting_target"));

// Enables spans and events with levels `INFO` and below:
let level_filter = LevelFilter::INFO;

// Combine the two filters together, returning a filter that only enables
// spans and events that *both* filters will enable:
let filter = target_filter.and(level_filter);

let _subscriber = tracing_subscriber::registry()
.with(layer.with_filter(filter))
.set_default();

// This event will *not* be enabled:
tracing::info!("an event with an uninteresting target");

// This event *will* be enabled:
tracing::info!(target: "interesting_target", "a very interesting event");

// This event will *not* be enabled:
tracing::debug!(target: "interesting_target", "interesting debug event...");

handle.assert_finished();
}
Copy link
Member Author

Choose a reason for hiding this comment

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

whoops this was supposed to be in a separate commit. lol whatever.

@hawkw
Copy link
Member Author

hawkw commented Sep 19, 2021

@davidbarsky any thoughts on whether the approach in a131037 or in 14e3a9b is better?

@hawkw
Copy link
Member Author

hawkw commented Sep 24, 2021

@davidbarsky ping

1 similar comment
@hawkw
Copy link
Member Author

hawkw commented Dec 1, 2021

@davidbarsky ping

@davidbarsky
Copy link
Member

@hawkw notes before i review the rest: very naively: I think I like the approach in 14e3a9b better, despite the fact that it might result in slightly worse compile times, but i'd be shocked if its noticable. that being said, what is the actual impact of a131037 in terms of combinator use?

tracing-subscriber/src/filter/layer_filters/combinator.rs Outdated Show resolved Hide resolved
tracing-subscriber/src/filter/layer_filters/combinator.rs Outdated Show resolved Hide resolved
tracing-subscriber/src/filter/layer_filters/combinator.rs Outdated Show resolved Hide resolved
tracing-subscriber/src/filter/layer_filters/combinator.rs Outdated Show resolved Hide resolved
tracing-subscriber/src/filter/layer_filters/combinator.rs Outdated Show resolved Hide resolved
}

fn max_level_hint(&self) -> Option<LevelFilter> {
// TODO(eliza): figure this out???
Copy link
Member

Choose a reason for hiding this comment

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

jeez, yeah, that really fucks with my brain. i'd maybe return None for now

hawkw and others added 9 commits December 22, 2021 09:27
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
apparently the `S` type param just can't ever be inferred when using the
combinators. that kicks ass lmao

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
...but i'm not sure if that's better or worse.

this fixes the type inference
issues and allows us to check that the combined layers are both
`Filter`s for the same subscriber. it also allows the `FilterExt` trait
to only be implemented for `Filter`s.

on the other hand, it means propagating a bunch of phantomdatas
everywhere.  this makes the code grosser and also takes longer for the
compiler to typecheck (though i doubt people will be nesting these
combinators deep enough to notice). it also means we can't derive traits
like `Debug` and `Clone`, and need manual implementations. which is
*fine*, it's just annoying...

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
thanks @davidbarsky

Co-authored-by: David Barsky <me@davidbarsky.com>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
@hawkw hawkw marked this pull request as ready for review December 22, 2021 17:33
@hawkw hawkw requested a review from a team as a code owner December 22, 2021 17:33
@hawkw hawkw changed the title [WIP] subscriber: add combinators for combining filters subscriber: add combinators for combining filters Jan 14, 2022
@hawkw hawkw merged commit 5ddbd4e into v0.1.x Jan 24, 2022
@hawkw hawkw deleted the eliza/filter-combinators branch January 24, 2022 22:05
hawkw added a commit that referenced this pull request Jan 25, 2022
# 0.3.7 (Jan 25, 2022)

This release adds combinators for combining filters.

Additionally, this release also updates the `thread-local` crate to
v1.1.4, fixing warnings for the security advisory [RUSTSEC-2022-0006].
Note that previous versions of `tracing-subscriber` did not use any of
the `thread-local` crate's APIs effected by the vulnerability. However,
updating the version fixes warnings emitted by `cargo audit` and similar
tools.

### Added

- **filter**: Added combinators for combining filters ([#1578])

### Fixed

- **registry**: Updated `thread-local` to v1.1.4 ([#1858])

Thanks to new contributor @matze for contributing to this release!

[RUSTSEC-2022-0006]: https://rustsec.org/advisories/RUSTSEC-2022-0006
[#1578]: #1578
[#1858]: #1858
hawkw added a commit that referenced this pull request Jan 25, 2022
# 0.3.7 (Jan 25, 2022)

This release adds combinators for combining filters.

Additionally, this release also updates the `thread-local` crate to
v1.1.4, fixing warnings for the security advisory [RUSTSEC-2022-0006].
Note that previous versions of `tracing-subscriber` did not use any of
the `thread-local` crate's APIs effected by the vulnerability. However,
updating the version fixes warnings emitted by `cargo audit` and similar
tools.

### Added

- **filter**: Added combinators for combining filters ([#1578])

### Fixed

- **registry**: Updated `thread-local` to v1.1.4 ([#1858])

Thanks to new contributor @matze for contributing to this release!

[RUSTSEC-2022-0006]: https://rustsec.org/advisories/RUSTSEC-2022-0006
[#1578]: #1578
[#1858]: #1858
kaffarell pushed a commit to kaffarell/tracing that referenced this pull request May 22, 2024
## Motivation

In some cases, it can be useful to express a more complex filtering
behavior by composing multiple filters. This is especially useful when
re-using the same logic in multiple filters.


## Solution

This branch adds a new `FilterExt` extension trait with combinators for
composing filters. The current set of combinators are `And` (enables
spans/events that are enabled by *both* filters), `Or` (enables any span
or event enabled by *either* filter), and `Not` (negate the output of a
filter).

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
kaffarell pushed a commit to kaffarell/tracing that referenced this pull request May 22, 2024
# 0.3.7 (Jan 25, 2022)

This release adds combinators for combining filters.

Additionally, this release also updates the `thread-local` crate to
v1.1.4, fixing warnings for the security advisory [RUSTSEC-2022-0006].
Note that previous versions of `tracing-subscriber` did not use any of
the `thread-local` crate's APIs effected by the vulnerability. However,
updating the version fixes warnings emitted by `cargo audit` and similar
tools.

### Added

- **filter**: Added combinators for combining filters ([tokio-rs#1578])

### Fixed

- **registry**: Updated `thread-local` to v1.1.4 ([tokio-rs#1858])

Thanks to new contributor @matze for contributing to this release!

[RUSTSEC-2022-0006]: https://rustsec.org/advisories/RUSTSEC-2022-0006
[tokio-rs#1578]: tokio-rs#1578
[tokio-rs#1858]: tokio-rs#1858
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants