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
12use super::instruments::SyncInstrument;
13
14/// A no-op instance of a `MetricProvider`
15#[derive(Debug, Default)]
16pub(crate) struct NoopMeterProvider {
17    _private: (),
18}
19
20impl NoopMeterProvider {
21    /// Create a new no-op meter provider.
22    pub(crate) fn new() -> Self {
23        NoopMeterProvider { _private: () }
24    }
25}
26
27impl MeterProvider for NoopMeterProvider {
28    fn meter_with_scope(&self, scope: crate::InstrumentationScope) -> Meter {
29        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.");
30        Meter::new(Arc::new(NoopMeter::new()))
31    }
32}
33
34/// A no-op instance of a `Meter`
35#[derive(Debug, Default)]
36pub(crate) struct NoopMeter {
37    _private: (),
38}
39
40impl NoopMeter {
41    /// Create a new no-op meter core.
42    pub(crate) fn new() -> Self {
43        NoopMeter { _private: () }
44    }
45}
46
47impl InstrumentProvider for NoopMeter {}
48
49/// A no-op sync instrument
50#[derive(Debug, Default)]
51pub(crate) struct NoopSyncInstrument {
52    _private: (),
53}
54
55impl NoopSyncInstrument {
56    /// Create a new no-op sync instrument
57    pub(crate) fn new() -> Self {
58        NoopSyncInstrument { _private: () }
59    }
60}
61
62impl<T> SyncInstrument<T> for NoopSyncInstrument {
63    fn measure(&self, _value: T, _attributes: &[KeyValue]) {
64        // Ignored
65    }
66}