aws_smithy_observability/
meter.rs1use crate::instruments::{
10    AsyncInstrumentBuilder, AsyncMeasure, Histogram, InstrumentBuilder, MonotonicCounter,
11    UpDownCounter,
12};
13use crate::{attributes::Attributes, instruments::ProvideInstrument};
14use std::{borrow::Cow, fmt::Debug, sync::Arc};
15
16pub trait ProvideMeter: Send + Sync + Debug {
18    fn get_meter(&self, scope: &'static str, attributes: Option<&Attributes>) -> Meter;
20}
21
22#[derive(Clone)]
24pub struct Meter {
25    pub(crate) instrument_provider: Arc<dyn ProvideInstrument + Send + Sync>,
26}
27
28impl Meter {
29    pub fn new(instrument_provider: Arc<dyn ProvideInstrument + Send + Sync>) -> Self {
31        Meter {
32            instrument_provider,
33        }
34    }
35
36    #[allow(clippy::type_complexity)]
38    pub fn create_gauge<F>(
39        &self,
40        name: impl Into<Cow<'static, str>>,
41        callback: F,
42    ) -> AsyncInstrumentBuilder<'_, Arc<dyn AsyncMeasure<Value = f64>>, f64>
43    where
44        F: Fn(&dyn AsyncMeasure<Value = f64>) + Send + Sync + 'static,
45    {
46        AsyncInstrumentBuilder::new(self, name.into(), Arc::new(callback))
47    }
48
49    pub fn create_up_down_counter(
51        &self,
52        name: impl Into<Cow<'static, str>>,
53    ) -> InstrumentBuilder<'_, Arc<dyn UpDownCounter>> {
54        InstrumentBuilder::new(self, name.into())
55    }
56
57    #[allow(clippy::type_complexity)]
59    pub fn create_async_up_down_counter<F>(
60        &self,
61        name: impl Into<Cow<'static, str>>,
62        callback: F,
63    ) -> AsyncInstrumentBuilder<'_, Arc<dyn AsyncMeasure<Value = i64>>, i64>
64    where
65        F: Fn(&dyn AsyncMeasure<Value = i64>) + Send + Sync + 'static,
66    {
67        AsyncInstrumentBuilder::new(self, name.into(), Arc::new(callback))
68    }
69
70    pub fn create_monotonic_counter(
72        &self,
73        name: impl Into<Cow<'static, str>>,
74    ) -> InstrumentBuilder<'_, Arc<dyn MonotonicCounter>> {
75        InstrumentBuilder::new(self, name.into())
76    }
77
78    #[allow(clippy::type_complexity)]
80    pub fn create_async_monotonic_counter<F>(
81        &self,
82        name: impl Into<Cow<'static, str>>,
83        callback: F,
84    ) -> AsyncInstrumentBuilder<'_, Arc<dyn AsyncMeasure<Value = u64>>, u64>
85    where
86        F: Fn(&dyn AsyncMeasure<Value = u64>) + Send + Sync + 'static,
87    {
88        AsyncInstrumentBuilder::new(self, name.into(), Arc::new(callback))
89    }
90
91    pub fn create_histogram(
93        &self,
94        name: impl Into<Cow<'static, str>>,
95    ) -> InstrumentBuilder<'_, Arc<dyn Histogram>> {
96        InstrumentBuilder::new(self, name.into())
97    }
98}