subscriber: impl Clone for EnvFilter #2956
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
In #2360, people have requested
impl Clone for EnvFilter
in order to useEnvFilter
as aclap
argument. Inconduwuit
, we want this in order to do something like this:tokio-console
'sconsole_subscriber
layer needs to be unfiltered in order to pick up tokio trace events, but we have several "logging-type" layers that we want to apply the configuredEnvFilter
to.We considered the workaround of constructing several fresh
EnvFilter
s withEnvFilter::try_new(config.log.clone())
, but would prefer to just clone theEnvFilter
itself and not have to deal with repeating the parsing/error handling.Solution
We generally expect users to be cloning an
EnvFilter
before attaching it to a subscriber, rather than cloningEnvFilters
that are already attached. Because of this, we reset all the accumulated dynamic state when cloning. This means that some spans and callsites might be missed when an already-attachedEnvFilter
is cloned, but the presence of the dynamic state mean that detaching and attachingEnvFilter
s to existing subscribers (e.g. withreload
) already doesn't work very well. This isn't a new class of problem.There was a previous implementation of this in #2398, that shared the dynamic state between all cloned filters behind an
Arc
. I chose not do go for that approach because it causes inconsistencies if the cloned filters are attached to different subscribers.A third option would be to clone the dynamic state, but this gets kinda messy with lock poisoning. I can't think of a scenario where that behavior would actually be better.