8000 Add trait/type name to the span name on trait methods that use instrument · Issue #1939 · tokio-rs/tracing · GitHub
[go: up one dir, main page]

Skip to content

Add trait/type name to the span name on trait methods that use instrument #1939

@tiagolobocastro

Description

@tiagolobocastro

Feature Request

Crates

tracing

Motivation

When we have a trait, some trait methods may have less descriptive names on their own, example:

#[async_trait]
impl trait Pool for PoolXpto {
#[instrument]
async fn create(&self) { ... }
}

In some cases, you might want a more specific name for the span, example "Pool::create" or "Pool::PoolXpto::create". In this case I ended up changing each instrument, example:

#[async_trait]
impl trait Pool for PoolXpto {
#[instrument(name = "Pool::PoolXpto::create")]
async fn create(&self) { ... }
}

This process of manually changing the name is a bit tedious and error prone. It would be nice if we could somehow do this automatically.

Proposal

  1. Maybe we can have a post fix that gets automatically added to the name? Example:
#[async_trait]
impl trait Pool for PoolXpto {
#[instrument(postfix = "Pool::PoolXpto")] // -> span name would be "Pool::PoolXpto::create"
async fn create(&self) { ... }
}
  1. Can we add some information to the trait that the instrument might be able to make use of? Example:
#[async_trait]
#[tracing_postfix]
impl trait Pool for PoolXpto {
#[instrument] // => this would then create a span with name "Pool::PoolXpto::create"
async fn create(&self) { ... }
}

Maybe we could have a trait that gets implemented with "tracing" and returns the postfix name which then instrument uses. Not sure how feasible this is though. Maybe something like:

trait TracingPostfix<Trait: ?Sized> {
fn postfix(&self) -> &static str;
}
impl trait TracingPostfix<dyn PoolXpto> for Pool {
fn postfix(&self) -> &'static str { "Pool::PoolXpto" }
}

Alternatives

Simply add the names one by one on each trait method and be sure to set them correctly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    crate/attributesRelated to the `tracing-attributes` cratekind/featureNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0