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 + 'static {
18 fn get_meter(&self, scope: &'static str, attributes: Option<&Attributes>) -> Meter;
20
21 fn as_any(&self) -> &dyn std::any::Any {
27 &()
28 }
29}
30
31#[derive(Clone)]
33pub struct Meter {
34 pub(crate) instrument_provider: Arc<dyn ProvideInstrument + Send + Sync>,
35}
36
37impl Meter {
38 pub fn new(instrument_provider: Arc<dyn ProvideInstrument + Send + Sync>) -> Self {
40 Meter {
41 instrument_provider,
42 }
43 }
44
45 #[allow(clippy::type_complexity)]
47 pub fn create_gauge<F>(
48 &self,
49 name: impl Into<Cow<'static, str>>,
50 callback: F,
51 ) -> AsyncInstrumentBuilder<'_, Arc<dyn AsyncMeasure<Value = f64>>, f64>
52 where
53 F: Fn(&dyn AsyncMeasure<Value = f64>) + Send + Sync + 'static,
54 {
55 AsyncInstrumentBuilder::new(self, name.into(), Arc::new(callback))
56 }
57
58 pub fn create_up_down_counter(
60 &self,
61 name: impl Into<Cow<'static, str>>,
62 ) -> InstrumentBuilder<'_, Arc<dyn UpDownCounter>> {
63 InstrumentBuilder::new(self, name.into())
64 }
65
66 #[allow(clippy::type_complexity)]
68 pub fn create_async_up_down_counter<F>(
69 &self,
70 name: impl Into<Cow<'static, str>>,
71 callback: F,
72 ) -> AsyncInstrumentBuilder<'_, Arc<dyn AsyncMeasure<Value = i64>>, i64>
73 where
74 F: Fn(&dyn AsyncMeasure<Value = i64>) + Send + Sync + 'static,
75 {
76 AsyncInstrumentBuilder::new(self, name.into(), Arc::new(callback))
77 }
78
79 pub fn create_monotonic_counter(
81 &self,
82 name: impl Into<Cow<'static, str>>,
83 ) -> InstrumentBuilder<'_, Arc<dyn MonotonicCounter>> {
84 InstrumentBuilder::new(self, name.into())
85 }
86
87 #[allow(clippy::type_complexity)]
89 pub fn create_async_monotonic_counter<F>(
90 &self,
91 name: impl Into<Cow<'static, str>>,
92 callback: F,
93 ) -> AsyncInstrumentBuilder<'_, Arc<dyn AsyncMeasure<Value = u64>>, u64>
94 where
95 F: Fn(&dyn AsyncMeasure<Value = u64>) + Send + Sync + 'static,
96 {
97 AsyncInstrumentBuilder::new(self, name.into(), Arc::new(callback))
98 }
99
100 pub fn create_histogram(
102 &self,
103 name: impl Into<Cow<'static, str>>,
104 ) -> InstrumentBuilder<'_, Arc<dyn Histogram>> {
105 InstrumentBuilder::new(self, name.into())
106 }
107}