Skip to main content

TimerMetric

Trait TimerMetric 

Source
pub trait TimerMetric {
    // Required method
    fn name(&self) -> &'static str;
}
Expand description

A metric for capturing timings.

Timings are a positive number of milliseconds between a start and end time. Examples include time taken to render a web page or time taken for a database call to return.

§Example

use relay_statsd::{metric, TimerMetric};

enum MyTimer {
    ProcessA,
    ProcessB,
}

impl TimerMetric for MyTimer {
    fn name(&self) -> &'static str {
        match self {
            Self::ProcessA => "process_a",
            Self::ProcessB => "process_b",
        }
    }
}


// measure time by explicitly setting a std::timer::Duration
let start_time = Instant::now();
process_a();
metric!(timer(MyTimer::ProcessA) = start_time.elapsed());

// provide tags to a timer
metric!(
    timer(MyTimer::ProcessA) = start_time.elapsed(),
    server = "server1",
    host = "host1",
);

// measure time implicitly by enclosing a code block in a metric
metric!(timer(MyTimer::ProcessA), {
    process_a();
});

// measure block and also provide tags
metric!(
    timer(MyTimer::ProcessB),
    server = "server1",
    host = "host1",
    {
        process_a();
    }
);

Required Methods§

Source

fn name(&self) -> &'static str

Returns the timer metric name that will be sent to statsd.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§