use prometheus::core::{Atomic, AtomicF64, AtomicI64, GenericGauge, Number};
pub type IntGaugeGuard = GaugeGuard<AtomicI64>;
pub type FloatGaugeGuard = GaugeGuard<AtomicF64>;
#[must_use = "the gauge is decremented as soon as the guard is dropped"]
pub struct GaugeGuard<P: Atomic> {
gauge: GenericGauge<P>,
amount: P::T,
}
impl<P: Atomic> GaugeGuard<P> {
pub fn release(self) {}
}
impl<P: Atomic> Drop for GaugeGuard<P> {
fn drop(&mut self) {
self.gauge.sub(self.amount);
}
}
pub trait GaugeGuardExt<P: Atomic> {
fn inc_guard(&self) -> GaugeGuard<P>;
fn add_guard(&self, amount: P::T) -> GaugeGuard<P>;
}
impl<P: Atomic> GaugeGuardExt<P> for GenericGauge<P> {
fn inc_guard(&self) -> GaugeGuard<P> {
self.add_guard(P::T::from_i64(1))
}
fn add_guard(&self, amount: P::T) -> GaugeGuard<P> {
self.add(amount);
GaugeGuard {
gauge: self.clone(),
amount,
}
}
}