Skip to main content

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::{InstrumentProvider, Meter, MeterProvider},
8    otel_debug, KeyValue,
9};
10use std::sync::Arc;
11
12#[cfg(feature = "experimental_metrics_bound_instruments")]
13use super::instruments::BoundSyncInstrument;
14use super::instruments::SyncInstrument;
15
16/// A no-op instance of a `MetricProvider`
17#[derive(Debug, Default)]
18pub struct NoopMeterProvider {
19    _private: (),
20}
21
22impl NoopMeterProvider {
23    /// Create a new no-op meter provider.
24    pub fn new() -> Self {
25        NoopMeterProvider { _private: () }
26    }
27}
28
29impl MeterProvider for NoopMeterProvider {
30    fn meter_with_scope(&self, scope: crate::InstrumentationScope) -> Meter {
31        otel_debug!(name: "NoopMeterProvider.MeterCreation", meter_name = scope.name(), message = "Meter was obtained from a NoopMeterProvider. No metrics will be recorded. If global::meter_with_scope()/meter() was used, ensure that a valid MeterProvider is set globally before creating Meter.");
32        Meter::new(Arc::new(NoopMeter::new()))
33    }
34}
35
36/// A no-op instance of a `Meter`
37#[derive(Debug, Default)]
38pub(crate) struct NoopMeter {
39    _private: (),
40}
41
42impl NoopMeter {
43    /// Create a new no-op meter core.
44    pub(crate) fn new() -> Self {
45        NoopMeter { _private: () }
46    }
47}
48
49impl InstrumentProvider for NoopMeter {}
50
51/// A no-op sync instrument
52#[derive(Debug, Default)]
53pub(crate) struct NoopSyncInstrument {
54    _private: (),
55}
56
57impl NoopSyncInstrument {
58    /// Create a new no-op sync instrument
59    pub(crate) fn new() -> Self {
60        NoopSyncInstrument { _private: () }
61    }
62}
63
64impl<T: Send + Sync + 'static> SyncInstrument<T> for NoopSyncInstrument {
65    fn measure(&self, _value: T, _attributes: &[KeyValue]) {
66        // Ignored
67    }
68
69    #[cfg(feature = "experimental_metrics_bound_instruments")]
70    fn bind(&self, _attributes: &[KeyValue]) -> Box<dyn BoundSyncInstrument<T> + Send + Sync> {
71        Box::new(NoopBoundSyncInstrument::new())
72    }
73}
74
75#[cfg(feature = "experimental_metrics_bound_instruments")]
76pub(crate) struct NoopBoundSyncInstrument {
77    _private: (),
78}
79
80#[cfg(feature = "experimental_metrics_bound_instruments")]
81impl NoopBoundSyncInstrument {
82    pub(crate) fn new() -> Self {
83        NoopBoundSyncInstrument { _private: () }
84    }
85}
86
87#[cfg(feature = "experimental_metrics_bound_instruments")]
88impl<T> BoundSyncInstrument<T> for NoopBoundSyncInstrument {
89    fn measure(&self, _measurement: T) {
90        // Ignored
91    }
92}