opentelemetry/metrics/instruments/
histogram.rsuse crate::{
metrics::{InstrumentBuilder, MetricsError},
KeyValue,
};
use core::fmt;
use std::sync::Arc;
pub trait SyncHistogram<T> {
fn record(&self, value: T, attributes: &[KeyValue]);
}
#[derive(Clone)]
pub struct Histogram<T>(Arc<dyn SyncHistogram<T> + Send + Sync>);
impl<T> fmt::Debug for Histogram<T>
where
T: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!("Histogram<{}>", std::any::type_name::<T>()))
}
}
impl<T> Histogram<T> {
pub fn new(inner: Arc<dyn SyncHistogram<T> + Send + Sync>) -> Self {
Histogram(inner)
}
pub fn record(&self, value: T, attributes: &[KeyValue]) {
self.0.record(value, attributes)
}
}
impl TryFrom<InstrumentBuilder<'_, Histogram<f64>>> for Histogram<f64> {
type Error = MetricsError;
fn try_from(builder: InstrumentBuilder<'_, Histogram<f64>>) -> Result<Self, Self::Error> {
builder
.instrument_provider
.f64_histogram(builder.name, builder.description, builder.unit)
}
}
impl TryFrom<InstrumentBuilder<'_, Histogram<u64>>> for Histogram<u64> {
type Error = MetricsError;
fn try_from(builder: InstrumentBuilder<'_, Histogram<u64>>) -> Result<Self, Self::Error> {
builder
.instrument_provider
.u64_histogram(builder.name, builder.description, builder.unit)
}
}