use std::sync::Arc;
use std::sync::atomic::AtomicU64;
use prometheus_client::metrics::family::Family;
use prometheus_client::metrics::gauge::Gauge as ClientGauge;
use crate::telemetry::metrics::error::MetricError;
use crate::telemetry::metrics::instrument::InstrumentState;
use crate::telemetry::metrics::labels::Labels;
pub(crate) type ClientGaugeF64 = ClientGauge<f64, AtomicU64>;
#[derive(Debug, Clone)]
pub struct Gauge {
state: Arc<InstrumentState<Family<Labels, ClientGaugeF64>>>,
labels: Labels,
}
impl Gauge {
pub fn set(&self, value: f64) {
self.state.family.get_or_create(&self.labels).set(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, ClientGaugeF64>) -> Self {
Self {
state: Arc::new(InstrumentState::new(name.to_owned(), family)),
labels: Labels::new(),
}
}
}