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