opentelemetry/metrics/instruments/
histogram.rs

1use crate::KeyValue;
2use core::fmt;
3use std::sync::Arc;
4
5use super::SyncInstrument;
6
7/// An instrument that records a distribution of values.
8///
9/// [`Histogram`] can be cloned to create multiple handles to the same instrument. If a [`Histogram`] needs to be shared,
10/// users are recommended to clone the [`Histogram`] instead of creating duplicate [`Histogram`]s for the same metric. Creating
11/// duplicate [`Histogram`]s for the same metric could lower SDK performance.
12#[derive(Clone)]
13#[non_exhaustive]
14pub struct Histogram<T>(Arc<dyn SyncInstrument<T> + Send + Sync>);
15
16impl<T> fmt::Debug for Histogram<T>
17where
18    T: fmt::Debug,
19{
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        f.write_fmt(format_args!("Histogram<{}>", std::any::type_name::<T>()))
22    }
23}
24
25impl<T> Histogram<T> {
26    /// Create a new histogram.
27    pub fn new(inner: Arc<dyn SyncInstrument<T> + Send + Sync>) -> Self {
28        Histogram(inner)
29    }
30
31    /// Adds an additional value to the distribution.
32    pub fn record(&self, value: T, attributes: &[KeyValue]) {
33        self.0.measure(value, attributes)
34    }
35}