opentelemetry/metrics/
noop.rs
1use 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#[derive(Debug, Default)]
17pub struct NoopMeterProvider {
18 _private: (),
19}
20
21impl NoopMeterProvider {
22 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#[derive(Debug, Default)]
42pub struct NoopMeterCore {
43 _private: (),
44}
45
46impl NoopMeterCore {
47 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#[derive(Debug, Default)]
65pub struct NoopRegistration {
66 _private: (),
67}
68
69impl NoopRegistration {
70 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#[derive(Debug, Default)]
84pub struct NoopSyncInstrument {
85 _private: (),
86}
87
88impl NoopSyncInstrument {
89 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 }
99}
100
101impl<T> SyncUpDownCounter<T> for NoopSyncInstrument {
102 fn add(&self, _value: T, _attributes: &[KeyValue]) {
103 }
105}
106
107impl<T> SyncHistogram<T> for NoopSyncInstrument {
108 fn record(&self, _value: T, _attributes: &[KeyValue]) {
109 }
111}
112
113impl<T> SyncGauge<T> for NoopSyncInstrument {
114 fn record(&self, _value: T, _attributes: &[KeyValue]) {
115 }
117}
118
119#[derive(Debug, Default)]
121pub struct NoopAsyncInstrument {
122 _private: (),
123}
124
125impl NoopAsyncInstrument {
126 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 }
136
137 fn as_any(&self) -> Arc<dyn Any> {
138 Arc::new(())
139 }
140}