Skip to main content

zebra_network/
metrics.rs

1//! Descriptions of the metrics this crate records.
2//!
3//! Applications that install a [`metrics`] recorder call [`describe`] right after installing it, so
4//! that exporters can document each metric, for example in Prometheus `# HELP` lines.
5
6use metrics::{describe_counter, describe_gauge, describe_histogram, Unit};
7
8/// The label keys of each labelled metric this crate records.
9///
10/// - `network`: `mainnet`, `testnet`, or `regtest`.
11/// - `direction`: `inbound` or `outbound`.
12/// - `address_family`: `ipv4` or `ipv6`.
13/// - `implementation`: `zebra`, `zakura`, `legacy_zcashd`, or `other`, from the peer's user agent.
14/// - `stage`: `admission`, `tcp_connect`, `tcp_or_handshake`, `version`, `handshake`, or
15///   `unknown`.
16/// - `outcome`: `success`, `canceled`, `banned`, `capacity_or_rate_limited`, or a handshake or
17///   I/O error kind such as `timeout`, `connection_refused`, or `obsolete_version`.
18/// - `command`: a Zcash protocol command, a request or response name, or, for
19///   `zebra.net.connection.state`, a connection state such as `AwaitingRequest`.
20/// - `error`: a serialization error kind: `io`, `parse`, `utf8_error`, `try_from_slice_error`,
21///   `try_from_int_error`, `from_hex_error`, `amount`, `bad_transaction_balance`, `script`,
22///   `opcode`, or `num`.
23/// - `result`: `success` or `failure`.
24/// - `reason`: for handshake failures, `unexpected_message`, `nonce_reuse`, `duplicate_nonce`,
25///   `connection_closed`, `io_error`, `serialization`, `obsolete_version`,
26///   `missing_required_services`, or `timeout`; for peer set fallbacks, `no_serving_peer`,
27///   `queue_full`, or `expired`.
28/// - `kind`: `instant` or `waited`.
29/// - `remote_version` and `negotiated_version`: a protocol version Zebra knows, or `other`.
30/// - `min_version`: the minimum protocol version Zebra accepts for the current network upgrade.
31/// - `missing_service`: the required service a rejected peer doesn't provide: `NODE_NETWORK`,
32///   meaning the peer doesn't serve full history because it doesn't advertise `NODE_NETWORK`, or
33///   advertises it with `NODE_PRUNED`.
34/// - `seed`: on the `zcash.net.peers.initial` series that count the peers from configured initial
35///   peers, the configured entry when it is one of Zebra's default DNS seeders, or `custom` for any
36///   other entry. The series without it counts the peers from the peer cache file.
37pub const LABELS: &[(&str, &[&str])] = &[
38    (
39        "zcash.net.connection.attempts.total",
40        &["network", "direction", "address_family"],
41    ),
42    (
43        "zcash.net.connection.outcomes.total",
44        &["network", "direction", "address_family", "stage", "outcome"],
45    ),
46    (
47        "zcash.net.peer.version.messages.total",
48        &["network", "direction", "address_family", "implementation"],
49    ),
50    (
51        "zcash.net.peer.version.outcomes.total",
52        &[
53            "network",
54            "direction",
55            "address_family",
56            "implementation",
57            "outcome",
58        ],
59    ),
60    ("zebra.net.connection.state", &["command"]),
61    ("zebra.net.in.requests", &["command"]),
62    ("zebra.net.in.responses", &["command"]),
63    ("zebra.net.out.requests", &["command"]),
64    ("zebra.net.out.requests.canceled", &["command"]),
65    ("zebra.net.out.responses", &["command"]),
66    ("zebra.net.in.errors", &["error"]),
67    ("zcash.net.in.messages", &["command"]),
68    ("zcash.net.out.messages", &["command"]),
69    ("zcash.net.peer.handshake.duration_seconds", &["result"]),
70    ("zcash.net.peer.handshake.failures.total", &["reason"]),
71    (
72        "zcash.net.peers.obsolete",
73        &["remote_version", "min_version"],
74    ),
75    ("zcash.net.peers.missing_services", &["missing_service"]),
76    (
77        "zcash.net.peers.connected",
78        &["remote_version", "negotiated_version", "min_version"],
79    ),
80    ("zcash.net.peers.initial", &["seed"]),
81    ("zcash.net.peers", &["network"]),
82    ("zcash.net.peer_set.find_fallback", &["reason", "network"]),
83    ("zcash.net.peer_set.find_queued", &["network"]),
84    ("zcash.net.peer_set.inventory_queued", &["network"]),
85    ("zcash.net.peer_set.inventory_refusal", &["kind", "network"]),
86    ("pool.num_ready", &["network"]),
87    ("pool.num_unready", &["network"]),
88    ("crawler.in_flight_handshakes", &["network"]),
89    ("candidate_set.responded", &["network"]),
90    ("candidate_set.gossiped", &["network"]),
91    ("candidate_set.failed", &["network"]),
92    ("candidate_set.pending", &["network"]),
93    ("candidate_set.recently_live", &["network"]),
94    ("candidate_set.disconnected", &["network"]),
95];
96
97/// Describes every metric this crate records to the installed [`metrics`] recorder.
98pub fn describe() {
99    // Connection lifecycle
100    describe_counter!(
101        "zcash.net.connection.attempts.total",
102        Unit::Count,
103        "TCP connection attempts started."
104    );
105    describe_counter!(
106        "zcash.net.connection.outcomes.total",
107        Unit::Count,
108        "Finished connection attempts, by the stage they reached and how they ended."
109    );
110    describe_counter!(
111        "zcash.net.peer.version.messages.total",
112        Unit::Count,
113        "Version messages decoded from peers during handshakes."
114    );
115    describe_counter!(
116        "zcash.net.peer.version.outcomes.total",
117        Unit::Count,
118        "Handshakes that decoded a peer's version message, by how they ended."
119    );
120    describe_gauge!(
121        "zebra.net.connection.state",
122        Unit::Count,
123        "Live peer connections in each connection state."
124    );
125
126    // Handshakes
127    describe_histogram!(
128        "zcash.net.peer.handshake.duration_seconds",
129        Unit::Seconds,
130        "Time taken to complete or fail a peer handshake."
131    );
132    describe_counter!(
133        "zcash.net.peer.handshake.failures.total",
134        Unit::Count,
135        "Failed peer handshakes, by reason."
136    );
137    describe_counter!(
138        "zcash.net.peers.connected",
139        Unit::Count,
140        "Successful peer handshakes, by protocol version."
141    );
142    describe_gauge!(
143        "zcash.net.peers.version.connected",
144        Unit::Count,
145        "Protocol version of the peer in the most recent successful handshake."
146    );
147    describe_counter!(
148        "zcash.net.peers.obsolete",
149        Unit::Count,
150        "Handshakes rejected because the peer's protocol version is obsolete."
151    );
152    describe_gauge!(
153        "zcash.net.peers.version.obsolete",
154        Unit::Count,
155        "Protocol version of the peer in the most recent handshake rejected as obsolete."
156    );
157    describe_counter!(
158        "zcash.net.peers.missing_services",
159        Unit::Count,
160        "Handshakes rejected because the peer doesn't advertise the required services."
161    );
162
163    // Messages
164    describe_counter!(
165        "zcash.net.in.messages",
166        Unit::Count,
167        "Protocol messages received from peers, by command."
168    );
169    describe_counter!(
170        "zcash.net.out.messages",
171        Unit::Count,
172        "Protocol messages sent to peers, by command."
173    );
174    describe_counter!(
175        "zcash.net.in.bytes.total",
176        Unit::Bytes,
177        "Bytes of protocol messages received from peers."
178    );
179    describe_counter!(
180        "zcash.net.out.bytes.total",
181        Unit::Bytes,
182        "Bytes of protocol messages sent to peers."
183    );
184    describe_counter!(
185        "zebra.net.in.errors",
186        Unit::Count,
187        "Messages from peers that failed to deserialize, by error kind."
188    );
189    describe_counter!(
190        "zebra.net.in.requests",
191        Unit::Count,
192        "Requests received from peers, by request."
193    );
194    describe_counter!(
195        "zebra.net.out.responses",
196        Unit::Count,
197        "Responses sent to peers, by response."
198    );
199    describe_counter!(
200        "zebra.net.out.requests",
201        Unit::Count,
202        "Requests sent to peers, by request."
203    );
204    describe_counter!(
205        "zebra.net.in.responses",
206        Unit::Count,
207        "Responses received from peers, by response."
208    );
209    describe_counter!(
210        "zebra.net.out.requests.canceled",
211        Unit::Count,
212        "Requests to peers canceled before they were sent, by request."
213    );
214    describe_counter!(
215        "peer.canceled",
216        Unit::Count,
217        "Requests to peers canceled before they were sent."
218    );
219
220    describe_histogram!(
221        "zebra.net.block.decode.wait_seconds",
222        Unit::Seconds,
223        "Time a received block message waited for a rayon thread to start decoding it."
224    );
225    describe_histogram!(
226        "zebra.net.block.decode.duration_seconds",
227        Unit::Seconds,
228        "Time taken to decode a received block message, including the wait for a rayon thread."
229    );
230    describe_counter!(
231        "zebra.net.compact_block.admitted",
232        Unit::Count,
233        "Compact block announcements that this node kept, to rebuild the block when it is downloaded."
234    );
235    describe_counter!(
236        "zebra.net.compact_block.rebuilt",
237        Unit::Count,
238        "Blocks that this node rebuilt from a compact announcement and its mempool."
239    );
240
241    // Inbound overload
242    describe_counter!(
243        "pool.closed.loadshed",
244        Unit::Count,
245        "Peer connections closed because Zebra was overloaded with inbound requests."
246    );
247    describe_counter!(
248        "pool.closed.inbound.timeout",
249        Unit::Count,
250        "Peer connections closed because Zebra timed out handling an inbound request."
251    );
252    describe_counter!(
253        "pool.ignored.loadshed",
254        Unit::Count,
255        "Inbound requests ignored because Zebra was overloaded."
256    );
257    describe_counter!(
258        "pool.ignored.inbound.timeout",
259        Unit::Count,
260        "Inbound requests ignored because Zebra timed out handling them."
261    );
262
263    // Peer set
264    describe_gauge!(
265        "zcash.net.peers",
266        Unit::Count,
267        "Peers connected to the peer set, ready or busy."
268    );
269    describe_gauge!(
270        "pool.num_ready",
271        Unit::Count,
272        "Peers in the peer set that are ready for a request."
273    );
274    describe_gauge!(
275        "pool.num_unready",
276        Unit::Count,
277        "Peers in the peer set that are busy or not yet ready."
278    );
279    describe_counter!(
280        "zcash.net.peer_set.find_queued",
281        Unit::Count,
282        "FindBlocks and FindHeaders requests queued until a peer that serves them is ready."
283    );
284    describe_counter!(
285        "zcash.net.peer_set.find_fallback",
286        Unit::Count,
287        "FindBlocks and FindHeaders requests sent to a peer that might not serve them, by reason."
288    );
289    describe_counter!(
290        "zcash.net.peer_set.inventory_queued",
291        Unit::Count,
292        "Inventory requests waiting for a busy peer that might have the inventory."
293    );
294    describe_counter!(
295        "zcash.net.peer_set.inventory_refusal",
296        Unit::Count,
297        "Inventory requests failed because no peer that might have the inventory could take them."
298    );
299    describe_counter!(
300        "pool.inventory.dropped",
301        Unit::Count,
302        "Times the inventory registry fell behind and dropped inventory changes."
303    );
304    describe_counter!(
305        "pool.inventory.dropped.messages",
306        Unit::Count,
307        "Inventory changes dropped because the inventory registry fell behind."
308    );
309
310    // Peer discovery
311    describe_gauge!(
312        "crawler.in_flight_handshakes",
313        Unit::Count,
314        "Outbound handshakes the peer crawler has in progress."
315    );
316    describe_counter!(
317        "zcash.net.peers.initial",
318        Unit::Count,
319        "Initial peer addresses from each DNS seeder and the peer cache."
320    );
321    describe_counter!(
322        "zcash.net.peers.cache",
323        Unit::Count,
324        "Peer addresses written to the peer cache file."
325    );
326    describe_gauge!(
327        "candidate_set.responded",
328        Unit::Count,
329        "Address book peers that responded to Zebra recently."
330    );
331    describe_gauge!(
332        "candidate_set.gossiped",
333        Unit::Count,
334        "Address book peers gossiped by other peers that Zebra hasn't tried yet."
335    );
336    describe_gauge!(
337        "candidate_set.failed",
338        Unit::Count,
339        "Address book peers whose latest connection attempt failed."
340    );
341    describe_gauge!(
342        "candidate_set.pending",
343        Unit::Count,
344        "Address book peers with a connection attempt in progress."
345    );
346    describe_gauge!(
347        "candidate_set.recently_live",
348        Unit::Count,
349        "Address book peers that were live recently."
350    );
351    describe_gauge!(
352        "candidate_set.disconnected",
353        Unit::Count,
354        "Address book peers that stopped responding recently."
355    );
356}