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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
macro_rules! impl_max_gauge {
($name:ident, $ty:ty, $min_val:expr) => {
/// Max gauge — tracks the maximum since last take.
///
/// `take()` returns the maximum and resets the gauge. Useful for
/// periodic reporting ("what was the peak since last report?").
///
/// # Use Cases
/// - Peak latency per reporting interval
/// - High-water mark gauges (Prometheus-style)
/// - Periodic max collection
#[derive(Debug, Clone)]
pub struct $name {
max: $ty,
has_value: bool,
}
impl $name {
/// Creates a new empty gauge.
#[inline]
#[must_use]
pub const fn new() -> Self {
Self {
max: $min_val,
has_value: false,
}
}
/// Records a sample.
#[inline]
pub fn update(&mut self, sample: $ty) {
if !self.has_value || sample > self.max {
self.max = sample;
}
self.has_value = true;
}
/// Returns the max since last take/reset, and resets the gauge.
#[inline]
pub fn take(&mut self) -> Option<$ty> {
if self.has_value {
let val = self.max;
self.max = $min_val;
self.has_value = false;
Option::Some(val)
} else {
Option::None
}
}
/// Peeks at the current max without resetting.
#[inline]
#[must_use]
pub fn peek(&self) -> Option<$ty> {
if self.has_value {
Option::Some(self.max)
} else {
Option::None
}
}
/// Resets the gauge.
#[inline]
pub fn reset(&mut self) {
self.max = $min_val;
self.has_value = false;
}
}
impl Default for $name {
#[inline]
fn default() -> Self {
Self::new()
}
}
};
}
impl_max_gauge!(MaxGaugeF64, f64, f64::MIN);
impl_max_gauge!(MaxGaugeF32, f32, f32::MIN);
impl_max_gauge!(MaxGaugeI64, i64, i64::MIN);
impl_max_gauge!(MaxGaugeI32, i32, i32::MIN);
impl_max_gauge!(MaxGaugeI128, i128, i128::MIN);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty() {
let mut g = MaxGaugeF64::new();
assert!(g.peek().is_none());
assert!(g.take().is_none());
}
#[test]
#[allow(clippy::float_cmp)]
fn tracks_max() {
let mut g = MaxGaugeF64::new();
g.update(10.0);
g.update(50.0);
g.update(30.0);
assert_eq!(g.peek(), Some(50.0));
}
#[test]
#[allow(clippy::float_cmp)]
fn take_returns_and_resets() {
let mut g = MaxGaugeF64::new();
g.update(50.0);
assert_eq!(g.take(), Some(50.0));
assert!(g.take().is_none()); // already taken
g.update(20.0);
assert_eq!(g.take(), Some(20.0));
}
#[test]
fn i64_basic() {
let mut g = MaxGaugeI64::new();
g.update(100);
g.update(200);
assert_eq!(g.take(), Some(200));
}
#[test]
fn reset() {
let mut g = MaxGaugeF64::new();
g.update(100.0);
g.reset();
assert!(g.peek().is_none());
}
#[test]
fn default_is_empty() {
let g = MaxGaugeI32::default();
assert!(g.peek().is_none());
}
#[test]
fn i128_basic() {
let mut g = MaxGaugeI128::new();
g.update(100);
g.update(200);
assert_eq!(g.take(), Some(200));
}
}