use std::{sync::Arc, time::Duration};
use crate::{
encoder::{EncodeCounterValue, EncodeGaugeValue},
metrics::{
counter::{CounterValue, LazyCounter},
gauge::{GaugeValue, LazyGauge},
},
};
mod id;
mod scrape_ctx;
mod source;
use self::id::LazyGroupId;
#[inline]
pub fn enter_scope() -> impl Drop {
scrape_ctx::enter()
}
pub struct LazyGroup<S> {
pub(crate) id: LazyGroupId,
pub(crate) sample: Arc<dyn Fn() -> S + Send + Sync>,
}
impl<S> Clone for LazyGroup<S> {
fn clone(&self) -> Self {
Self { id: self.id, sample: Arc::clone(&self.sample) }
}
}
impl<S> LazyGroup<S>
where
S: Send + Sync + 'static,
{
pub fn new(sample: impl Fn() -> S + Send + Sync + 'static) -> Self {
let id = id::next_lazy_group_id();
Self { id, sample: Arc::new(sample) }
}
pub fn counter<N, M>(&self, map: M) -> LazyCounter<N>
where
M: Fn(&S) -> N + Send + Sync + 'static,
N: EncodeCounterValue + CounterValue + 'static,
{
source::counter_from_group(self.clone(), map, None)
}
pub fn counter_with_created<N, M>(&self, map: M, created: Duration) -> LazyCounter<N>
where
M: Fn(&S) -> N + Send + Sync + 'static,
N: EncodeCounterValue + CounterValue + 'static,
{
source::counter_from_group(self.clone(), map, Some(created))
}
pub fn gauge<N, M>(&self, map: M) -> LazyGauge<N>
where
M: Fn(&S) -> N + Send + Sync + 'static,
N: EncodeGaugeValue + GaugeValue + 'static,
{
source::gauge_from_group(self.clone(), map)
}
}