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: consider changing on_layer to take the subscriber by reference #2101

Open
hawkw opened this issue Apr 28, 2022 · 0 comments
Open
Labels
crate/subscriber Related to the `tracing-subscriber` crate meta/breaking This is a breaking change, and should wait until the next breaking release.

Comments

@hawkw
Copy link
Member

hawkw commented Apr 28, 2022

Feature Request

Crates

  • tracing-subscriber

Motivation

Currently, the Layer::on_layer callback in tracing_subscriber takes the Subscriber that the layer is being added to as an &mut reference. This was intended to allow the Subscriber to mutate itself as part of the on_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 the Subscriber, it cannot mutate the rest of the subscriber when reloading a Layer. 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 in reload::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):

We could change on_layer to take &Subscriber instead, and increment the counter of filter IDs using an atomic, but changing the function prototype would be a breaking change, so we can't do that until 0.4.

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 the on_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.

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 meta/breaking This is a breaking change, and should wait until the next breaking release.
Projects
None yet
Development

No branches or pull requests

1 participant