Skip to content

subscriber: consider changing on_layer to take the subscriber by reference #2101

@hawkw

Description

@hawkw

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    crate/subscriberRelated to the `tracing-subscriber` cratemeta/breakingThis is a breaking change, and should wait until the next breaking release.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions