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    fn as_any(&self) -> &dyn std::any::Any;
23}
24
25/// The entry point to creating instruments. A grouping of related metrics.
26#[derive(Clone)]
27pub struct Meter {
28    pub(crate) instrument_provider: Arc<dyn ProvideInstrument + Send + Sync>,
29}
30
31impl Meter {
32    /// Create a new [Meter] from an [ProvideInstrument]
33    pub fn new(instrument_provider: Arc<dyn ProvideInstrument + Send + Sync>) -> Self {
34        Meter {
35            instrument_provider,
36        }
37    }
38
39    /// Create a new Gauge.
40    #[allow(clippy::type_complexity)]
41    pub fn create_gauge<F>(
42        &self,
43        name: impl Into<Cow<'static, str>>,
44        callback: F,
45    ) -> AsyncInstrumentBuilder<'_, Arc<dyn AsyncMeasure<Value = f64>>, f64>
46    where
47        F: Fn(&dyn AsyncMeasure<Value = f64>) + Send + Sync + 'static,
48    {
49        AsyncInstrumentBuilder::new(self, name.into(), Arc::new(callback))
50    }
51
52    /// Create a new [UpDownCounter].
53    pub fn create_up_down_counter(
54        &self,
55        name: impl Into<Cow<'static, str>>,
56    ) -> InstrumentBuilder<'_, Arc<dyn UpDownCounter>> {
57        InstrumentBuilder::new(self, name.into())
58    }
59
60    /// Create a new AsyncUpDownCounter.
61    #[allow(clippy::type_complexity)]
62    pub fn create_async_up_down_counter<F>(
63        &self,
64        name: impl Into<Cow<'static, str>>,
65        callback: F,
66    ) -> AsyncInstrumentBuilder<'_, Arc<dyn AsyncMeasure<Value = i64>>, i64>
67    where
68        F: Fn(&dyn AsyncMeasure<Value = i64>) + Send + Sync + 'static,
69    {
70        AsyncInstrumentBuilder::new(self, name.into(), Arc::new(callback))
71    }
72
73    /// Create a new [MonotonicCounter].
74    pub fn create_monotonic_counter(
75        &self,
76        name: impl Into<Cow<'static, str>>,
77    ) -> InstrumentBuilder<'_, Arc<dyn MonotonicCounter>> {
78        InstrumentBuilder::new(self, name.into())
79    }
80
81    /// Create a new AsyncMonotonicCounter.
82    #[allow(clippy::type_complexity)]
83    pub fn create_async_monotonic_counter<F>(
84        &self,
85        name: impl Into<Cow<'static, str>>,
86        callback: F,
87    ) -> AsyncInstrumentBuilder<'_, Arc<dyn AsyncMeasure<Value = u64>>, u64>
88    where
89        F: Fn(&dyn AsyncMeasure<Value = u64>) + Send + Sync + 'static,
90    {
91        AsyncInstrumentBuilder::new(self, name.into(), Arc::new(callback))
92    }
93
94    /// Create a new [Histogram].
95    pub fn create_histogram(
96        &self,
97        name: impl Into<Cow<'static, str>>,
98    ) -> InstrumentBuilder<'_, Arc<dyn Histogram>> {
99        InstrumentBuilder::new(self, name.into())
100    }
101}