use crate::KeyValue;
use core::fmt;
use std::sync::Arc;
#[cfg(feature = "experimental_metrics_bound_instruments")]
use super::BoundSyncInstrument;
use super::SyncInstrument;
#[derive(Clone)]
#[non_exhaustive]
pub struct Histogram<T>(Arc<dyn SyncInstrument<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 SyncInstrument<T> + Send + Sync>) -> Self {
Histogram(inner)
}
pub fn record(&self, value: T, attributes: &[KeyValue]) {
self.0.measure(value, attributes)
}
#[cfg(feature = "experimental_metrics_bound_instruments")]
pub fn bind(&self, attributes: &[KeyValue]) -> BoundHistogram<T> {
BoundHistogram(Arc::from(self.0.bind(attributes)))
}
}
#[cfg(feature = "experimental_metrics_bound_instruments")]
#[derive(Clone)]
#[must_use = "dropping a BoundHistogram immediately is a no-op; store it to benefit from pre-bound attributes"]
pub struct BoundHistogram<T>(Arc<dyn BoundSyncInstrument<T> + Send + Sync>);
#[cfg(feature = "experimental_metrics_bound_instruments")]
impl<T> fmt::Debug for BoundHistogram<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!(
"BoundHistogram<{}>",
std::any::type_name::<T>()
))
}
}
#[cfg(feature = "experimental_metrics_bound_instruments")]
impl<T> BoundHistogram<T> {
pub fn record(&self, value: T) {
self.0.measure(value)
}
}