aws_smithy_observability/
noop.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6//! An noop implementation of the Meter traits
7
8use std::marker::PhantomData;
9use std::{fmt::Debug, sync::Arc};
10
11use crate::instruments::{
12    AsyncInstrumentBuilder, AsyncMeasure, Histogram, InstrumentBuilder, MonotonicCounter,
13    ProvideInstrument, UpDownCounter,
14};
15use crate::{
16    attributes::Attributes,
17    context::Context,
18    meter::{Meter, ProvideMeter},
19};
20
21/// A no-op implementation of [`ProvideMeter`] that creates no-op meters.
22///
23/// This provider is useful for testing or when observability is disabled.
24#[derive(Debug)]
25pub struct NoopMeterProvider;
26impl ProvideMeter for NoopMeterProvider {
27    fn get_meter(&self, _scope: &'static str, _attributes: Option<&Attributes>) -> Meter {
28        Meter::new(Arc::new(NoopMeter))
29    }
30
31    fn as_any(&self) -> &dyn std::any::Any {
32        self
33    }
34}
35
36#[derive(Debug)]
37pub(crate) struct NoopMeter;
38impl ProvideInstrument for NoopMeter {
39    fn create_gauge(
40        &self,
41        _builder: AsyncInstrumentBuilder<'_, Arc<dyn AsyncMeasure<Value = f64>>, f64>,
42    ) -> Arc<dyn AsyncMeasure<Value = f64>> {
43        Arc::new(NoopAsyncMeasurement(PhantomData::<f64>))
44    }
45
46    fn create_up_down_counter(
47        &self,
48        _builder: InstrumentBuilder<'_, Arc<dyn UpDownCounter>>,
49    ) -> Arc<dyn UpDownCounter> {
50        Arc::new(NoopUpDownCounter)
51    }
52
53    fn create_async_up_down_counter(
54        &self,
55        _builder: AsyncInstrumentBuilder<'_, Arc<dyn AsyncMeasure<Value = i64>>, i64>,
56    ) -> Arc<dyn AsyncMeasure<Value = i64>> {
57        Arc::new(NoopAsyncMeasurement(PhantomData::<i64>))
58    }
59
60    fn create_monotonic_counter(
61        &self,
62        _builder: InstrumentBuilder<'_, Arc<dyn MonotonicCounter>>,
63    ) -> Arc<dyn MonotonicCounter> {
64        Arc::new(NoopMonotonicCounter)
65    }
66
67    fn create_async_monotonic_counter(
68        &self,
69        _builder: AsyncInstrumentBuilder<'_, Arc<dyn AsyncMeasure<Value = u64>>, u64>,
70    ) -> Arc<dyn AsyncMeasure<Value = u64>> {
71        Arc::new(NoopAsyncMeasurement(PhantomData::<u64>))
72    }
73
74    fn create_histogram(
75        &self,
76        _builder: InstrumentBuilder<'_, Arc<dyn Histogram>>,
77    ) -> Arc<dyn Histogram> {
78        Arc::new(NoopHistogram)
79    }
80}
81
82#[derive(Debug)]
83struct NoopAsyncMeasurement<T: Send + Sync + Debug>(PhantomData<T>);
84impl<T: Send + Sync + Debug> AsyncMeasure for NoopAsyncMeasurement<T> {
85    type Value = T;
86
87    fn record(&self, _value: T, _attributes: Option<&Attributes>, _context: Option<&dyn Context>) {}
88
89    fn stop(&self) {}
90}
91
92#[derive(Debug)]
93struct NoopUpDownCounter;
94impl UpDownCounter for NoopUpDownCounter {
95    fn add(&self, _value: i64, _attributes: Option<&Attributes>, _context: Option<&dyn Context>) {}
96}
97
98#[derive(Debug)]
99struct NoopMonotonicCounter;
100impl MonotonicCounter for NoopMonotonicCounter {
101    fn add(&self, _value: u64, _attributes: Option<&Attributes>, _context: Option<&dyn Context>) {}
102}
103
104#[derive(Debug)]
105struct NoopHistogram;
106impl Histogram for NoopHistogram {
107    fn record(
108        &self,
109        _value: f64,
110        _attributes: Option<&Attributes>,
111        _context: Option<&dyn Context>,
112    ) {
113    }
114}