Skip to main content

opentelemetry/metrics/instruments/
histogram.rs

1use crate::KeyValue;
2use core::fmt;
3use std::sync::Arc;
4
5#[cfg(feature = "experimental_metrics_bound_instruments")]
6use super::BoundSyncInstrument;
7use super::SyncInstrument;
8
9/// An instrument that records a distribution of values.
10///
11/// [`Histogram`] can be cloned to create multiple handles to the same instrument. If a [`Histogram`] needs to be shared,
12/// users are recommended to clone the [`Histogram`] instead of creating duplicate [`Histogram`]s for the same metric. Creating
13/// duplicate [`Histogram`]s for the same metric could lower SDK performance.
14#[derive(Clone)]
15#[non_exhaustive]
16pub struct Histogram<T>(Arc<dyn SyncInstrument<T> + Send + Sync>);
17
18impl<T> fmt::Debug for Histogram<T>
19where
20    T: fmt::Debug,
21{
22    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23        f.write_fmt(format_args!("Histogram<{}>", std::any::type_name::<T>()))
24    }
25}
26
27impl<T> Histogram<T> {
28    /// Create a new histogram.
29    pub fn new(inner: Arc<dyn SyncInstrument<T> + Send + Sync>) -> Self {
30        Histogram(inner)
31    }
32
33    /// Adds an additional value to the distribution.
34    pub fn record(&self, value: T, attributes: &[KeyValue]) {
35        self.0.measure(value, attributes)
36    }
37
38    /// Binds this histogram to a fixed set of attributes.
39    ///
40    /// Corresponds to the `Bind` capability in the OpenTelemetry spec (status:
41    /// Development as of spec 1.57.0).
42    #[cfg(feature = "experimental_metrics_bound_instruments")]
43    pub fn bind(&self, attributes: &[KeyValue]) -> BoundHistogram<T> {
44        BoundHistogram(Arc::from(self.0.bind(attributes)))
45    }
46}
47
48/// A histogram bound to a fixed set of attributes.
49///
50/// Created by calling [`Histogram::bind`] with an attribute set. All subsequent
51/// [`record`](BoundHistogram::record) calls use the pre-resolved attributes, bypassing
52/// per-call attribute lookup for significantly better performance.
53///
54/// `BoundHistogram` can be cloned cheaply to share a single bound state across
55/// threads or modules without re-binding. The underlying tracker is reclaimed
56/// when the last clone is dropped.
57#[cfg(feature = "experimental_metrics_bound_instruments")]
58#[derive(Clone)]
59#[must_use = "dropping a BoundHistogram immediately is a no-op; store it to benefit from pre-bound attributes"]
60pub struct BoundHistogram<T>(Arc<dyn BoundSyncInstrument<T> + Send + Sync>);
61
62#[cfg(feature = "experimental_metrics_bound_instruments")]
63impl<T> fmt::Debug for BoundHistogram<T> {
64    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65        f.write_fmt(format_args!(
66            "BoundHistogram<{}>",
67            std::any::type_name::<T>()
68        ))
69    }
70}
71
72#[cfg(feature = "experimental_metrics_bound_instruments")]
73impl<T> BoundHistogram<T> {
74    /// Records a value in the histogram using the pre-bound attributes.
75    pub fn record(&self, value: T) {
76        self.0.measure(value)
77    }
78}