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
32#[derive(Debug)]
33pub(crate) struct NoopMeter;
34impl ProvideInstrument for NoopMeter {
35    fn create_gauge(
36        &self,
37        _builder: AsyncInstrumentBuilder<'_, Arc<dyn AsyncMeasure<Value = f64>>, f64>,
38    ) -> Arc<dyn AsyncMeasure<Value = f64>> {
39        Arc::new(NoopAsyncMeasurement(PhantomData::<f64>))
40    }
41
42    fn create_up_down_counter(
43        &self,
44        _builder: InstrumentBuilder<'_, Arc<dyn UpDownCounter>>,
45    ) -> Arc<dyn UpDownCounter> {
46        Arc::new(NoopUpDownCounter)
47    }
48
49    fn create_async_up_down_counter(
50        &self,
51        _builder: AsyncInstrumentBuilder<'_, Arc<dyn AsyncMeasure<Value = i64>>, i64>,
52    ) -> Arc<dyn AsyncMeasure<Value = i64>> {
53        Arc::new(NoopAsyncMeasurement(PhantomData::<i64>))
54    }
55
56    fn create_monotonic_counter(
57        &self,
58        _builder: InstrumentBuilder<'_, Arc<dyn MonotonicCounter>>,
59    ) -> Arc<dyn MonotonicCounter> {
60        Arc::new(NoopMonotonicCounter)
61    }
62
63    fn create_async_monotonic_counter(
64        &self,
65        _builder: AsyncInstrumentBuilder<'_, Arc<dyn AsyncMeasure<Value = u64>>, u64>,
66    ) -> Arc<dyn AsyncMeasure<Value = u64>> {
67        Arc::new(NoopAsyncMeasurement(PhantomData::<u64>))
68    }
69
70    fn create_histogram(
71        &self,
72        _builder: InstrumentBuilder<'_, Arc<dyn Histogram>>,
73    ) -> Arc<dyn Histogram> {
74        Arc::new(NoopHistogram)
75    }
76}
77
78#[derive(Debug)]
79struct NoopAsyncMeasurement<T: Send + Sync + Debug>(PhantomData<T>);
80impl<T: Send + Sync + Debug> AsyncMeasure for NoopAsyncMeasurement<T> {
81    type Value = T;
82
83    fn record(&self, _value: T, _attributes: Option<&Attributes>, _context: Option<&dyn Context>) {}
84
85    fn stop(&self) {}
86}
87
88#[derive(Debug)]
89struct NoopUpDownCounter;
90impl UpDownCounter for NoopUpDownCounter {
91    fn add(&self, _value: i64, _attributes: Option<&Attributes>, _context: Option<&dyn Context>) {}
92}
93
94#[derive(Debug)]
95struct NoopMonotonicCounter;
96impl MonotonicCounter for NoopMonotonicCounter {
97    fn add(&self, _value: u64, _attributes: Option<&Attributes>, _context: Option<&dyn Context>) {}
98}
99
100#[derive(Debug)]
101struct NoopHistogram;
102impl Histogram for NoopHistogram {
103    fn record(
104        &self,
105        _value: f64,
106        _attributes: Option<&Attributes>,
107        _context: Option<&dyn Context>,
108    ) {
109    }
110}