Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
subscriber: Add a
Layer
type for composing subscribers (#136)
* add `Layer` trait for composing subscribers ### Motivation In many cases, it is valuable to compose multiple consumers for trace events. However, certain aspects of `tracing`'s design --- in particular, that span IDs are assigned by the subscriber, and that a single subscriber collects events from multiple threads --- make it difficult to compose the `Subscriber` trait directly. ## Solution This branch introduces a new trait, `Layer`, in `tracing-subscriber`. `Layer` represents the composable subset of the `Subscriber` functionality --- it recieves notifications of spans and events, and can filter callsites, but it does not assign IDs or manage reference counts, instead being notified on span closure by the underlying subscriber. A `Layer` can thus be wrapped around another subscriber to add additional functionality. In addition, this branch adds functionality for composing layers, and a `filter` module that provides `Layer` implementations for simple filters. ## Future Work To have a complete story for multi-subscriber fanout, we will also want to implement a performant concurrent span store, as I described in #157. To better support the use of subscriber layers, may wish to consider having a "registry" subscriber that implements _only_ span storage, ID generation, and lifecycle management, for composing layers on top of. Finally, we should consider adding `Layer` implementations to other crates that currently only expose `Subscriber` impls. Refs: #135 Signed-off-by: Eliza Weisman <[email protected]> * subscriber: remove ref-counting from Layer (#149) ## Motivation As I mentioned in #136 (comment), the `Layer` trait proposed in #136 should probably _not_ be responsible for managing ref-counts and determining span closures. Instead, the root subscriber should be responsible for this, and `Layer`s should be able to opt into a callback that's notified _when_ a span is closed. ## Solution Adding a callback that's called on closure to `Layer` required modifying the `Subscriber` trait to allow indicating when a span closes. This branch attempts to do this without a breaking change, by adding a new `try_close` trait method to `Subscriber`. `try_close` is identical to `drop_span` except that it returns a boolean value, set to `true` if the span has closed. This function was added in #153. This branch updates `tracing-subscriber::Layer` to use `try_close`. Rather than having `clone_span`/`drop_span` callbacks on `Layer`, we now have a `close` function, which is called when the inner subscriber's `try_close` returns `true`. Signed-off-by: Eliza Weisman <[email protected]> * ensure a Context always refers to a Subscriber This reworks layer composition a bit, and fixes an issue where `Context`s were not actually guaranteed to wrap a `Subscriber`, and thus implemented no methods. Layers now guarantee that `S` implements `Subscriber`. Signed-off-by: Eliza Weisman <[email protected]>
- Loading branch information