s2n_quic_core/event/metrics/aggregate/probe/
dynamic.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::event::metrics::aggregate::{
5    self,
6    info::{self, Str},
7    Info,
8};
9
10#[derive(Clone, Debug, Default)]
11pub struct Registry(());
12
13impl aggregate::Registry for Registry {
14    type Counter = Counter;
15    type BoolCounter = BoolCounter;
16    type NominalCounter = NominalCounter;
17    type Measure = Measure;
18    type Gauge = Gauge;
19    type Timer = Timer;
20    type NominalTimer = NominalTimer;
21
22    #[inline]
23    fn register_counter(&self, info: &'static Info) -> Self::Counter {
24        Self::Counter::new(info)
25    }
26
27    #[inline]
28    fn register_bool_counter(&self, info: &'static Info) -> Self::BoolCounter {
29        Self::BoolCounter::new(info)
30    }
31
32    #[inline]
33    fn register_nominal_counter(
34        &self,
35        info: &'static Info,
36        variant: &'static info::Variant,
37    ) -> Self::NominalCounter {
38        Self::NominalCounter::new(info, variant)
39    }
40
41    #[inline]
42    fn register_measure(&self, info: &'static Info) -> Self::Measure {
43        Self::Measure::new(info)
44    }
45
46    #[inline]
47    fn register_gauge(&self, info: &'static Info) -> Self::Gauge {
48        Self::Gauge::new(info)
49    }
50
51    #[inline]
52    fn register_timer(&self, info: &'static Info) -> Self::Timer {
53        Self::Timer::new(info)
54    }
55
56    #[inline]
57    fn register_nominal_timer(
58        &self,
59        info: &'static Info,
60        variant: &'static info::Variant,
61    ) -> Self::NominalTimer {
62        Self::NominalTimer::new(info, variant)
63    }
64}
65
66macro_rules! recorder {
67    (
68        $recorder:ident,
69        $name:ident,
70        $module:ident,
71        $register:ident,
72        $record:ident,
73        $as_metric:ident : $metric_ty:ty
74        $(, $variant:ident : $variant_ty:ty)?
75    ) => {
76        mod $module {
77            use super::*;
78            use crate::probe::define;
79            use aggregate::Metric;
80
81            define!(
82                extern "probe" {
83                    #[link_name = $register]
84                    fn register(id: usize, name: &Str, units: &Str, $($variant: &Str)?);
85
86                    #[link_name = $record]
87                    fn record(id: usize, name: &Str, units: &Str, $($variant: &Str, )? value: $metric_ty);
88                }
89            );
90
91            #[derive(Copy, Clone, Debug, Default)]
92            pub struct $name(());
93
94            impl $name {
95                pub(super) fn new(info: &'static Info $(, $variant: $variant_ty)?) -> Self {
96                    register(info.id, info.name, info.units.as_str(), $($variant.name)?);
97                    Self(())
98                }
99            }
100
101            impl aggregate::$recorder for $name {
102                #[inline]
103                fn record<T: Metric>(&self, info: &'static Info, $($variant: $variant_ty, )? value: T) {
104                    record(info.id, info.name, info.units.as_str(), $($variant.name, )? value.$as_metric());
105                }
106            }
107        }
108
109        pub use $module::$name;
110    };
111}
112
113recorder!(
114    Recorder,
115    Counter,
116    counter,
117    s2n_quic__counter__register,
118    s2n_quic__counter__record,
119    as_u64: u64
120);
121recorder!(
122    NominalRecorder,
123    NominalCounter,
124    nominal_counter,
125    s2n_quic__counter__nominal__register,
126    s2n_quic__counter__nominal__record,
127    as_u64: u64,
128    variant: &'static info::Variant
129);
130recorder!(
131    Recorder,
132    Measure,
133    measure,
134    s2n_quic__measure__register,
135    s2n_quic__measure__record,
136    as_u64: u64
137);
138recorder!(
139    Recorder,
140    Gauge,
141    gauge,
142    s2n_quic__gauge__register,
143    s2n_quic__gauge__record,
144    as_u64: u64
145);
146recorder!(
147    Recorder,
148    Timer,
149    timer,
150    s2n_quic__timer__register,
151    s2n_quic__timer__record,
152    as_duration: core::time::Duration
153);
154recorder!(
155    NominalRecorder,
156    NominalTimer,
157    nominal_timer,
158    s2n_quic__timer__nominal__register,
159    s2n_quic__timer__nominal__record,
160    as_duration: core::time::Duration,
161    variant: &'static info::Variant
162);
163
164mod bool_counter {
165    use super::*;
166    use crate::probe::define;
167
168    define!(
169        extern "probe" {
170            #[link_name = s2n_quic__counter__bool__register]
171            fn register(id: usize, name: &Str);
172
173            #[link_name = s2n_quic__counter__bool__record]
174            fn record(id: usize, name: &Str, value: bool);
175        }
176    );
177
178    #[derive(Copy, Clone, Debug, Default)]
179    pub struct BoolCounter(());
180
181    impl BoolCounter {
182        pub(super) fn new(info: &'static Info) -> Self {
183            register(info.id, info.name);
184            Self(())
185        }
186    }
187
188    impl aggregate::BoolRecorder for BoolCounter {
189        #[inline]
190        fn record(&self, info: &'static Info, value: bool) {
191            record(info.id, info.name, value);
192        }
193    }
194}
195
196pub use bool_counter::BoolCounter;