Skip to content

Commit

Permalink
Add Context method for resolving an Event's SpanRef
Browse files Browse the repository at this point in the history
  • Loading branch information
nightkr committed Jun 10, 2021
1 parent dcd537e commit 01415a7
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions tracing-subscriber/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,29 @@ where
}
}

/// Finds the [`SpanRef`] associated with an [`Event`], if it has one.
///
/// 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.
#[inline]
#[cfg(feature = "registry")]
#[cfg_attr(docsrs, doc(cfg(feature = "registry")))]
pub fn event_span(&self, event: &Event<'_>) -> Option<SpanRef<'_, S>>
where
S: for<'lookup> LookupSpan<'lookup>,
{
if event.is_root() {
None
} else if event.is_contextual() {
self.lookup_current()
} else {
event.parent().and_then(|id| self.span(id))
}
}

/// 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
Expand Down

0 comments on commit 01415a7

Please sign in to comment.