opentelemetry/metrics/
noop.rs

1//! # No-op OpenTelemetry Metrics Implementation
2//!
3//! This implementation is returned as the global Meter if no `MeterProvider`
4//! has been set. It is expected to have minimal resource utilization and
5//! runtime impact.
6use crate::{
7    metrics::{
8        AsyncInstrument, CallbackRegistration, InstrumentProvider, Meter, MeterProvider, Observer,
9        Result, SyncCounter, SyncGauge, SyncHistogram, SyncUpDownCounter,
10    },
11    KeyValue,
12};
13use std::{any::Any, borrow::Cow, sync::Arc};
14
15/// A no-op instance of a `MetricProvider`
16#[derive(Debug, Default)]
17pub struct NoopMeterProvider {
18    _private: (),
19}
20
21impl NoopMeterProvider {
22    /// Create a new no-op meter provider.
23    pub fn new() -> Self {
24        NoopMeterProvider { _private: () }
25    }
26}
27
28impl MeterProvider for NoopMeterProvider {
29    fn versioned_meter(
30        &self,
31        _name: impl Into<Cow<'static, str>>,
32        _version: Option<impl Into<Cow<'static, str>>>,
33        _schema_url: Option<impl Into<Cow<'static, str>>>,
34        _attributes: Option<Vec<KeyValue>>,
35    ) -> Meter {
36        Meter::new(Arc::new(NoopMeterCore::new()))
37    }
38}
39
40/// A no-op instance of a `Meter`
41#[derive(Debug, Default)]
42pub struct NoopMeterCore {
43    _private: (),
44}
45
46impl NoopMeterCore {
47    /// Create a new no-op meter core.
48    pub fn new() -> Self {
49        NoopMeterCore { _private: () }
50    }
51}
52
53impl InstrumentProvider for NoopMeterCore {
54    fn register_callback(
55        &self,
56        _instruments: &[Arc<dyn Any>],
57        _callback: Box<dyn Fn(&dyn Observer) + Send + Sync>,
58    ) -> Result<Box<dyn CallbackRegistration>> {
59        Ok(Box::new(NoopRegistration::new()))
60    }
61}
62
63/// A no-op instance of a callback [CallbackRegistration].
64#[derive(Debug, Default)]
65pub struct NoopRegistration {
66    _private: (),
67}
68
69impl NoopRegistration {
70    /// Create a new no-op registration.
71    pub fn new() -> Self {
72        NoopRegistration { _private: () }
73    }
74}
75
76impl CallbackRegistration for NoopRegistration {
77    fn unregister(&mut self) -> Result<()> {
78        Ok(())
79    }
80}
81
82/// A no-op sync instrument
83#[derive(Debug, Default)]
84pub struct NoopSyncInstrument {
85    _private: (),
86}
87
88impl NoopSyncInstrument {
89    /// Create a new no-op sync instrument
90    pub fn new() -> Self {
91        NoopSyncInstrument { _private: () }
92    }
93}
94
95impl<T> SyncCounter<T> for NoopSyncInstrument {
96    fn add(&self, _value: T, _attributes: &[KeyValue]) {
97        // Ignored
98    }
99}
100
101impl<T> SyncUpDownCounter<T> for NoopSyncInstrument {
102    fn add(&self, _value: T, _attributes: &[KeyValue]) {
103        // Ignored
104    }
105}
106
107impl<T> SyncHistogram<T> for NoopSyncInstrument {
108    fn record(&self, _value: T, _attributes: &[KeyValue]) {
109        // Ignored
110    }
111}
112
113impl<T> SyncGauge<T> for NoopSyncInstrument {
114    fn record(&self, _value: T, _attributes: &[KeyValue]) {
115        // Ignored
116    }
117}
118
119/// A no-op async instrument.
120#[derive(Debug, Default)]
121pub struct NoopAsyncInstrument {
122    _private: (),
123}
124
125impl NoopAsyncInstrument {
126    /// Create a new no-op async instrument
127    pub fn new() -> Self {
128        NoopAsyncInstrument { _private: () }
129    }
130}
131
132impl<T> AsyncInstrument<T> for NoopAsyncInstrument {
133    fn observe(&self, _value: T, _attributes: &[KeyValue]) {
134        // Ignored
135    }
136
137    fn as_any(&self) -> Arc<dyn Any> {
138        Arc::new(())
139    }
140}