ts_opentelemetry_api/metrics/
meter.rs

1use core::fmt;
2use std::any::Any;
3use std::borrow::Cow;
4use std::sync::Arc;
5
6use crate::metrics::{
7    AsyncInstrumentBuilder, Counter, Histogram, InstrumentBuilder, InstrumentProvider,
8    ObservableCounter, ObservableGauge, ObservableUpDownCounter, Result, UpDownCounter,
9};
10use crate::KeyValue;
11
12use super::AsyncInstrument;
13
14/// Provides access to named [Meter] instances, for instrumenting an application
15/// or crate.
16pub trait MeterProvider {
17    /// Returns a new [Meter] with the provided name and default configuration.
18    ///
19    /// A [Meter] should be scoped at most to a single application or crate. The
20    /// name needs to be unique so it does not collide with other names used by
21    /// an application, nor other applications.
22    ///
23    /// If the name is empty, then an implementation defined default name will
24    /// be used instead.
25    fn meter(&self, name: impl Into<Cow<'static, str>>) -> Meter {
26        self.versioned_meter(
27            name,
28            None::<Cow<'static, str>>,
29            None::<Cow<'static, str>>,
30            None,
31        )
32    }
33
34    /// Creates an implementation of the [`Meter`] interface.
35    ///
36    /// The instrumentation name must be the name of the library providing instrumentation. This
37    /// name may be the same as the instrumented code only if that code provides built-in
38    /// instrumentation. If the instrumentation name is empty, then a implementation defined
39    /// default name will be used instead.
40    fn versioned_meter(
41        &self,
42        name: impl Into<Cow<'static, str>>,
43        version: Option<impl Into<Cow<'static, str>>>,
44        schema_url: Option<impl Into<Cow<'static, str>>>,
45        attributes: Option<Vec<KeyValue>>,
46    ) -> Meter;
47}
48
49/// Provides access to instrument instances for recording metrics.
50#[derive(Clone)]
51pub struct Meter {
52    pub(crate) instrument_provider: Arc<dyn InstrumentProvider + Send + Sync>,
53}
54
55impl Meter {
56    /// Create a new named meter from an instrumentation provider
57    #[doc(hidden)]
58    pub fn new(instrument_provider: Arc<dyn InstrumentProvider + Send + Sync>) -> Self {
59        Meter {
60            instrument_provider,
61        }
62    }
63
64    /// creates an instrument builder for recording increasing values.
65    pub fn u64_counter(
66        &self,
67        name: impl Into<Cow<'static, str>>,
68    ) -> InstrumentBuilder<'_, Counter<u64>> {
69        InstrumentBuilder::new(self, name.into())
70    }
71
72    /// creates an instrument builder for recording increasing values.
73    pub fn f64_counter(
74        &self,
75        name: impl Into<Cow<'static, str>>,
76    ) -> InstrumentBuilder<'_, Counter<f64>> {
77        InstrumentBuilder::new(self, name.into())
78    }
79
80    /// creates an instrument builder for recording increasing values via callback.
81    pub fn u64_observable_counter(
82        &self,
83        name: impl Into<Cow<'static, str>>,
84    ) -> AsyncInstrumentBuilder<'_, ObservableCounter<u64>, u64> {
85        AsyncInstrumentBuilder::new(self, name.into())
86    }
87
88    /// creates an instrument builder for recording increasing values via callback.
89    pub fn f64_observable_counter(
90        &self,
91        name: impl Into<Cow<'static, str>>,
92    ) -> AsyncInstrumentBuilder<'_, ObservableCounter<f64>, f64> {
93        AsyncInstrumentBuilder::new(self, name.into())
94    }
95
96    /// creates an instrument builder for recording changes of a value.
97    pub fn i64_up_down_counter(
98        &self,
99        name: impl Into<Cow<'static, str>>,
100    ) -> InstrumentBuilder<'_, UpDownCounter<i64>> {
101        InstrumentBuilder::new(self, name.into())
102    }
103
104    /// creates an instrument builder for recording changes of a value.
105    pub fn f64_up_down_counter(
106        &self,
107        name: impl Into<Cow<'static, str>>,
108    ) -> InstrumentBuilder<'_, UpDownCounter<f64>> {
109        InstrumentBuilder::new(self, name.into())
110    }
111
112    /// creates an instrument builder for recording changes of a value via callback.
113    pub fn i64_observable_up_down_counter(
114        &self,
115        name: impl Into<Cow<'static, str>>,
116    ) -> AsyncInstrumentBuilder<'_, ObservableUpDownCounter<i64>, i64> {
117        AsyncInstrumentBuilder::new(self, name.into())
118    }
119
120    /// creates an instrument builder for recording changes of a value via callback.
121    pub fn f64_observable_up_down_counter(
122        &self,
123        name: impl Into<Cow<'static, str>>,
124    ) -> AsyncInstrumentBuilder<'_, ObservableUpDownCounter<f64>, f64> {
125        AsyncInstrumentBuilder::new(self, name.into())
126    }
127
128    /// creates an instrument builder for recording the current value via callback.
129    pub fn u64_observable_gauge(
130        &self,
131        name: impl Into<Cow<'static, str>>,
132    ) -> AsyncInstrumentBuilder<'_, ObservableGauge<u64>, u64> {
133        AsyncInstrumentBuilder::new(self, name.into())
134    }
135
136    /// creates an instrument builder for recording the current value via callback.
137    pub fn i64_observable_gauge(
138        &self,
139        name: impl Into<Cow<'static, str>>,
140    ) -> AsyncInstrumentBuilder<'_, ObservableGauge<i64>, i64> {
141        AsyncInstrumentBuilder::new(self, name.into())
142    }
143
144    /// creates an instrument builder for recording the current value via callback.
145    pub fn f64_observable_gauge(
146        &self,
147        name: impl Into<Cow<'static, str>>,
148    ) -> AsyncInstrumentBuilder<'_, ObservableGauge<f64>, f64> {
149        AsyncInstrumentBuilder::new(self, name.into())
150    }
151
152    /// creates an instrument builder for recording a distribution of values.
153    pub fn f64_histogram(
154        &self,
155        name: impl Into<Cow<'static, str>>,
156    ) -> InstrumentBuilder<'_, Histogram<f64>> {
157        InstrumentBuilder::new(self, name.into())
158    }
159
160    /// creates an instrument builder for recording a distribution of values.
161    pub fn u64_histogram(
162        &self,
163        name: impl Into<Cow<'static, str>>,
164    ) -> InstrumentBuilder<'_, Histogram<u64>> {
165        InstrumentBuilder::new(self, name.into())
166    }
167
168    /// creates an instrument builder for recording a distribution of values.
169    pub fn i64_histogram(
170        &self,
171        name: impl Into<Cow<'static, str>>,
172    ) -> InstrumentBuilder<'_, Histogram<i64>> {
173        InstrumentBuilder::new(self, name.into())
174    }
175
176    /// Registers a callback to be called during the collection of a measurement
177    /// cycle.
178    ///
179    /// The instruments passed as arguments to be registered are the only
180    /// instruments that may observe values.
181    ///
182    /// If no instruments are passed, the callback will not be registered.
183    pub fn register_callback<F>(
184        &self,
185        instruments: &[Arc<dyn Any>],
186        callback: F,
187    ) -> Result<Box<dyn CallbackRegistration>>
188    where
189        F: Fn(&dyn Observer) + Send + Sync + 'static,
190    {
191        self.instrument_provider
192            .register_callback(instruments, Box::new(callback))
193    }
194}
195
196/// A token representing the unique registration of a callback for a set of
197/// instruments with a [Meter].
198pub trait CallbackRegistration: Send + Sync {
199    /// Removes the callback registration from its associated [Meter].
200    fn unregister(&mut self) -> Result<()>;
201}
202
203/// Records measurements for multiple instruments in a callback.
204pub trait Observer {
205    /// Records the f64 value with attributes for the observable.
206    fn observe_f64(&self, inst: &dyn AsyncInstrument<f64>, measurement: f64, attrs: &[KeyValue]);
207
208    /// Records the u64 value with attributes for the observable.
209    fn observe_u64(&self, inst: &dyn AsyncInstrument<u64>, measurement: u64, attrs: &[KeyValue]);
210
211    /// Records the i64 value with attributes for the observable.
212    fn observe_i64(&self, inst: &dyn AsyncInstrument<i64>, measurement: i64, attrs: &[KeyValue]);
213}
214
215impl fmt::Debug for Meter {
216    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
217        f.write_str("Meter")
218    }
219}