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: better default filtering #336

Merged
merged 12 commits into from Sep 10, 2019
Merged

subscriber: better default filtering #336

merged 12 commits into from Sep 10, 2019

Conversation

hawkw
Copy link
Member

@hawkw hawkw commented Sep 9, 2019

Feature Request

Crates

  • tracing-subscriber

Motivation

Currently, the tracing-subscriber FmtSubscriber::default
implementation defaults to no filtering. This means that all
instrumentation, including extremely verbose trace-level diagnostics
from crates like tokio are enabled by default.

This is because the subscriber itself does not implement filtering, in
order to allow it to be composed with filters implemented by Layers.
However, defaulting to no filtering at all is definitely a surprising
behavior. I didn't want to conditionally return a different type based
on whether or not filtering was enabled by the filter feature flag,
but this is probably not worth the confusion introduced by this
behavior. We should make this more intuitive.

Solution

This branch changes tracing-subscriber to default to enabling
the INFO level and above. If the filter feature flag is enabled,
users may opt-in to env_logger-style filtering. Additionally,
regardless of feature flags, a new with_max_level method is
added to the FmtSubscriber builder, which takes a Level or
LevelFilter. LevelFilter now implements Layer by enabling
any spans and events that are less than or equal to that Level.

Fixes: #331
Fixes: #332

@hawkw hawkw added the kind/feature New feature or request label Sep 9, 2019
@hawkw hawkw requested review from Ralith, davidbarsky and a team September 9, 2019 22:41
@hawkw hawkw self-assigned this Sep 9, 2019
@github-actions github-actions bot added the crate/subscriber Related to the `tracing-subscriber` crate label Sep 9, 2019
@Ralith
Copy link
Collaborator

Ralith commented Sep 9, 2019

I'm a bit confused by the description and change here. The behavior I'd like to see is that, regardless of whether the filter feature is enabled or not, the FmtSubscriber would display all messages at info level or above. To respect RUST_LOG, a user would have to opt in to it by explicitly passing in Filter::from_default_env(). This ensures that neither types nor default behavior change solely because a feature flag is enabled, which should minimize unpleasant surprises.

In addition to the high potential surprise, it looks like this change preserves the default behavior of displaying everything, which is practically unusable if you're using tokio (or any hypothetical other library that contains many trace statements).

@Ralith
Copy link
Collaborator

Ralith commented Sep 9, 2019

To clarify, my suggestion was that the default FmtSubscriber not use Filter at all, but some other filtering implementation which would hopefully be simple enough not to require any form of feature gating.

@hawkw
Copy link
Member Author

hawkw commented Sep 10, 2019

To clarify, my suggestion was that the default FmtSubscriber not use Filter at all, but some other filtering implementation which would hopefully be simple enough not to require any form of feature gating.

Ah, I misunderstood your suggestion. Thanks for clarifying that. This makes significantly more sense.

This changes the `FmtSubscriber` type to default to using
`Filter::from_default_env` when the filter feature flag is enabled. Rather than conditionally producing a layered
or un-layered subscriber, we now wrap the layered type with a new type
implementing `Subscriber` to make the API more consistent across
feature flags.

Closes #331

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>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
@hawkw
Copy link
Member Author

hawkw commented Sep 10, 2019

@Ralith I've changed this up a bit so that it actually implements the behavior you suggested (which, IMO, makes significantly more sense). The default max level is now always INFO; env-logger filtering is never used by default. Users can set a different max level even without the filter feature enabled, or can change to an env-logger style filter.

Let me know what you think now?

Copy link
Collaborator

@Ralith Ralith left a comment

Choose a reason for hiding this comment

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

Looks awesome, thanks! I think this will make the stack a lot more approachable.

It might be clearer to rename the type (and feature) from Filter to EnvFilter or similar, but that can be handled independently of this change.

@hawkw
Copy link
Member Author

hawkw commented Sep 10, 2019 via email

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>
@hawkw hawkw merged commit b385da6 into master Sep 10, 2019
@hawkw hawkw deleted the eliza/default-filters branch September 10, 2019 23:04
hawkw added a commit that referenced this pull request Sep 12, 2019
This branch renames `Filter` to `EnvFilter` and deprecates the previous
name, as suggested in #336 (review).
This should make the difference between an `EnvFilter` and a
`LevelFilter` clearer.
The `filter` feature has also been deprecated in favor of `env-filter`.

Co-Authored-By: Benjamin Saunders <ben.e.saunders@gmail.com>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
hawkw added a commit that referenced this pull request Sep 12, 2019
Fixed:

- `EnvFilter` ignoring directives with targets that are the same number
  of characters (#333)
- `EnvFilter` failing to properly apply filter directives to events
  generated from `log` records by`tracing-log` (#344)

Changed:

- Renamed `Filter` to `EnvFilter`, deprecated `Filter` (#339)
- Renamed "filter" feature flag to "env-filter", deprecated "filter" (#339)
- `FmtSubscriber` now defaults to enabling only the `INFO` level and
  above when a max level filter or `EnvFilter` is not set (#336)

Added:

- `EnvFilter::add_directive` to add new directives to filters after they
  are constructed (#334)
- `fmt::Builder::with_max_level` to set a global level filter for a
  `FmtSubscriber` without requiring the use of `EnvFilter` (#336)
- `Layer` implementation for `LevelFilter` (#336)
- `EnvFilter` now implements `fmt::Display` (#329)

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
hawkw added a commit that referenced this pull request Sep 13, 2019
Fixed:

- `EnvFilter` ignoring directives with targets that are the same number
  of characters (#333)
- `EnvFilter` failing to properly apply filter directives to events
  generated from `log` records by`tracing-log` (#344)

Changed:

- Renamed `Filter` to `EnvFilter`, deprecated `Filter` (#339)
- Renamed "filter" feature flag to "env-filter", deprecated "filter" (#339)
- `FmtSubscriber` now defaults to enabling only the `INFO` level and
  above when a max level filter or `EnvFilter` is not set (#336)
- Made `parking_lot` dependency an opt-in feature flag (#348)

Added:

- `EnvFilter::add_directive` to add new directives to filters after they
  are constructed (#334)
- `fmt::Builder::with_max_level` to set a global level filter for a
  `FmtSubscriber` without requiring the use of `EnvFilter` (#336)
- `Layer` implementation for `LevelFilter` (#336)
- `EnvFilter` now implements `fmt::Display` (#329)

Removed:

- Removed dependency on `crossbeam-util` (#348)

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
hawkw added a commit that referenced this pull request Sep 13, 2019
### Fixed

- `EnvFilter` ignoring directives with targets that are the same number
  of characters (#333)
- `EnvFilter` failing to properly apply filter directives to events
  generated from `log` records by`tracing-log` (#344)

### Changed

- Renamed `Filter` to `EnvFilter`, deprecated `Filter` (#339)
- Renamed "filter" feature flag to "env-filter", deprecated "filter" (#339)
- `FmtSubscriber` now defaults to enabling only the `INFO` level and
  above when a max level filter or `EnvFilter` is not set (#336)

### Added:

- `EnvFilter::add_directive` to add new directives to filters after they
  are constructed (#334)
- `fmt::Builder::with_max_level` to set a global level filter for a
  `FmtSubscriber` without requiring the use of `EnvFilter` (#336)
- `Layer` implementation for `LevelFilter` (#336)
- `EnvFilter` now implements `fmt::Display` (#329)

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crate/subscriber Related to the `tracing-subscriber` crate kind/feature New feature or request
Projects
None yet
2 participants