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