mirror of
https://github.com/tokio-rs/tracing.git
synced 2025-10-01 15:00:33 +00:00
subscriber: add function to send an event to the inner subscriber (#286)
## Motivation In some cases, `Layer` implementations may wish to trigger new events on the inner subscriber. Potential use cases include layers that aggregate or roll up events. Now that PR #281 allows events to be constructed without implicitly dispatching them to a `Subscriber`, we can add support for this to the `Layer` trait. ## Solution This commit adds a `Context::event` method to the `layer::Context` type in `tracing-subscriber`. This takes an `&Event` and calls `Subscriber::event` on the wrapped subscriber. Signed-off-by: Eliza Weisman <eliza@buoyant.io>
This commit is contained in:
parent
ffca615cc5
commit
a9d4d98e39
@ -620,6 +620,44 @@ impl<'a, S: Subscriber> Context<'a, S> {
|
||||
// `tracing-core` should make `Current::unknown()` public?
|
||||
.unwrap_or_else(span::Current::none)
|
||||
}
|
||||
|
||||
/// Returns whether the wrapped subscriber would enable the current span.
|
||||
#[inline]
|
||||
pub fn enabled(&self, metadata: &Metadata) -> bool {
|
||||
self.subscriber
|
||||
.map(|subscriber| subscriber.enabled(metadata))
|
||||
// If this context is `None`, we are registering a callsite, so
|
||||
// return `true` so that the layer does not incorrectly assume that
|
||||
// the inner subscriber has disabled this metadata.
|
||||
// TODO(eliza): would it be more correct for this to return an `Option`?
|
||||
.unwrap_or(true)
|
||||
}
|
||||
|
||||
/// Records the provided `event` with the wrapped subscriber.
|
||||
///
|
||||
/// # Notes
|
||||
///
|
||||
/// - The subscriber is free to expect that the event's callsite has been
|
||||
/// [registered][register], and may panic or fail to observe the event if this is
|
||||
/// not the case. The `tracing` crate's macros ensure that all events are
|
||||
/// registered, but if the event is constructed through other means, the
|
||||
/// user is responsible for ensuring that [`register_callsite`][register]
|
||||
/// has been called prior to calling this method.
|
||||
/// - This does _not_ call [`enabled`] on the inner subscriber. If the
|
||||
/// caller wishes to apply the wrapped subscriber's filter before choosing
|
||||
/// whether to record the event, it may first call [`Context::enabled`] to
|
||||
/// check whether the event would be enabled. This allows `Layer`s to
|
||||
/// elide constructing the event if it would not be recorded.
|
||||
///
|
||||
/// [register]: https://docs.rs/tracing-core/latest/tracing_core/subscriber/trait.Subscriber.html#method.register_callsite
|
||||
/// [`enabled`]: https://docs.rs/tracing-core/latest/tracing_core/subscriber/trait.Subscriber.html#method.enabled
|
||||
/// [`Context::enabled`]: #method.enabled
|
||||
#[inline]
|
||||
pub fn event(&self, event: &Event) {
|
||||
if let Some(ref subscriber) = self.subscriber {
|
||||
subscriber.event(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, S> Context<'a, S> {
|
||||
|
Loading…
x
Reference in New Issue
Block a user