Skip to content

Commit

Permalink
subscriber: Add a Layer type for composing subscribers (#136)
Browse files Browse the repository at this point in the history
* 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
hawkw authored Jul 17, 2019
1 parent d6750d0 commit 8ad3e8e
Show file tree
Hide file tree
Showing 7 changed files with 718 additions and 17 deletions.
2 changes: 1 addition & 1 deletion tracing-fmt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ lock_api = "0.1"
chrono = { version = "0.4", optional = true }

[dev-dependencies]
tracing = "0.1"
tracing = { version = "0.1", path = "../tracing" }
2 changes: 1 addition & 1 deletion tracing-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["Eliza Weisman <[email protected]>"]
edition = "2018"

[dependencies]
tracing = "0.1"
tracing = { version = "0.1", path = "../tracing" }

[dev-dependencies]
tracing-log = { path = "../tracing-log" }
Expand Down
2 changes: 1 addition & 1 deletion tracing-proc-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ authors = ["Eliza Weisman <[email protected]>", "David Barsky <[email protected]
proc-macro = true

[dependencies]
tracing = "0.1"
tracing = { version = "0.1", path = "../tracing" }
syn = { version = "0.15", features = ["full", "extra-traits"] }
quote = "0.6"
proc-macro2 = { version = "0.4", features = ["nightly"] }
Expand Down
3 changes: 2 additions & 1 deletion tracing-subscriber/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ authors = ["Eliza Weisman <[email protected]>"]
edition = "2018"

[dependencies]
tracing = "0.1"
tracing-core = "0.1.2"

[dev-dependencies]
tracing-log = { path = "../tracing-log" }
tracing = "0.1"
Loading

0 comments on commit 8ad3e8e

Please sign in to comment.