use crate::telemetry::metrics::{MetricKeyValue, MetricParameters};
use std::{
any::Any,
fmt::Debug,
sync::{Arc, OnceLock},
time::Duration,
};
pub trait CustomMetricAttributes: Debug + Send + Sync {
fn as_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync>;
}
#[derive(Debug, Clone)]
pub enum MetricEvent<I: BufferInstrumentRef> {
Create {
params: MetricParameters,
populate_into: LazyBufferInstrument<I>,
kind: MetricKind,
},
CreateAttributes {
populate_into: BufferAttributes,
append_from: Option<BufferAttributes>,
attributes: Vec<MetricKeyValue>,
},
Update {
instrument: LazyBufferInstrument<I>,
attributes: BufferAttributes,
update: MetricUpdateVal,
},
}
#[derive(Debug, Clone, Copy)]
pub enum MetricKind {
Counter,
Gauge,
GaugeF64,
Histogram,
HistogramF64,
HistogramDuration,
UpDownCounter,
}
#[derive(Debug, Clone, Copy)]
pub enum MetricUpdateVal {
Delta(u64),
DeltaF64(f64),
Value(u64),
ValueF64(f64),
Duration(Duration),
SignedDelta(i64),
}
pub trait MetricCallBufferer<I: BufferInstrumentRef>: Send + Sync {
fn retrieve(&self) -> Vec<MetricEvent<I>>;
}
pub type BufferAttributes = LazyRef<Arc<dyn CustomMetricAttributes + 'static>>;
pub trait BufferInstrumentRef {}
pub type LazyBufferInstrument<T> = LazyRef<Arc<T>>;
#[derive(Debug, Clone)]
pub struct LazyRef<T> {
to_be_initted: Arc<OnceLock<T>>,
}
impl<T> LazyRef<T> {
pub fn hole() -> Self {
Self {
to_be_initted: Arc::new(OnceLock::new()),
}
}
pub fn get(&self) -> &T {
self.to_be_initted
.get()
.expect("You must initialize the reference before using it")
}
pub fn set(&self, val: T) -> Result<(), T> {
self.to_be_initted.set(val)
}
}