Skip to content

Allow specifying parent and/or follows_from of spans made by #[instrument] #879

@anp

Description

@anp

Feature Request

Crates

tracing & tracing-core, I think.

Motivation

It would be very useful to have support for #[instrument] on methods of a type which holds a span corresponding to its lifecycle.

Proposal

It's not (yet?) possible to inject fields into a struct from a derive, so we'll assume that a user has added a span field to the type whose lifecycle we're tracking:

struct ServiceConnection {
    // ...
    span: tracing::Span,
}

What I think we want is a way for that span to be used as the immediate span for method calls that are annotated with instrument:

impl ServiceConnection {
    #[instrument(parent = "self.span")]
    fn send_response(&self) { ... }
}

In this example, send_response's span would have the value's span as its parent. I think we'd also want a way to say that the method's span follows from the span which was active when the call was made, so that the method's span is connected both to the object and to the active tree of spans. Maybe that should be implicit or have a convenient shorthand?

Alternatives

We could express these semantics in terms of a trait, like

trait Spanned {
    fn span(&self) -> tracing::Span;
}

Then #[instrument] could have a shorthand for using self's span:

impl ServiceConnection {
    #[instrument(spanned)]
    fn send_response(&self) { ... }
}

Related

This relies on #651 to omit self by default from instrumentation. The core idea is to provide a less verbose way to instrument self.

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