aws_smithy_observability/
meter.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6//! Metrics are used to gain insight into the operational performance and health of a system in
7//! real time.
8
9use 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
16/// Provides named instances of [Meter].
17pub trait ProvideMeter: Send + Sync + Debug + 'static {
18    /// Get or create a named [Meter].
19    fn get_meter(&self, scope: &'static str, attributes: Option<&Attributes>) -> Meter;
20
21    /// Downcast to `Any` for type inspection.
22    ///
23    /// The default implementation returns a reference to `()`, which will fail
24    /// any downcast attempts. Implementors should override this method to return
25    /// `self` for proper type inspection support.
26    fn as_any(&self) -> &dyn std::any::Any {
27        &()
28    }
29}
30
31/// The entry point to creating instruments. A grouping of related metrics.
32#[derive(Clone)]
33pub struct Meter {
34    pub(crate) instrument_provider: Arc<dyn ProvideInstrument + Send + Sync>,
35}
36
37impl Meter {
38    /// Create a new [Meter] from an [ProvideInstrument]
39    pub fn new(instrument_provider: Arc<dyn ProvideInstrument + Send + Sync>) -> Self {
40        Meter {
41            instrument_provider,
42        }
43    }
44
45    /// Create a new Gauge.
46    #[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    /// Create a new [UpDownCounter].
59    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    /// Create a new AsyncUpDownCounter.
67    #[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    /// Create a new [MonotonicCounter].
80    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    /// Create a new AsyncMonotonicCounter.
88    #[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    /// Create a new [Histogram].
101    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}