opentelemetry_spanprocessor_any/metrics/
noop.rs

1//! # No-op OpenTelemetry Metrics Implementation
2//!
3//! This implementation is returned as the global Meter if no `Meter`
4//! has been set. It is also useful for testing purposes as it is intended
5//! to have minimal resource utilization and runtime impact.
6use crate::{
7    metrics::{
8        sdk_api::{
9            AsyncInstrumentCore, InstrumentCore, MeterCore, SyncBoundInstrumentCore,
10            SyncInstrumentCore,
11        },
12        AsyncRunner, Descriptor, InstrumentKind, Measurement, Meter, MeterProvider, Number,
13        NumberKind, Result,
14    },
15    Context, KeyValue,
16};
17use std::any::Any;
18use std::sync::Arc;
19
20lazy_static::lazy_static! {
21    static ref NOOP_DESCRIPTOR: Descriptor = Descriptor::new(String::new(), "noop", None, InstrumentKind::Counter, NumberKind::U64);
22}
23
24/// A no-op instance of a `MetricProvider`
25#[derive(Debug, Default)]
26pub struct NoopMeterProvider {
27    _private: (),
28}
29
30impl NoopMeterProvider {
31    /// Create a new no-op meter provider.
32    pub fn new() -> Self {
33        NoopMeterProvider { _private: () }
34    }
35}
36
37impl MeterProvider for NoopMeterProvider {
38    fn meter(&self, name: &'static str, version: Option<&'static str>) -> Meter {
39        Meter::new(name, version, Arc::new(NoopMeterCore::new()))
40    }
41}
42
43/// A no-op instance of a `Meter`
44#[derive(Debug, Default)]
45pub struct NoopMeterCore {
46    _private: (),
47}
48
49impl NoopMeterCore {
50    /// Create a new no-op meter core.
51    pub fn new() -> Self {
52        NoopMeterCore { _private: () }
53    }
54}
55
56impl MeterCore for NoopMeterCore {
57    fn new_sync_instrument(&self, _descriptor: Descriptor) -> Result<Arc<dyn SyncInstrumentCore>> {
58        Ok(Arc::new(NoopSyncInstrument::new()))
59    }
60
61    fn new_async_instrument(
62        &self,
63        _descriptor: Descriptor,
64        _runner: Option<AsyncRunner>,
65    ) -> Result<Arc<dyn AsyncInstrumentCore>> {
66        Ok(Arc::new(NoopAsyncInstrument::new()))
67    }
68
69    fn record_batch_with_context(
70        &self,
71        _cx: &Context,
72        _attributes: &[KeyValue],
73        _measurements: Vec<Measurement>,
74    ) {
75        // Ignored
76    }
77
78    fn new_batch_observer(&self, _runner: AsyncRunner) -> Result<()> {
79        Ok(())
80    }
81}
82
83/// A no-op sync instrument
84#[derive(Debug, Default)]
85pub struct NoopSyncInstrument {
86    _private: (),
87}
88
89impl NoopSyncInstrument {
90    /// Create a new no-op sync instrument
91    pub fn new() -> Self {
92        NoopSyncInstrument { _private: () }
93    }
94}
95
96impl InstrumentCore for NoopSyncInstrument {
97    fn descriptor(&self) -> &Descriptor {
98        &NOOP_DESCRIPTOR
99    }
100}
101
102impl SyncInstrumentCore for NoopSyncInstrument {
103    fn bind(&self, _attributes: &'_ [KeyValue]) -> Arc<dyn SyncBoundInstrumentCore> {
104        Arc::new(NoopBoundSyncInstrument::new())
105    }
106    fn record_one(&self, _number: Number, _attributes: &'_ [KeyValue]) {
107        // Ignored
108    }
109    fn as_any(&self) -> &dyn Any {
110        self
111    }
112}
113
114/// A no-op bound sync instrument
115#[derive(Debug, Default)]
116pub struct NoopBoundSyncInstrument {
117    _private: (),
118}
119
120impl NoopBoundSyncInstrument {
121    /// Create a new no-op bound sync instrument
122    pub fn new() -> Self {
123        NoopBoundSyncInstrument { _private: () }
124    }
125}
126
127impl SyncBoundInstrumentCore for NoopBoundSyncInstrument {
128    fn record_one(&self, _number: Number) {
129        // Ignored
130    }
131}
132
133/// A no-op async instrument.
134#[derive(Debug, Default)]
135pub struct NoopAsyncInstrument {
136    _private: (),
137}
138
139impl NoopAsyncInstrument {
140    /// Create a new no-op async instrument
141    pub fn new() -> Self {
142        NoopAsyncInstrument { _private: () }
143    }
144}
145
146impl InstrumentCore for NoopAsyncInstrument {
147    fn descriptor(&self) -> &Descriptor {
148        &NOOP_DESCRIPTOR
149    }
150}
151
152impl AsyncInstrumentCore for NoopAsyncInstrument {
153    fn as_any(&self) -> &dyn Any {
154        self
155    }
156}