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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//! Metrics recording trait for external metrics collection.
//!
//! This module defines the [`MetricsRecorder`] trait that allows applications
//! to plug in custom metrics collection backends. The trait provides a minimal
//! interface for recording Raft internal events.
//!
//! # Example
//!
//! Implement the trait, then install the recorder with [`Raft::set_metrics_recorder`]. The setter
//! is async: it sends the recorder to `RaftCore` only when its returned future is awaited.
//! `Ok(())` means that the command is queued for `RaftCore`. The recorder is installed, and
//! receives callbacks, only after `RaftCore` handles the command.
//!
//! ```no_run
//! # #![deny(unused_must_use)]
//! use std::sync::Arc;
//!
//! use openraft::metrics::MetricsRecorder;
//! # use openraft::Raft;
//! # use openraft::RaftTypeConfig;
//! # use openraft::errors::Fatal;
//! # use openraft::storage::RaftStateMachine;
//!
//! #[derive(Debug)]
//! struct MyRecorder;
//!
//! impl MetricsRecorder for MyRecorder {
//! fn record_apply_batch(&self, entry_count: u64) { /* ... */ }
//! fn record_append_batch(&self, entry_count: u64) { /* ... */ }
//! fn record_write_batch(&self, entry_count: u64) { /* ... */ }
//! fn set_current_term(&self, term: u64) { /* ... */ }
//! fn set_last_log_index(&self, index: u64) { /* ... */ }
//! fn set_committed_index(&self, index: u64) { /* ... */ }
//! fn set_applied_index(&self, index: u64) { /* ... */ }
//! fn set_snapshot_index(&self, index: u64) { /* ... */ }
//! fn set_purged_index(&self, index: u64) { /* ... */ }
//! fn set_server_state(&self, state: u8) { /* ... */ }
//! fn increment_vote(&self) { /* ... */ }
//! fn increment_heartbeat(&self) { /* ... */ }
//! fn increment_append(&self) { /* ... */ }
//! }
//!
//! # async fn install<C, SM>(raft: &Raft<C, SM>) -> Result<(), Fatal<C>>
//! # where
//! # C: RaftTypeConfig,
//! # SM: RaftStateMachine<C>,
//! # {
//! // Queue the recorder for RaftCore to install.
//! raft.set_metrics_recorder(Some(Arc::new(MyRecorder))).await?;
//! # Ok(())
//! # }
//! ```
//!
//! [`Raft::set_metrics_recorder`]: crate::Raft::set_metrics_recorder
use since;
use crateRaftTypeConfig;
use crateServerState;
use crateRaftMetrics;
use crateRaftTerm;
/// Trait for recording Raft metrics to external systems.
///
/// Implement this trait to collect metrics from Raft internals and export them
/// to your preferred metrics backend (e.g., Prometheus, OpenTelemetry, StatsD).
///
/// All methods are called from the RaftCore task and should return quickly to
/// avoid blocking Raft operations.
///
/// `RaftCore` sets the gauges through [`forward_metrics()`], which calls a gauge setter only when
/// it has a value to pass. A skipped setter leaves its gauge unchanged.
/// Forward gauge metrics from `RaftMetrics` to a `MetricsRecorder`.
///
/// This helper function extracts the relevant fields from `RaftMetrics` and
/// calls the corresponding setter methods on the recorder. It only forwards
/// gauge metrics (current state values), not counters or histograms.
///
/// It calls [`set_server_state()`] every time, and each other setter only when it has a value to
/// pass:
///
/// - [`set_current_term()`] receives [`current_term`] when [`RaftTerm::as_u64()`] converts it. The
/// `RaftTerm` implementations for integer types return `None` for a negative term or one above
/// `u64::MAX`.
/// - [`set_last_log_index()`] receives [`last_log_index`] when it is `Some`.
/// - [`set_committed_index()`] receives the index of [`local_committed`] when it is `Some`.
/// - [`set_applied_index()`] receives the index of [`last_applied`] when it is `Some`.
/// - [`set_snapshot_index()`] receives the index of [`snapshot`] when it is `Some`.
/// - [`set_purged_index()`] receives the index of [`purged`] when it is `Some`.
///
/// Forwarding never clears a gauge: a skipped setter leaves the recorder's value as it was. For
/// example, forwarding [`RaftMetrics::new_initial()`], whose log fields are all `None`, to a
/// recorder that already holds values leaves its index gauges as they were. A call with index 0
/// reports the first log entry, not an absent value. To tell an absent value from a retained one,
/// read [`RaftMetrics`] itself, such as from [`Raft::metrics()`].
///
/// # Example
///
/// ```
/// use openraft::RaftTypeConfig;
/// use openraft::metrics::MetricsRecorder;
/// use openraft::metrics::RaftMetrics;
/// use openraft::metrics::forward_metrics;
///
/// fn on_metrics_update<C>(metrics: &RaftMetrics<C>, recorder: &dyn MetricsRecorder)
/// where C: RaftTypeConfig {
/// forward_metrics(metrics, recorder);
/// }
/// ```
///
/// [`set_server_state()`]: MetricsRecorder::set_server_state
/// [`set_current_term()`]: MetricsRecorder::set_current_term
/// [`set_last_log_index()`]: MetricsRecorder::set_last_log_index
/// [`set_committed_index()`]: MetricsRecorder::set_committed_index
/// [`set_applied_index()`]: MetricsRecorder::set_applied_index
/// [`set_snapshot_index()`]: MetricsRecorder::set_snapshot_index
/// [`set_purged_index()`]: MetricsRecorder::set_purged_index
/// [`current_term`]: RaftMetrics::current_term
/// [`last_log_index`]: RaftMetrics::last_log_index
/// [`local_committed`]: RaftMetrics::local_committed
/// [`last_applied`]: RaftMetrics::last_applied
/// [`snapshot`]: RaftMetrics::snapshot
/// [`purged`]: RaftMetrics::purged
/// [`Raft::metrics()`]: crate::Raft::metrics