opentelemetry_spanprocessor_any/metrics/
sync_instrument.rs

1use crate::{
2    metrics::{sdk_api, Number},
3    KeyValue,
4};
5use std::marker;
6use std::sync::Arc;
7
8/// Measurement is used for reporting a synchronous batch of metric values.
9/// Instances of this type should be created by synchronous instruments (e.g.,
10/// `Counter::measurement`).
11#[derive(Debug)]
12pub struct Measurement {
13    number: Number,
14    instrument: Arc<dyn sdk_api::SyncInstrumentCore>,
15}
16
17impl Measurement {
18    /// Create a new measurement for an instrument
19    pub(crate) fn new(number: Number, instrument: Arc<dyn sdk_api::SyncInstrumentCore>) -> Self {
20        Measurement { number, instrument }
21    }
22
23    /// The number recorded by this measurement
24    pub fn number(&self) -> &Number {
25        &self.number
26    }
27
28    /// Convert this measurement into the underlying number
29    pub fn into_number(self) -> Number {
30        self.number
31    }
32
33    /// The instrument that recorded this measurement
34    pub fn instrument(&self) -> &Arc<dyn sdk_api::SyncInstrumentCore> {
35        &self.instrument
36    }
37}
38
39/// Wrapper around a sdk-implemented sync instrument for a given type
40#[derive(Clone, Debug)]
41pub(crate) struct SyncInstrument<T> {
42    instrument: Arc<dyn sdk_api::SyncInstrumentCore>,
43    _marker: marker::PhantomData<T>,
44}
45
46impl<T> SyncInstrument<T> {
47    /// Create a new sync instrument from an sdk-implemented sync instrument
48    pub(crate) fn new(instrument: Arc<dyn sdk_api::SyncInstrumentCore>) -> Self {
49        SyncInstrument {
50            instrument,
51            _marker: marker::PhantomData,
52        }
53    }
54
55    /// Create a new bound sync instrument
56    pub(crate) fn bind(&self, attributes: &[KeyValue]) -> SyncBoundInstrument<T> {
57        let bound_instrument = self.instrument.bind(attributes);
58        SyncBoundInstrument {
59            bound_instrument,
60            _marker: marker::PhantomData,
61        }
62    }
63
64    /// Record a value directly to the underlying instrument
65    pub(crate) fn direct_record(&self, number: Number, attributes: &[KeyValue]) {
66        self.instrument.record_one(number, attributes)
67    }
68
69    /// Reference to the underlying sdk-implemented instrument
70    pub(crate) fn instrument(&self) -> &Arc<dyn sdk_api::SyncInstrumentCore> {
71        &self.instrument
72    }
73}
74
75/// Wrapper around a sdk-implemented sync bound instrument
76#[derive(Clone, Debug)]
77pub(crate) struct SyncBoundInstrument<T> {
78    bound_instrument: Arc<dyn sdk_api::SyncBoundInstrumentCore>,
79    _marker: marker::PhantomData<T>,
80}
81
82impl<T> SyncBoundInstrument<T> {
83    /// Record a value directly to the underlying instrument
84    pub(crate) fn direct_record(&self, number: Number) {
85        self.bound_instrument.record_one(number)
86    }
87}