spacetimedb/util/
prometheus_handle.rs1use std::time::Instant;
2
3use prometheus::{Histogram, IntGauge};
4
5pub struct GaugeInc {
7 gauge: IntGauge,
8}
9impl Drop for GaugeInc {
10 #[inline]
11 fn drop(&mut self) {
12 self.gauge.dec();
13 }
14}
15
16#[inline]
18pub fn inc_scope(gauge: &IntGauge) -> GaugeInc {
19 gauge.inc();
20 GaugeInc { gauge: gauge.clone() }
21}
22
23pub trait IntGaugeExt {
24 fn inc_scope(&self) -> GaugeInc;
25}
26
27impl IntGaugeExt for IntGauge {
28 fn inc_scope(&self) -> GaugeInc {
29 inc_scope(self)
30 }
31}
32
33pub struct TimerGuard {
36 histogram: Histogram,
37 timer: Instant,
38}
39
40impl Drop for TimerGuard {
41 fn drop(&mut self) {
42 self.histogram.observe(self.timer.elapsed().as_secs_f64());
43 }
44}
45
46pub trait HistogramExt {
47 fn with_timer(self, timer: Instant) -> TimerGuard;
48}
49
50impl HistogramExt for Histogram {
51 fn with_timer(self, timer: Instant) -> TimerGuard {
52 TimerGuard { histogram: self, timer }
53 }
54}