-
Notifications
You must be signed in to change notification settings - Fork 1
/
metrics.rs
159 lines (144 loc) · 4.38 KB
/
metrics.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
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
#[cfg(feature = "metrics")]
use lazy_static::lazy_static;
#[cfg(feature = "metrics")]
use prometheus::*;
#[cfg(feature = "metrics")]
lazy_static! {
static ref REQUEST_TIMES: Histogram = register_histogram!(HistogramOpts::new(
"request_execution_times",
"Request Execution Times"
)
.buckets(vec![
0.001, 0.0025, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0
]))
.unwrap();
static ref VIEW_CHANGE_TIMES: Histogram = register_histogram!(HistogramOpts::new(
"view_change_execution_times",
"View Change Execution Times"
)
.buckets(vec![
0.001, 0.0025, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0
]))
.unwrap();
static ref LOW_MARK: IntGauge =
register_int_gauge!(opts!("low_mark", "Request Id Low Mark")).unwrap();
static ref HIGH_MARK: IntGauge =
register_int_gauge!(opts!("high_mark", "Request Id High Mark")).unwrap();
static ref NEXT_SEQUENCE: IntGauge =
register_int_gauge!(opts!("next_sequence", "Next Sequence Number")).unwrap();
static ref LAST_COMMIT: IntGauge =
register_int_gauge!(opts!("last_commit", "Last committed sequence number")).unwrap();
static ref VIEW_NUMBER: IntGauge =
register_int_gauge!(opts!("view_number", "The number of the current view")).unwrap();
static ref IS_PRIMARY: IntGauge = register_int_gauge!(opts!(
"is_primary",
"Is this replica the primary? 1=true, 0=false"
))
.unwrap();
}
#[derive(Debug)]
pub struct Timer {
#[cfg(feature = "metrics")]
timer: Option<HistogramTimer>,
}
#[cfg(feature = "metrics")]
impl Timer {
pub fn new(histogram_timer: HistogramTimer) -> Self {
Self {
timer: Some(histogram_timer),
}
}
// timer will observer automatically when going out of scope
pub fn stop_and_record(&mut self) {
let option = self.timer.take();
if let Some(histogram_timer) = option {
histogram_timer.stop_and_record();
}
}
// discard to prevent observation when going out of scope
pub fn stop_and_discard(&mut self) {
let option = self.timer.take();
if let Some(histogram_timer) = option {
histogram_timer.stop_and_discard();
}
}
}
#[cfg(not(feature = "metrics"))]
impl Timer {
pub fn stop_and_record(&mut self) {
//do nothing
}
pub fn stop_and_discard(&mut self) {
//do nothing
}
}
#[cfg(feature = "metrics")]
pub fn request_execution_timer() -> Timer {
Timer::new(REQUEST_TIMES.start_timer())
}
#[cfg(not(feature = "metrics"))]
pub fn request_execution_timer() -> Timer {
Timer {}
}
#[cfg(feature = "metrics")]
pub fn view_change_timer() -> Timer {
Timer::new(VIEW_CHANGE_TIMES.start_timer())
}
#[cfg(not(feature = "metrics"))]
pub fn view_change_timer() -> Timer {
Timer {}
}
#[cfg(feature = "metrics")]
pub fn record_low_mark(mark: u64) {
LOW_MARK.set(mark as i64);
}
#[cfg(not(feature = "metrics"))]
pub fn record_low_mark(_sequence_number: u64) {
//do nothing
//compiler will nicely delete this function
}
#[cfg(feature = "metrics")]
pub fn record_high_mark(mark: u64) {
HIGH_MARK.set(mark as i64);
}
#[cfg(not(feature = "metrics"))]
pub fn record_high_mark(_sequence_number: u64) {
//do nothing
//compiler will nicely delete this function
}
#[cfg(feature = "full-metrics")]
pub fn record_next_sequence(sequence_number: u64) {
NEXT_SEQUENCE.set(sequence_number as i64);
}
#[cfg(not(feature = "full-metrics"))]
pub fn record_next_sequence(_sequence_number: u64) {
//do nothing
//compiler will nicely delete this function
}
#[cfg(feature = "full-metrics")]
pub fn record_last_commit(sequence_number: u64) {
LAST_COMMIT.set(sequence_number as i64);
}
#[cfg(not(feature = "full-metrics"))]
pub fn record_last_commit(_sequence_number: u64) {
//do nothing
//compiler will nicely delete this function
}
#[cfg(feature = "metrics")]
pub fn record_view_number(view: u64) {
VIEW_NUMBER.set(view as i64);
}
#[cfg(not(feature = "metrics"))]
pub fn record_view_number(_view: u64) {
//do nothing
//compiler will nicely delete this function
}
#[cfg(feature = "metrics")]
pub fn record_is_primary(state: bool) {
IS_PRIMARY.set(state as i64)
}
#[cfg(not(feature = "metrics"))]
pub fn record_is_primary(_state: bool) {
//do nothing
//compiler will nicely delete this function
}