subscriber: consider changing on_layer
to take the subscriber by reference
#2101
Labels
crate/subscriber
Related to the `tracing-subscriber` crate
meta/breaking
This is a breaking change, and should wait until the next breaking release.
Feature Request
Crates
tracing-subscriber
Motivation
Currently, the
Layer::on_layer
callback intracing_subscriber
takes theSubscriber
that the layer is being added to as an&mut
reference. This was intended to allow theSubscriber
to mutate itself as part of theon_layer
callback. For example, per-layer filtering uses this callback to increment the filter ID counter in order to generate a new filter ID.However, this introduces some issues in the face of layer reloading. Because the
reload
layer stores the _layer_behind a mutex, but not theSubscriber
, it cannot mutate the rest of the subscriber when reloading aLayer
. This means that the subscriber cannot generate a new filter ID if a filter is replaced or if new filtered layers are added. This currently causes a panic (see #1629).Proposal
We may wish to change
Layer::on_layer
to take an&S
rather than an&mut S
. This would allow it to be called inreload::Reload
. In the case of per-layer filtering, we could change the subscriber to use an atomic counter to generate filter IDs, as I mentioned in #1629 (comment):Alternatives
Alternatively, we may not want to do this. Although an atomic counter works in the case of filter ID generation, if other
Subscriber
implementations take advantage of the ability to mutate themselves in theon_layer
callback, they may not be able to trivially switch to a&S
receiver. Mutating a more complex data structure would require locking.This is a breaking change, so we can't do it until 0.4, in any case.
The text was updated successfully, but these errors were encountered: