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    /// This allows type-based identification of meter providers.
23    fn as_any(&self) -> &dyn std::any::Any;
24
25    /// Returns the name of this provider implementation.
26    /// This is used for feature tracking without requiring type imports.
27    fn provider_name(&self) -> &'static str {
28        "unknown"
29    }
30}
31
32/// The entry point to creating instruments. A grouping of related metrics.
33#[derive(Clone)]
34pub struct Meter {
35    pub(crate) instrument_provider: Arc<dyn ProvideInstrument + Send + Sync>,
36}
37
38impl Meter {
39    /// Create a new [Meter] from an [ProvideInstrument]
40    pub fn new(instrument_provider: Arc<dyn ProvideInstrument + Send + Sync>) -> Self {
41        Meter {
42            instrument_provider,
43        }
44    }
45
46    /// Create a new Gauge.
47    #[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    /// Create a new [UpDownCounter].
60    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    /// Create a new AsyncUpDownCounter.
68    #[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    /// Create a new [MonotonicCounter].
81    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    /// Create a new AsyncMonotonicCounter.
89    #[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    /// Create a new [Histogram].
102    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}