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