opentelemetry_spanprocessor_any/metrics/
observer.rs

1use crate::metrics::{
2    sdk_api, AsyncRunner, Descriptor, InstrumentKind, Meter, Number, NumberKind, Observation,
3    Result, Unit,
4};
5use std::sync::Arc;
6
7/// An Observer callback that can report observations for multiple instruments.
8#[derive(Debug)]
9pub struct BatchObserver<'a> {
10    meter: &'a Meter,
11}
12
13impl<'a> BatchObserver<'a> {
14    pub(crate) fn new(meter: &'a Meter) -> Self {
15        BatchObserver { meter }
16    }
17
18    /// Creates a new integer `SumObserverBuilder` for `u64` values with the given name.
19    pub fn u64_sum_observer<T>(&self, name: T) -> SumObserverBuilder<'_, u64>
20    where
21        T: Into<String>,
22    {
23        SumObserverBuilder::new(self.meter, name.into(), None, NumberKind::U64)
24    }
25
26    /// Creates a new floating point `SumObserverBuilder` for `f64` values with the given name.
27    pub fn f64_sum_observer<T>(&self, name: T) -> SumObserverBuilder<'_, f64>
28    where
29        T: Into<String>,
30    {
31        SumObserverBuilder::new(self.meter, name.into(), None, NumberKind::F64)
32    }
33
34    /// Creates a new integer `UpDownSumObserverBuilder` for `i64` values with the given name.
35    pub fn i64_up_down_sum_observer<T>(&self, name: T) -> UpDownSumObserverBuilder<'_, i64>
36    where
37        T: Into<String>,
38    {
39        UpDownSumObserverBuilder::new(self.meter, name.into(), None, NumberKind::I64)
40    }
41
42    /// Creates a new floating point `UpDownSumObserverBuilder` for `f64` values with the given name.
43    pub fn f64_up_down_sum_observer<T>(&self, name: T) -> UpDownSumObserverBuilder<'_, f64>
44    where
45        T: Into<String>,
46    {
47        UpDownSumObserverBuilder::new(self.meter, name.into(), None, NumberKind::F64)
48    }
49
50    /// Creates a new integer `ValueObserverBuilder` for `u64` values with the given name.
51    pub fn u64_value_observer<T>(&self, name: T) -> ValueObserverBuilder<'_, u64>
52    where
53        T: Into<String>,
54    {
55        ValueObserverBuilder::new(self.meter, name.into(), None, NumberKind::U64)
56    }
57
58    /// Creates a new integer `ValueObserverBuilder` for `i64` values with the given name.
59    pub fn i64_value_observer<T>(&self, name: T) -> ValueObserverBuilder<'_, i64>
60    where
61        T: Into<String>,
62    {
63        ValueObserverBuilder::new(self.meter, name.into(), None, NumberKind::I64)
64    }
65
66    /// Creates a new floating point `ValueObserverBuilder` for `f64` values with the given name.
67    pub fn f64_value_observer<T>(&self, name: T) -> ValueObserverBuilder<'_, f64>
68    where
69        T: Into<String>,
70    {
71        ValueObserverBuilder::new(self.meter, name.into(), None, NumberKind::F64)
72    }
73}
74
75/// A metric that captures a precomputed sum of values at a point in time.
76#[derive(Debug)]
77pub struct SumObserver<T> {
78    instrument: Arc<dyn sdk_api::AsyncInstrumentCore>,
79    _marker: std::marker::PhantomData<T>,
80}
81
82impl<T> SumObserver<T>
83where
84    T: Into<Number>,
85{
86    /// Returns an `Observation`: a `BatchObserverCallback` argument, for an
87    /// asynchronous instrument. This returns an implementation-level
88    /// object for use by the SDK, users should not refer to this.
89    pub fn observation(&self, value: T) -> Observation {
90        Observation::new(value.into(), self.instrument.clone())
91    }
92}
93
94/// Configuration options for building a `SumObserver`
95#[derive(Debug)]
96pub struct SumObserverBuilder<'a, T> {
97    meter: &'a Meter,
98    descriptor: Descriptor,
99    runner: Option<AsyncRunner>,
100    _marker: std::marker::PhantomData<T>,
101}
102
103impl<'a, T> SumObserverBuilder<'a, T> {
104    pub(crate) fn new(
105        meter: &'a Meter,
106        name: String,
107        runner: Option<AsyncRunner>,
108        number_kind: NumberKind,
109    ) -> Self {
110        SumObserverBuilder {
111            meter,
112            descriptor: Descriptor::new(
113                name,
114                meter.instrumentation_library().name,
115                meter.instrumentation_library().version,
116                InstrumentKind::SumObserver,
117                number_kind,
118            ),
119            runner,
120            _marker: std::marker::PhantomData,
121        }
122    }
123
124    /// Set the description of this `SumObserver`
125    pub fn with_description<S: Into<String>>(mut self, description: S) -> Self {
126        self.descriptor.set_description(description.into());
127        self
128    }
129
130    /// Set the unit for this `SumObserver`.
131    pub fn with_unit(mut self, unit: Unit) -> Self {
132        self.descriptor.config.unit = Some(unit);
133        self
134    }
135
136    /// Create a `SumObserver` from this configuration.
137    pub fn try_init(self) -> Result<SumObserver<T>> {
138        let instrument = self
139            .meter
140            .new_async_instrument(self.descriptor, self.runner)?;
141
142        Ok(SumObserver {
143            instrument,
144            _marker: std::marker::PhantomData,
145        })
146    }
147
148    /// Create a `SumObserver` from this configuration.
149    ///
150    /// # Panics
151    ///
152    /// This method panics if it cannot create an instrument with the provided
153    /// config. If you want to handle results instead, use [`try_init`]
154    ///
155    /// [`try_init`]: SumObserverBuilder::try_init()
156    pub fn init(self) -> SumObserver<T> {
157        SumObserver {
158            instrument: self
159                .meter
160                .new_async_instrument(self.descriptor, self.runner)
161                .unwrap(),
162            _marker: std::marker::PhantomData,
163        }
164    }
165}
166
167/// A metric that captures a precomputed non-monotonic sum of values at a point
168/// in time.
169#[derive(Debug)]
170pub struct UpDownSumObserver<T> {
171    instrument: Arc<dyn sdk_api::AsyncInstrumentCore>,
172    _marker: std::marker::PhantomData<T>,
173}
174
175impl<T> UpDownSumObserver<T>
176where
177    T: Into<Number>,
178{
179    /// Returns an `Observation`: a `BatchObserverCallback` argument, for an
180    /// asynchronous instrument. This returns an implementation-level
181    /// object for use by the SDK, users should not refer to this.
182    pub fn observation(&self, value: T) -> Observation {
183        Observation::new(value.into(), self.instrument.clone())
184    }
185}
186
187/// Configuration options for building a `UpDownSumObserver`
188#[derive(Debug)]
189pub struct UpDownSumObserverBuilder<'a, T> {
190    meter: &'a Meter,
191    descriptor: Descriptor,
192    runner: Option<AsyncRunner>,
193    _marker: std::marker::PhantomData<T>,
194}
195
196impl<'a, T> UpDownSumObserverBuilder<'a, T> {
197    pub(crate) fn new(
198        meter: &'a Meter,
199        name: String,
200        runner: Option<AsyncRunner>,
201        number_kind: NumberKind,
202    ) -> Self {
203        UpDownSumObserverBuilder {
204            meter,
205            descriptor: Descriptor::new(
206                name,
207                meter.instrumentation_library().name,
208                meter.instrumentation_library().version,
209                InstrumentKind::UpDownSumObserver,
210                number_kind,
211            ),
212            runner,
213            _marker: std::marker::PhantomData,
214        }
215    }
216
217    /// Set the description of this `UpDownSumObserver`
218    pub fn with_description<S: Into<String>>(mut self, description: S) -> Self {
219        self.descriptor.set_description(description.into());
220        self
221    }
222
223    /// Set the unit for this `UpDownSumObserver`.
224    pub fn with_unit(mut self, unit: Unit) -> Self {
225        self.descriptor.config.unit = Some(unit);
226        self
227    }
228
229    /// Create a `UpDownSumObserver` from this configuration.
230    pub fn try_init(self) -> Result<UpDownSumObserver<T>> {
231        let instrument = self
232            .meter
233            .new_async_instrument(self.descriptor, self.runner)?;
234
235        Ok(UpDownSumObserver {
236            instrument,
237            _marker: std::marker::PhantomData,
238        })
239    }
240
241    /// Create a `UpDownSumObserver` from this configuration.
242    ///
243    /// # Panics
244    ///
245    /// This method panics if it cannot create an instrument with the provided
246    /// config. If you want to handle results instead, use [`try_init`]
247    ///
248    /// [`try_init`]: UpDownSumObserverBuilder::try_init()
249    pub fn init(self) -> UpDownSumObserver<T> {
250        UpDownSumObserver {
251            instrument: self
252                .meter
253                .new_async_instrument(self.descriptor, self.runner)
254                .unwrap(),
255            _marker: std::marker::PhantomData,
256        }
257    }
258}
259
260/// A metric that captures a set of values at a point in time.
261#[derive(Debug)]
262pub struct ValueObserver<T> {
263    instrument: Arc<dyn sdk_api::AsyncInstrumentCore>,
264    _marker: std::marker::PhantomData<T>,
265}
266
267impl<T> ValueObserver<T>
268where
269    T: Into<Number>,
270{
271    /// Returns an `Observation`: a `BatchObserverCallback` argument, for an
272    /// asynchronous instrument. This returns an implementation-level
273    /// object for use by the SDK, users should not refer to this.
274    pub fn observation(&self, value: T) -> Observation {
275        Observation::new(value.into(), self.instrument.clone())
276    }
277}
278
279/// Configuration options for building a `ValueObserver`
280#[derive(Debug)]
281pub struct ValueObserverBuilder<'a, T> {
282    meter: &'a Meter,
283    descriptor: Descriptor,
284    runner: Option<AsyncRunner>,
285    _marker: std::marker::PhantomData<T>,
286}
287
288impl<'a, T> ValueObserverBuilder<'a, T> {
289    pub(crate) fn new(
290        meter: &'a Meter,
291        name: String,
292        runner: Option<AsyncRunner>,
293        number_kind: NumberKind,
294    ) -> Self {
295        ValueObserverBuilder {
296            meter,
297            descriptor: Descriptor::new(
298                name,
299                meter.instrumentation_library().name,
300                meter.instrumentation_library().version,
301                InstrumentKind::ValueObserver,
302                number_kind,
303            ),
304            runner,
305            _marker: std::marker::PhantomData,
306        }
307    }
308    /// Set the description of this `ValueObserver`
309    pub fn with_description<S: Into<String>>(mut self, description: S) -> Self {
310        self.descriptor.set_description(description.into());
311        self
312    }
313
314    /// Set the unit for this `ValueObserver`.
315    pub fn with_unit(mut self, unit: Unit) -> Self {
316        self.descriptor.config.unit = Some(unit);
317        self
318    }
319
320    /// Create a `ValueObserver` from this configuration.
321    pub fn try_init(self) -> Result<ValueObserver<T>> {
322        let instrument = self
323            .meter
324            .new_async_instrument(self.descriptor, self.runner)?;
325
326        Ok(ValueObserver {
327            instrument,
328            _marker: std::marker::PhantomData,
329        })
330    }
331
332    /// Create a `ValueObserver` from this configuration.
333    ///
334    /// # Panics
335    ///
336    /// This method panics if it cannot create an instrument with the provided
337    /// config. If you want to handle results instead, use [`try_init`]
338    ///
339    /// [`try_init`]: ValueObserverBuilder::try_init()
340    pub fn init(self) -> ValueObserver<T> {
341        ValueObserver {
342            instrument: self
343                .meter
344                .new_async_instrument(self.descriptor, self.runner)
345                .unwrap(),
346            _marker: std::marker::PhantomData,
347        }
348    }
349}