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: added builder for filter Directive #3018

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rustonaut
Copy link

@rustonaut rustonaut commented Jun 24, 2024

Motivation

  1. There are some filters which can not be created with the current Directive string repr syntax, like e.g. the strings bool, 1 or 2.3 or values containing characters including but not limited to }], (for some of this there are partial fixes in other PRS). Furthermore updating the syntax the accommodate this is always a potential braking change, one which also can be subtle to detect.

  2. Sometimes there is need to create Directives programmatically, e.g. from a different kind of logging config or to set more complicated defaults.

  3. We might want to add other matching capabilities in the future which might not be re presentable in the string syntax (or at least entail a braking change). Like e.g. matching fields regex, matching based on dyn Value, a field matching the string true and boolean true at the same time, etc.

By allowing programmatic creation of Directives we allow downstream users to work around existing issues, experiment with solution to such issues in external crates and in addition set the foundation for adding future matching capabilities in the future.

Solution

  • adds Directive::builder() / DirectiveBuilder
  • ValueMatch is now public as we need it in the builder. But we wrapped the current implementation instead of exposing it so that we can freely change/extend it in the future.

Cavets

  • as we now can create filters which can not be represented with the parser (without making a breaking change to the parsing format) we no longer have the guarantee that a Display <=> FromStr round trip works

Out of Scope

  • currently the only way to add build Directives is by using EnvFilter::add_directive, which means there always is "some" parsed or default directive in the EnvFilter before we can add our directives. We could e.g. impl FromIterator<Directive> for EnvFilter or allow adding directives instead of parsing for building the builder. But that is outside of the scope of this PR.

Workaround For: #1584, #1181
Fixes: #2507, #404
Refs: #1584, #1181

Motivation
----------

1. There are some filters which can not be created with the current Directive string repr syntax, like e.g. the strings `bool`, `1` or `2.3` or values containing characters including but not limited to `}],` (for some of this there are partial fixes in other PRS). Furthermore updating the syntax the accommodate this is always a potential braking change, one which also can be subtle to detect.

2. Sometimes there is need to create `Directives` programmatically, e.g. from a different kind of logging config or to set more complicated defaults.

3. We might want to add other matching capabilities in the future which might not be re presentable in the string syntax (or at least entail a braking change). Like e.g. matching fields regex, matching based on `dyn Value`, a field matching the string `true` and boolean `true` at the same time,  etc.

By allowing programmatic creation of `Directive`s we allow downstream users to work around existing issues, experiment with solution to such issues in external crates and in addition set the foundation for adding future matching capabilities in the future.

Solution
--------

- adds `Directive::builder()` / `DirectiveBuilder`
- `ValueMatch` is now public, but wrapped to that we can freely extend and change it without braking changes

Workaround For: tokio-rs#1584, tokio-rs#1181
Fixes: tokio-rs#2507, tokio-rs#404
Refs: tokio-rs#1584, tokio-rs#1181
@rustonaut rustonaut force-pushed the rustonaut/filter-directive-builder branch from 61bdbd8 to 0c5d313 Compare June 24, 2024 13:56
@@ -0,0 +1,102 @@
use core::fmt::Debug;
Copy link
Author

Choose a reason for hiding this comment

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

❓ Should env/directive.rs be moved to env/directive/mod.rs?


// ==== impl Builder ====

impl Builder {
Copy link
Author

Choose a reason for hiding this comment

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

❗ Currently we could add all of this methods directly onto Directive but as idk. if that will be the case in the future I added a builder.

#[derive(Debug, Clone)]
#[repr(transparent)]
pub struct ValueMatch {
inner: ValueMatchInternal,
Copy link
Author

Choose a reason for hiding this comment

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

For building directives this needs to be a public type in some form, the reason why I choose a wrapper instead of #[non_exhaustive] is:

  • like #[non_exhaustive] we can add variants
  • but we also can fully change the internal representation
  • and we don't want to expose MatchDebug and MatchPattern
    • through we could give them a public interface, too
  • we wan to prevent people from creating a F64(f64::NAN)

I still could change it to a pub #[non_exhaustive] enum if desired.

@@ -55,12 +68,113 @@ pub(crate) enum ValueMatch {
Pat(Box<MatchPattern>),
}

// === impl ValueMatch ===
Copy link
Author

Choose a reason for hiding this comment

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

Preferably I would have added this block just above the ==== impl ValueMatchInternal ==== block but that would have introduced many more line changes as some existing impl where outside of their section.

}
}
}

Copy link
Author

Choose a reason for hiding this comment

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

Pulled up so that after this PR all impl ValueMatch are under the same ==== impl ValueMatch === section.

@rustonaut rustonaut marked this pull request as ready for review June 24, 2024 14:04
@rustonaut rustonaut requested review from hawkw, davidbarsky and a team as code owners June 24, 2024 14:04
@rustonaut rustonaut changed the title WIP: subscriber: added builder for filter Directive subscriber: added builder for filter Directive Jun 24, 2024
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.

Manually creating a Directive for a specific target
1 participant