Skip to content

API for automatically tracking duration of trace spans in a metric #125

@evanrittenhouse

Description

@evanrittenhouse

A very common use case for traces is understanding how long specific functions take. It'd be interesting if we could make that queryable for more fine-grained visibility into how services behave. I propose an API that requires both the tracing and metrics features which could be used to automatically do this.

Doing this right now would entail something like this:

#[tracing(span_fn = "do_something")]
async fn do_something() {
    let _guard = do_something_duration.start_timer();
    ...
    // _guard drops and records the duration
}

Instead, we could do something like this:

#[tracing(span_fn = "do_something", track_duration_with = "do_something_duration")]
async fn do_something() {
    // HistogramTimer is attached to the SpanScope, automatically recording how long `do_something` took
}

We'd have to provide a fully-qualified metric name, similar to how the crate_path works, to pick the metric up properly. The metric would have to be created as normal - we could panic or something if it doesn't exist. I think we could just add the guard onto SpanScope and attach it when call wrap_with_span in the macro parsing code.

This could be more generalized to something like

tracing::span("do_something").track_duration_with("do_something_duration").apply(...)

Happy to raise a PR if this is something we want to do, curious what people think

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions