use std::sync::Arc;
use prometheus_client::metrics::counter::Counter as ClientCounter;
use prometheus_client::metrics::family::Family;
use crate::telemetry::metrics::error::MetricError;
use crate::telemetry::metrics::instrument::InstrumentState;
use crate::telemetry::metrics::labels::Labels;
#[derive(Debug, Clone)]
pub struct Counter {
state: Arc<InstrumentState<Family<Labels, ClientCounter>>>,
labels: Labels,
}
impl Counter {
pub fn increment(&self, value: u64) {
self.state.family.get_or_create(&self.labels).inc_by(value);
}
pub fn with(&self, labels: Labels) -> Result<Self, MetricError> {
self.state.bind(&labels)?;
Ok(Self {
state: self.state.clone(),
labels,
})
}
pub(crate) fn new(name: &str, family: Family<Labels, ClientCounter>) -> Self {
Self {
state: Arc::new(InstrumentState::new(name.to_owned(), family)),
labels: Labels::new(),
}
}
}