Skip to main content

zebra_consensus/
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. They also
5//! call [`register_failure_counters`], so that exporters publish the failure counters before the
6//! first failure.
7
8use metrics::{describe_counter, describe_gauge, describe_histogram, Unit};
9
10/// The label keys of each labelled metric this crate records.
11///
12/// - `zebra.consensus.batch.duration_seconds`:
13///   - `verifier`: `ed25519`, `redjubjub`, `redpallas`, `halo2`, or `groth16_sapling`.
14///   - `result`: `success` or `failure`.
15/// - `zebra.consensus.cache.*`:
16///   - `verifier`: `halo2_pre_nu6_2`, `halo2_nu6_2`, `halo2_nu6_3_onward`, or `groth16_sapling`.
17/// - `zebra.consensus.block.phase.duration_seconds`: `phase`, `block_checks`,
18///   `transaction_verification`, or `state_commit`.
19pub const LABELS: &[(&str, &[&str])] = &[
20    (
21        "zebra.consensus.batch.duration_seconds",
22        &["verifier", "result"],
23    ),
24    ("zebra.consensus.cache.hit", &["verifier"]),
25    ("zebra.consensus.cache.miss", &["verifier"]),
26    ("zebra.consensus.cache.insert", &["verifier"]),
27    ("zebra.consensus.cache.evict", &["verifier"]),
28    ("zebra.consensus.cache.size", &["verifier"]),
29    ("zebra.consensus.block.phase.duration_seconds", &["phase"]),
30];
31
32/// The counters of signatures and shielded bundles in batches that failed verification.
33///
34/// A failed batch counts every item in it, before each item is verified on its own, so a counted
35/// item can still be valid.
36///
37/// They are only recorded when a verification fails, and `rate()` can't see the first increase of
38/// a series that didn't exist before, so [`register_failure_counters`] registers them at zero.
39pub const FAILURE_COUNTERS: &[&str] = &[
40    "signatures.ed25519.invalid",
41    "signatures.redjubjub.invalid",
42    "signatures.redpallas.invalid",
43    "proofs.halo2.invalid",
44    "proofs.sapling.invalid",
45];
46
47/// Registers every counter in [`FAILURE_COUNTERS`] with the installed [`metrics`] recorder,
48/// without changing its value, so that exporters publish it at zero before the first failure.
49pub fn register_failure_counters() {
50    for name in FAILURE_COUNTERS {
51        metrics::counter!(*name).increment(0);
52    }
53}
54
55/// Describes every metric this crate records to the installed [`metrics`] recorder.
56pub fn describe() {
57    // Checkpoint verifier
58    describe_gauge!(
59        "checkpoint.verified.height",
60        Unit::Count,
61        "Height of the latest block verified by the checkpoint verifier."
62    );
63    describe_gauge!(
64        "checkpoint.verified.count",
65        Unit::Count,
66        "Number of checkpoints verified so far."
67    );
68    describe_counter!(
69        "checkpoint.verified.block.count",
70        Unit::Count,
71        "Blocks verified against checkpoints."
72    );
73    describe_gauge!(
74        "checkpoint.processing.next.height",
75        Unit::Count,
76        "Height of the next checkpoint the checkpoint verifier is waiting to reach."
77    );
78    describe_gauge!(
79        "checkpoint.queued.max.height",
80        Unit::Count,
81        "Highest block height queued in the checkpoint verifier, or NaN when none are queued."
82    );
83    describe_gauge!(
84        "checkpoint.queued.continuous.height",
85        Unit::Count,
86        "Height of the end of the continuous run of queued blocks after the verified tip."
87    );
88    describe_gauge!(
89        "checkpoint.queued_slots",
90        Unit::Count,
91        "Number of heights with blocks queued in the checkpoint verifier."
92    );
93    describe_counter!(
94        "checkpoint.waiting.count",
95        Unit::Count,
96        "Times the checkpoint verifier waited for more blocks before it could verify a range."
97    );
98
99    // Block verifier
100    describe_histogram!(
101        "zebra.consensus.block.phase.duration_seconds",
102        Unit::Seconds,
103        "Time each phase of full block verification took, for blocks that aren't block \
104         proposals: the header and structure checks, transaction verification, and the state \
105         commit. The first two phases are recorded when they pass, even if a later phase rejects \
106         the block. The state commit is recorded only for committed blocks, and includes any \
107         wait for the parent block."
108    );
109
110    // Batch verifiers
111    describe_histogram!(
112        "zebra.consensus.batch.duration_seconds",
113        Unit::Seconds,
114        "Time taken to verify one batch of signatures or proofs."
115    );
116    describe_counter!(
117        "signatures.ed25519.validated",
118        Unit::Count,
119        "Ed25519 signatures that passed batch verification."
120    );
121    describe_counter!(
122        "signatures.ed25519.invalid",
123        Unit::Count,
124        "Ed25519 signatures in batches that failed verification, including valid ones that pass when verified on their own."
125    );
126    describe_counter!(
127        "signatures.redjubjub.validated",
128        Unit::Count,
129        "RedJubjub signatures that passed batch verification. zebrad verifies these signatures with their Sapling bundles, so it leaves this at zero."
130    );
131    describe_counter!(
132        "signatures.redjubjub.invalid",
133        Unit::Count,
134        "RedJubjub signatures in batches that failed verification, including valid ones that pass when verified on their own. zebrad verifies these signatures with their Sapling bundles, so it leaves this at zero."
135    );
136    describe_counter!(
137        "signatures.redpallas.validated",
138        Unit::Count,
139        "RedPallas signatures that passed batch verification. zebrad verifies these signatures with their Orchard bundles, so it leaves this at zero."
140    );
141    describe_counter!(
142        "signatures.redpallas.invalid",
143        Unit::Count,
144        "RedPallas signatures in batches that failed verification, including valid ones that pass when verified on their own. zebrad verifies these signatures with their Orchard bundles, so it leaves this at zero."
145    );
146    describe_counter!(
147        "proofs.halo2.verified",
148        Unit::Count,
149        "Orchard bundles whose Halo2 proofs and signatures passed batch verification."
150    );
151    describe_counter!(
152        "proofs.halo2.invalid",
153        Unit::Count,
154        "Orchard bundles in batches that failed verification, including valid ones that pass when verified on their own."
155    );
156    describe_counter!(
157        "proofs.sapling.verified",
158        Unit::Count,
159        "Sapling bundles whose Groth16 proofs and signatures passed batch verification."
160    );
161    describe_counter!(
162        "proofs.sapling.invalid",
163        Unit::Count,
164        "Sapling bundles in batches that failed verification, including valid ones that pass when verified on their own."
165    );
166
167    // Verification caches
168    describe_counter!(
169        "zebra.consensus.cache.hit",
170        Unit::Count,
171        "Verifications answered from a verification cache."
172    );
173    describe_counter!(
174        "zebra.consensus.cache.miss",
175        Unit::Count,
176        "Verifications that a verification cache passed to its verifier."
177    );
178    describe_counter!(
179        "zebra.consensus.cache.insert",
180        Unit::Count,
181        "Keys recorded as verified in a verification cache."
182    );
183    describe_counter!(
184        "zebra.consensus.cache.evict",
185        Unit::Count,
186        "Keys dropped from a verification cache to stay within its capacity."
187    );
188    describe_gauge!(
189        "zebra.consensus.cache.size",
190        Unit::Count,
191        "Number of keys a verification cache currently remembers."
192    );
193}