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