1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//! # No-op OpenTelemetry Metrics Implementation
//!
//! This implementation is returned as the global Meter if no `Meter`
//! has been set. It is also useful for testing purposes as it is intended
//! to have minimal resource utilization and runtime impact.
use crate::{
    metrics::{
        sdk_api::{
            AsyncInstrumentCore, InstrumentCore, MeterCore, SyncBoundInstrumentCore,
            SyncInstrumentCore,
        },
        AsyncRunner, Descriptor, InstrumentKind, Measurement, Meter, MeterProvider, Number,
        NumberKind, Result,
    },
    Context, KeyValue,
};
use std::any::Any;
use std::sync::Arc;

lazy_static::lazy_static! {
    static ref NOOP_DESCRIPTOR: Descriptor = Descriptor::new(String::new(), "noop", None, InstrumentKind::Counter, NumberKind::U64);
}

/// A no-op instance of a `MetricProvider`
#[derive(Debug, Default)]
pub struct NoopMeterProvider {
    _private: (),
}

impl NoopMeterProvider {
    /// Create a new no-op meter provider.
    pub fn new() -> Self {
        NoopMeterProvider { _private: () }
    }
}

impl MeterProvider for NoopMeterProvider {
    fn meter(&self, name: &'static str, version: Option<&'static str>) -> Meter {
        Meter::new(name, version, Arc::new(NoopMeterCore::new()))
    }
}

/// A no-op instance of a `Meter`
#[derive(Debug, Default)]
pub struct NoopMeterCore {
    _private: (),
}

impl NoopMeterCore {
    /// Create a new no-op meter core.
    pub fn new() -> Self {
        NoopMeterCore { _private: () }
    }
}

impl MeterCore for NoopMeterCore {
    fn new_sync_instrument(&self, _descriptor: Descriptor) -> Result<Arc<dyn SyncInstrumentCore>> {
        Ok(Arc::new(NoopSyncInstrument::new()))
    }

    fn new_async_instrument(
        &self,
        _descriptor: Descriptor,
        _runner: Option<AsyncRunner>,
    ) -> Result<Arc<dyn AsyncInstrumentCore>> {
        Ok(Arc::new(NoopAsyncInstrument::new()))
    }

    fn record_batch_with_context(
        &self,
        _cx: &Context,
        _labels: &[KeyValue],
        _measurements: Vec<Measurement>,
    ) {
        // Ignored
    }

    fn new_batch_observer(&self, _runner: AsyncRunner) -> Result<()> {
        Ok(())
    }
}

/// A no-op sync instrument
#[derive(Debug, Default)]
pub struct NoopSyncInstrument {
    _private: (),
}

impl NoopSyncInstrument {
    /// Create a new no-op sync instrument
    pub fn new() -> Self {
        NoopSyncInstrument { _private: () }
    }
}

impl InstrumentCore for NoopSyncInstrument {
    fn descriptor(&self) -> &Descriptor {
        &NOOP_DESCRIPTOR
    }
}

impl SyncInstrumentCore for NoopSyncInstrument {
    fn bind(&self, _labels: &'_ [KeyValue]) -> Arc<dyn SyncBoundInstrumentCore> {
        Arc::new(NoopBoundSyncInstrument::new())
    }
    fn record_one(&self, _number: Number, _labels: &'_ [KeyValue]) {
        // Ignored
    }
    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// A no-op bound sync instrument
#[derive(Debug, Default)]
pub struct NoopBoundSyncInstrument {
    _private: (),
}

impl NoopBoundSyncInstrument {
    /// Create a new no-op bound sync instrument
    pub fn new() -> Self {
        NoopBoundSyncInstrument { _private: () }
    }
}

impl SyncBoundInstrumentCore for NoopBoundSyncInstrument {
    fn record_one(&self, _number: Number) {
        // Ignored
    }
}

/// A no-op async instrument.
#[derive(Debug, Default)]
pub struct NoopAsyncInstrument {
    _private: (),
}

impl NoopAsyncInstrument {
    /// Create a new no-op async instrument
    pub fn new() -> Self {
        NoopAsyncInstrument { _private: () }
    }
}

impl InstrumentCore for NoopAsyncInstrument {
    fn descriptor(&self) -> &Descriptor {
        &NOOP_DESCRIPTOR
    }
}

impl AsyncInstrumentCore for NoopAsyncInstrument {
    fn as_any(&self) -> &dyn Any {
        self
    }
}