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 {
18    /// Get or create a named [Meter].
19    fn get_meter(&self, scope: &'static str, attributes: Option<&Attributes>) -> Meter;
20
21    /// Returns a reference to `self` as `&dyn Any` for downcasting.
22    ///
23    /// The default implementation returns a reference to the unit type,
24    /// which will fail any downcast attempts. Implementations should override
25    /// this to return `self` for proper type inspection.
26    fn as_any(&self) -> &dyn std::any::Any {
27        &()
28    }
29
30    /// Returns the name of this provider implementation.
31    /// This is used for feature tracking without requiring type imports.
32    fn provider_name(&self) -> &'static str {
33        "unknown"
34    }
35}
36
37/// The entry point to creating instruments. A grouping of related metrics.
38#[derive(Clone)]
39pub struct Meter {
40    pub(crate) instrument_provider: Arc<dyn ProvideInstrument + Send + Sync>,
41}
42
43impl Meter {
44    /// Create a new [Meter] from an [ProvideInstrument]
45    pub fn new(instrument_provider: Arc<dyn ProvideInstrument + Send + Sync>) -> Self {
46        Meter {
47            instrument_provider,
48        }
49    }
50
51    /// Create a new Gauge.
52    #[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    /// Create a new [UpDownCounter].
65    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    /// Create a new AsyncUpDownCounter.
73    #[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    /// Create a new [MonotonicCounter].
86    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    /// Create a new AsyncMonotonicCounter.
94    #[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    /// Create a new [Histogram].
107    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}