1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use prometheus::HistogramVec;
use std::time::Instant;

pub struct Timer {
    histogram: HistogramVec,
    start: Instant,
    tags: &'static [&'static str],
}

impl Timer {
    pub fn new(histogram: HistogramVec, tags: &'static [&'static str]) -> Self {
        Self {
            histogram,
            start: Instant::now(),
            tags,
        }
    }
}

impl Drop for Timer {
    fn drop(&mut self) {
        let elapsed = self.start.elapsed();
        self.histogram
            .with_label_values(self.tags)
            .observe(elapsed.as_secs_f64());
    }
}