1use prometheus::HistogramVec;
2use std::time::Instant;
3
4pub struct Timer {
5 histogram: HistogramVec,
6 start: Instant,
7 tags: &'static [&'static str],
8}
9
10impl Timer {
11 pub fn new(histogram: HistogramVec, tags: &'static [&'static str]) -> Self {
12 Self {
13 histogram,
14 start: Instant::now(),
15 tags,
16 }
17 }
18}
19
20impl Drop for Timer {
21 fn drop(&mut self) {
22 let elapsed = self.start.elapsed();
23 self.histogram
24 .with_label_values(self.tags)
25 .observe(elapsed.as_secs_f64());
26 }
27}