aws_smithy_observability/
meter.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

//! Metrics are used to gain insight into the operational performance and health of a system in
//! real time.

use crate::instruments::{
    AsyncInstrumentBuilder, AsyncMeasure, Histogram, InstrumentBuilder, MonotonicCounter,
    UpDownCounter,
};
use crate::{attributes::Attributes, instruments::ProvideInstrument};
use std::{borrow::Cow, fmt::Debug, sync::Arc};

/// Provides named instances of [Meter].
pub trait ProvideMeter: Send + Sync + Debug {
    /// Get or create a named [Meter].
    fn get_meter(&self, scope: &'static str, attributes: Option<&Attributes>) -> Meter;
}

/// The entry point to creating instruments. A grouping of related metrics.
#[derive(Clone)]
pub struct Meter {
    pub(crate) instrument_provider: Arc<dyn ProvideInstrument + Send + Sync>,
}

impl Meter {
    /// Create a new [Meter] from an [ProvideInstrument]
    pub fn new(instrument_provider: Arc<dyn ProvideInstrument + Send + Sync>) -> Self {
        Meter {
            instrument_provider,
        }
    }

    /// Create a new Gauge.
    #[allow(clippy::type_complexity)]
    pub fn create_gauge<F>(
        &self,
        name: impl Into<Cow<'static, str>>,
        callback: F,
    ) -> AsyncInstrumentBuilder<'_, Arc<dyn AsyncMeasure<Value = f64>>, f64>
    where
        F: Fn(&dyn AsyncMeasure<Value = f64>) + Send + Sync + 'static,
    {
        AsyncInstrumentBuilder::new(self, name.into(), Arc::new(callback))
    }

    /// Create a new [UpDownCounter].
    pub fn create_up_down_counter(
        &self,
        name: impl Into<Cow<'static, str>>,
    ) -> InstrumentBuilder<'_, Arc<dyn UpDownCounter>> {
        InstrumentBuilder::new(self, name.into())
    }

    /// Create a new AsyncUpDownCounter.
    #[allow(clippy::type_complexity)]
    pub fn create_async_up_down_counter<F>(
        &self,
        name: impl Into<Cow<'static, str>>,
        callback: F,
    ) -> AsyncInstrumentBuilder<'_, Arc<dyn AsyncMeasure<Value = i64>>, i64>
    where
        F: Fn(&dyn AsyncMeasure<Value = i64>) + Send + Sync + 'static,
    {
        AsyncInstrumentBuilder::new(self, name.into(), Arc::new(callback))
    }

    /// Create a new [MonotonicCounter].
    pub fn create_monotonic_counter(
        &self,
        name: impl Into<Cow<'static, str>>,
    ) -> InstrumentBuilder<'_, Arc<dyn MonotonicCounter>> {
        InstrumentBuilder::new(self, name.into())
    }

    /// Create a new AsyncMonotonicCounter.
    #[allow(clippy::type_complexity)]
    pub fn create_async_monotonic_counter<F>(
        &self,
        name: impl Into<Cow<'static, str>>,
        callback: F,
    ) -> AsyncInstrumentBuilder<'_, Arc<dyn AsyncMeasure<Value = u64>>, u64>
    where
        F: Fn(&dyn AsyncMeasure<Value = u64>) + Send + Sync + 'static,
    {
        AsyncInstrumentBuilder::new(self, name.into(), Arc::new(callback))
    }

    /// Create a new [Histogram].
    pub fn create_histogram(
        &self,
        name: impl Into<Cow<'static, str>>,
    ) -> InstrumentBuilder<'_, Arc<dyn Histogram>> {
        InstrumentBuilder::new(self, name.into())
    }
}