use std::{borrow::Cow, fmt::Debug};
pub trait CounterOps: Send + Sync + 'static + Debug {
fn increase(&self, val: u64);
}
pub trait GaugeOps: Send + Sync + 'static + Debug {
fn increase(&self, val: u64);
fn decrease(&self, val: u64);
fn absolute(&self, val: u64);
}
pub trait HistogramOps: Send + Sync + 'static + Debug {
fn record(&self, val: f64);
}
pub trait CounterVecOps: Send + Sync + 'static + Debug {
fn counter(&self, labels: &[Cow<'static, str>]) -> BoxedCounter;
}
pub trait GaugeVecOps: Send + Sync + 'static + Debug {
fn gauge(&self, labels: &[Cow<'static, str>]) -> BoxedGauge;
}
pub trait HistogramVecOps: Send + Sync + 'static + Debug {
fn histogram(&self, labels: &[Cow<'static, str>]) -> BoxedHistogram;
}
pub trait RegistryOps: Send + Sync + 'static + Debug {
fn register_counter_vec(
&self,
name: Cow<'static, str>,
desc: Cow<'static, str>,
label_names: &'static [&'static str],
) -> BoxedCounterVec;
fn register_gauge_vec(
&self,
name: Cow<'static, str>,
desc: Cow<'static, str>,
label_names: &'static [&'static str],
) -> BoxedGaugeVec;
fn register_histogram_vec(
&self,
name: Cow<'static, str>,
desc: Cow<'static, str>,
label_names: &'static [&'static str],
) -> BoxedHistogramVec;
fn register_histogram_vec_with_buckets(
&self,
name: Cow<'static, str>,
desc: Cow<'static, str>,
label_names: &'static [&'static str],
buckets: Vec<f64>,
) -> BoxedHistogramVec;
}
pub type BoxedCounter = Box<dyn CounterOps>;
pub type BoxedGauge = Box<dyn GaugeOps>;
pub type BoxedHistogram = Box<dyn HistogramOps>;
pub type BoxedCounterVec = Box<dyn CounterVecOps>;
pub type BoxedGaugeVec = Box<dyn GaugeVecOps>;
pub type BoxedHistogramVec = Box<dyn HistogramVecOps>;
pub type BoxedRegistry = Box<dyn RegistryOps>;
#[derive(Debug)]
pub struct Buckets;
impl Buckets {
pub fn linear(start: f64, width: f64, length: usize) -> Vec<f64> {
std::iter::repeat(())
.enumerate()
.map(|(i, _)| start + (width * (i as f64)))
.take(length)
.collect()
}
pub fn exponential(start: f64, factor: f64, length: usize) -> Vec<f64> {
std::iter::repeat(())
.enumerate()
.map(|(i, _)| start * factor.powf(i as f64))
.take(length)
.collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_linear_buckets() {
assert_eq!(Buckets::linear(0.0, 1.0, 5), vec![0.0, 1.0, 2.0, 3.0, 4.0]);
}
#[test]
fn test_exponential_buckets() {
assert_eq!(Buckets::exponential(1.0, 2.0, 5), vec![1.0, 2.0, 4.0, 8.0, 16.0]);
}
}