opentelemetry/metrics/instruments/
histogram.rs
1use crate::{
2 metrics::{InstrumentBuilder, MetricsError},
3 KeyValue,
4};
5use core::fmt;
6use std::sync::Arc;
7
8pub trait SyncHistogram<T> {
10 fn record(&self, value: T, attributes: &[KeyValue]);
12}
13
14#[derive(Clone)]
16pub struct Histogram<T>(Arc<dyn SyncHistogram<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 pub fn new(inner: Arc<dyn SyncHistogram<T> + Send + Sync>) -> Self {
30 Histogram(inner)
31 }
32
33 pub fn record(&self, value: T, attributes: &[KeyValue]) {
35 self.0.record(value, attributes)
36 }
37}
38
39impl TryFrom<InstrumentBuilder<'_, Histogram<f64>>> for Histogram<f64> {
40 type Error = MetricsError;
41
42 fn try_from(builder: InstrumentBuilder<'_, Histogram<f64>>) -> Result<Self, Self::Error> {
43 builder
44 .instrument_provider
45 .f64_histogram(builder.name, builder.description, builder.unit)
46 }
47}
48
49impl TryFrom<InstrumentBuilder<'_, Histogram<u64>>> for Histogram<u64> {
50 type Error = MetricsError;
51
52 fn try_from(builder: InstrumentBuilder<'_, Histogram<u64>>) -> Result<Self, Self::Error> {
53 builder
54 .instrument_provider
55 .u64_histogram(builder.name, builder.description, builder.unit)
56 }
57}