Skip to content

Look up span for event #1428

@nightkr

Description

@nightkr

Feature Request

Crates

  • tracing-subscriber

Motivation

Currently you need to handle a lot of cases yourself when writing a Layer that tries to map an Event back to its Span stack. To handle both implicit and explicit span association you more or less need the following logic:

if event.is_root() {
  None
} else if event.is_contextual() {
  ctx.lookup_current()
} else {
  event.parent().and_then(|id| ctx.span(id))
}

Otherwise you might get bugs like the following event being associated with the wrong Span!:

let span_a = info_span!("a");
let span_b = info_span!("b").entered();
info!(parent: span_a, "hey");

Aside from being tedious and encoding a lot of logic about how Events work internally, even seasoned Tracing users get this wrong, both informally and in published libraries (note: this is not a dig against @davidbarsky, just an example of the confusion that this can cause).

Motivated by some discussion on Discord, starting at: https://canary.discord.com/channels/500028886025895936/627649734592561152/852143116244221973

Proposal

Add a Context::event_span method that encodes this logic. Add documentation to Context::current_span and Event::parent referring to it.

Alternatives

Another option would be to have tracing::event!("message") (with no explicit parent) roughly equivalent to tracing::event!(parent: tracing::dispatch::get_current().current_span(), "message"), just like tracing::span! does. That would mean that Event::parent always Does The Right Thing, and no new API is required. @hawkw seemed to be against this path, but to be honest I never quite got what the problem is here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions