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
148
149
150
151
152
153
154
155
156
157
158
159
160
use std::time::{Duration, Instant};
use instruments::{Instrument, Update, Updates};
use snapshot::Snapshot;
use util;
use {Descriptive, PutsSnapshot, DECR, INCR};
#[deprecated(since = "0.9.17", note = "Use Gauge. It also supports INC/DEC")]
pub struct IncDecGauge {
name: String,
title: Option<String>,
description: Option<String>,
value: State,
memorize_extrema: Option<Duration>,
}
impl IncDecGauge {
pub fn new_with_defaults<T: Into<String>>(name: T) -> IncDecGauge {
Self {
name: name.into(),
title: None,
description: None,
value: State {
current: 0,
peak: 0,
bottom: 0,
last_peak_at: Instant::now(),
last_bottom_at: Instant::now(),
},
memorize_extrema: None,
}
}
fn modify(&mut self, incr: bool) {
let state = &mut self.value;
let v = if incr {
state.current + 1
} else {
state.current - 1
};
if let Some(ext_dur) = self.memorize_extrema {
let now = Instant::now();
if v >= state.peak {
state.last_peak_at = now;
state.peak = v;
} else if state.last_peak_at < now - ext_dur {
state.peak = v;
}
if v <= state.bottom {
state.last_bottom_at = now;
state.bottom = v;
} else if state.last_bottom_at < now - ext_dur {
state.bottom = v;
}
}
state.current = v;
}
pub fn get(&self) -> i64 {
self.value.current
}
pub fn set_memorize_extrema(&mut self, d: Duration) {
self.memorize_extrema = Some(d)
}
pub fn name(&self) -> &str {
&self.name
}
pub fn set_name<T: Into<String>>(&mut self, name: T) {
self.name = name.into();
}
pub fn set_title<T: Into<String>>(&mut self, title: T) {
self.title = Some(title.into())
}
pub fn set_description<T: Into<String>>(&mut self, description: T) {
self.description = Some(description.into())
}
}
impl Instrument for IncDecGauge {}
impl PutsSnapshot for IncDecGauge {
fn put_snapshot(&self, into: &mut Snapshot, descriptive: bool) {
util::put_postfixed_descriptives(self, &self.name, into, descriptive);
let state = &self.value;
into.items.push((self.name.clone(), state.current.into()));
if let Some(ext_dur) = self.memorize_extrema {
let peak_name = format!("{}_peak", self.name);
if state.last_peak_at > Instant::now() - ext_dur {
into.items.push((peak_name, state.peak.into()));
} else {
into.items.push((peak_name, state.current.into()));
}
let bottom_name = format!("{}_bottom", self.name);
if state.last_bottom_at > Instant::now() - ext_dur {
into.items.push((bottom_name, state.bottom.into()));
} else {
into.items.push((bottom_name, state.current.into()));
}
}
}
}
impl Updates for IncDecGauge {
fn update(&mut self, with: &Update) -> usize {
match *with {
Update::ObservationWithValue(v, _) => {
match v {
INCR => self.modify(true),
DECR => self.modify(false),
_ => {}
}
1
}
_ => 0,
}
}
}
impl Descriptive for IncDecGauge {
fn title(&self) -> Option<&str> {
self.title.as_ref().map(|n| &**n)
}
fn description(&self) -> Option<&str> {
self.description.as_ref().map(|n| &**n)
}
}
struct State {
current: i64,
peak: i64,
bottom: i64,
last_peak_at: Instant,
last_bottom_at: Instant,
}