trait-net 0.7.1

A collection of traits for client libraries for reducing boilerplate
Documentation
use crate::metrics::Observer;
use prometheus::{
    core::{AtomicU64, Collector, Desc, GenericCounter},
    proto::MetricFamily,
    IntCounterVec, Opts,
};
use std::ops::Deref;

#[derive(Clone)]
pub struct EndedCount(IntCounterVec);

impl EndedCount {
    pub fn new(opts: Opts, label_names: &[&str]) -> prometheus::Result<Self> {
        Ok(Self(IntCounterVec::new(opts, label_names)?))
    }

    pub fn make_observer(&self, labels: &[&str]) -> EndedCountObserver {
        EndedCountObserver(self.0.with_label_values(labels))
    }
}

impl Deref for EndedCount {
    type Target = IntCounterVec;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl Collector for EndedCount {
    fn desc(&self) -> Vec<&Desc> {
        self.0.desc()
    }

    fn collect(&self) -> Vec<MetricFamily> {
        self.0.collect()
    }
}

pub struct EndedCountObserver(GenericCounter<AtomicU64>);

impl<Out: ?Sized> Observer<Out> for EndedCountObserver {
    fn on_finish(&mut self, _: Option<&Out>) {
        self.0.inc();
    }
}