timer

Attribute Macro timer 

Source
#[timer]
Expand description

Decorator for synchronous and asynchronous functions to measure their execution time with RAII [prom-timer]

As the error message says, handler function needs to be async.

use prometheus::{self, HistogramVec, histogram_opts};

let timer = HistogramVec::new(
            histogram_opts!("timer", "Timer")
                .namespace("api_v2")
                .const_labels(labels.clone())
                .buckets(
                    [
                        0.001, 0.0025, 0.005, 0.01, 0.025, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 1.0, 2.0,
                       3.0, 4.0, 5.0, 10.0,
                    ]
                    .into(),
                ),
            &["tag"],
        )
        .unwrap();

registry.register(Box::new(timer.clone())).unwrap();

#[timer(timer, "f")]
async fn f() {
    // ... Some work here.
}

f() execution time will be stored as timer histogram entry with tag f