pub struct Context<'a, S> { /* private fields */ }
Expand description
Represents information about the current context provided to subscribers by the wrapped collector.
To access stored data keyed by a span ID, implementors of the Subscribe
trait should ensure that the Collect
type parameter is also bound by the
LookupSpan
:
use tracing::Collect;
use tracing_subscriber::{Subscribe, registry::LookupSpan};
pub struct MySubscriber;
impl<C> Subscribe<C> for MySubscriber
where
C: Collect + for<'a> LookupSpan<'a>,
{
// ...
}
Implementations§
Source§impl<'a, C> Context<'a, C>where
C: Collect,
impl<'a, C> Context<'a, C>where
C: Collect,
Sourcepub fn current_span(&self) -> Current
pub fn current_span(&self) -> Current
Returns the wrapped collector’s view of the current span.
Sourcepub fn enabled(&self, metadata: &Metadata<'_>) -> bool
pub fn enabled(&self, metadata: &Metadata<'_>) -> bool
Returns whether the wrapped collector would enable the current span.
Sourcepub fn event(&self, event: &Event<'_>)
pub fn event(&self, event: &Event<'_>)
Records the provided event
with the wrapped collector.
§Notes
- The collector is free to expect that the event’s callsite has been
registered, 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 thatregister_callsite
has been called prior to calling this method. - This does not call
enabled
on the inner collector. If the caller wishes to apply the wrapped collector’s filter before choosing whether to record the event, it may first callContext::enabled
to check whether the event would be enabled. This allows subscriberss to elide constructing the event if it would not be recorded.
Sourcepub fn event_span(&self, event: &Event<'_>) -> Option<SpanRef<'_, C>> ⓘwhere
C: for<'lookup> LookupSpan<'lookup>,
pub fn event_span(&self, event: &Event<'_>) -> Option<SpanRef<'_, C>> ⓘwhere
C: for<'lookup> LookupSpan<'lookup>,
Returns a SpanRef
for the parent span of the given Event
, if
it has a parent.
If the event has an explicitly overridden parent, this method returns
a reference to that span. If the event’s parent is the current span,
this returns a reference to the current span, if there is one. If this
returns None
, then either the event’s parent was explicitly set to
None
, or the event’s parent was defined contextually, but no span
is currently entered.
Compared to Context::current_span
and Context::lookup_current
,
this respects overrides provided by the Event
.
Compared to Event::parent
, this automatically falls back to the contextual
span, if required.
use tracing::{Event, Collect};
use tracing_subscriber::{
subscribe::{Context, Subscribe},
prelude::*,
registry::LookupSpan,
};
struct PrintingSubscriber;
impl<C> Subscribe<C> for PrintingSubscriber
where
C: Collect + for<'lookup> LookupSpan<'lookup>,
{
fn on_event(&self, event: &Event, ctx: Context<C>) {
let span = ctx.event_span(event);
println!("Event in span: {:?}", span.map(|s| s.name()));
}
}
tracing::collect::with_default(tracing_subscriber::registry().with(PrintingSubscriber), || {
tracing::info!("no span");
// Prints: Event in span: None
let span = tracing::info_span!("span");
tracing::info!(parent: &span, "explicitly specified");
// Prints: Event in span: Some("span")
let _guard = span.enter();
tracing::info!("contextual span");
// Prints: Event in span: Some("span")
});
Note: This requires the wrapped collector to implement theLookupSpan
trait. See the documentation onContext
's declaration for details.
Sourcepub fn metadata(&self, id: &Id) -> Option<&'static Metadata<'static>> ⓘwhere
C: for<'lookup> LookupSpan<'lookup>,
pub fn metadata(&self, id: &Id) -> Option<&'static Metadata<'static>> ⓘwhere
C: for<'lookup> LookupSpan<'lookup>,
Returns metadata for the span with the given id
, if it exists.
If this returns None
, then no span exists for that ID (either it has
closed or the ID is invalid).
Sourcepub fn span(&self, id: &Id) -> Option<SpanRef<'_, C>> ⓘwhere
C: for<'lookup> LookupSpan<'lookup>,
pub fn span(&self, id: &Id) -> Option<SpanRef<'_, C>> ⓘwhere
C: for<'lookup> LookupSpan<'lookup>,
Returns stored data for the span with the given id
, if it exists.
If this returns None
, then no span exists for that ID (either it has
closed or the ID is invalid).
Note: This requires the wrapped collector to implement theLookupSpan
trait. See the documentation onContext
's declaration for details.
Sourcepub fn exists(&self, id: &Id) -> boolwhere
C: for<'lookup> LookupSpan<'lookup>,
pub fn exists(&self, id: &Id) -> boolwhere
C: for<'lookup> LookupSpan<'lookup>,
Returns true
if an active span exists for the given Id
.
Note: This requires the wrapped collector to implement theLookupSpan
trait. See the documentation onContext
's declaration for details.
Sourcepub fn lookup_current(&self) -> Option<SpanRef<'_, C>> ⓘwhere
C: for<'lookup> LookupSpan<'lookup>,
pub fn lookup_current(&self) -> Option<SpanRef<'_, C>> ⓘwhere
C: for<'lookup> LookupSpan<'lookup>,
Returns stored data for the span that the wrapped collector considers to be the current.
If this returns None
, then we are not currently within a span.
Note: This requires the wrapped collector to implement theLookupSpan
trait. See the documentation onContext
's declaration for details.
Sourcepub fn span_scope(&self, id: &Id) -> Option<Scope<'_, C>> ⓘwhere
C: for<'lookup> LookupSpan<'lookup>,
pub fn span_scope(&self, id: &Id) -> Option<Scope<'_, C>> ⓘwhere
C: for<'lookup> LookupSpan<'lookup>,
Returns an iterator over the stored data for all the spans in the current context, starting with the specified span and ending with the root of the trace tree.
Note: This returns the spans in reverse order (from leaf to root). Use
Scope::from_root
in case root-to-leaf ordering is desired.
Note: This requires the wrapped collector to implement theLookupSpan
trait. See the documentation onContext
's declaration for details.
Sourcepub fn event_scope(&self, event: &Event<'_>) -> Option<Scope<'_, C>> ⓘwhere
C: for<'lookup> LookupSpan<'lookup>,
pub fn event_scope(&self, event: &Event<'_>) -> Option<Scope<'_, C>> ⓘwhere
C: for<'lookup> LookupSpan<'lookup>,
Returns an iterator over the stored data for all the spans in the current context, starting with the parent span of the specified event, and ending with the root of the trace tree and ending with the current span.
Note: Compared toscope
this returns the spans in reverse order (from leaf to root). UseScope::from_root
in case root-to-leaf ordering is desired.
Note: This requires the wrapped collector to implement theLookupSpan
trait. See the documentation onContext
's declaration for details.