ts_opentelemetry_api/metrics/
meter.rs1use 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
14pub trait MeterProvider {
17 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 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#[derive(Clone)]
51pub struct Meter {
52 pub(crate) instrument_provider: Arc<dyn InstrumentProvider + Send + Sync>,
53}
54
55impl Meter {
56 #[doc(hidden)]
58 pub fn new(instrument_provider: Arc<dyn InstrumentProvider + Send + Sync>) -> Self {
59 Meter {
60 instrument_provider,
61 }
62 }
63
64 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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
196pub trait CallbackRegistration: Send + Sync {
199 fn unregister(&mut self) -> Result<()>;
201}
202
203pub trait Observer {
205 fn observe_f64(&self, inst: &dyn AsyncInstrument<f64>, measurement: f64, attrs: &[KeyValue]);
207
208 fn observe_u64(&self, inst: &dyn AsyncInstrument<u64>, measurement: u64, attrs: &[KeyValue]);
210
211 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}