iota_network/randomness/
metrics.rs1use std::sync::Arc;
6
7use iota_sdk_types::RandomnessRound;
8use iota_types::committee::EpochId;
9use prometheus_filtered::{
10 Histogram, IntGauge, Registry, register_histogram_with_registry,
11 register_int_gauge_with_registry,
12};
13use tap::Pipe;
14
15#[derive(Clone)]
16pub(super) struct Metrics(Option<Arc<Inner>>);
17
18impl std::fmt::Debug for Metrics {
19 fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
20 fmt.debug_struct("Metrics").finish()
21 }
22}
23
24impl Metrics {
25 pub fn enabled(registry: &Registry) -> Self {
26 Metrics(Some(Inner::new(registry)))
27 }
28
29 pub fn disabled() -> Self {
30 Metrics(None)
31 }
32
33 pub fn set_epoch(&self, epoch: EpochId) {
34 if let Some(inner) = &self.0 {
35 inner.current_epoch.set(epoch as i64);
36 inner.highest_round_generated.set(-1);
37 inner.num_ignored_byzantine_peers.set(0);
38 }
39 }
40
41 pub fn record_completed_round(&self, round: RandomnessRound) {
42 if let Some(inner) = &self.0 {
43 inner.highest_round_generated.set(
44 inner
45 .highest_round_generated
46 .get()
47 .max(round.value() as i64),
48 );
49 }
50 }
51
52 pub fn set_num_rounds_pending(&self, num_rounds_pending: i64) {
53 if let Some(inner) = &self.0 {
54 inner.num_rounds_pending.set(num_rounds_pending);
55 }
56 }
57
58 pub fn num_rounds_pending(&self) -> Option<i64> {
59 self.0.as_ref().map(|inner| inner.num_rounds_pending.get())
60 }
61
62 pub fn round_generation_latency_metric(&self) -> Option<&Histogram> {
63 self.0.as_ref().map(|inner| &inner.round_generation_latency)
64 }
65
66 pub fn round_observation_latency_metric(&self) -> Option<&Histogram> {
67 self.0
68 .as_ref()
69 .map(|inner| &inner.round_observation_latency)
70 }
71
72 pub fn inc_num_ignored_byzantine_peers(&self) {
73 if let Some(inner) = &self.0 {
74 inner.num_ignored_byzantine_peers.inc();
75 }
76 }
77}
78
79struct Inner {
80 current_epoch: IntGauge,
81 highest_round_generated: IntGauge,
82 num_rounds_pending: IntGauge,
83 round_generation_latency: Histogram,
84 round_observation_latency: Histogram,
85 num_ignored_byzantine_peers: IntGauge,
86}
87
88const LATENCY_SEC_BUCKETS: &[f64] = &[
89 0.001, 0.005, 0.01, 0.05, 0.1, 0.15, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.2, 1.4,
90 1.6, 1.8, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5, 10.,
91 12.5, 15., 17.5, 20., 25., 30., 60., 90., 120., 180., 300.,
92];
93
94impl Inner {
95 pub fn new(registry: &Registry) -> Arc<Self> {
96 Self {
97 current_epoch: register_int_gauge_with_registry!(
98 "randomness_current_epoch",
99 "The current epoch for which randomness is being generated (only updated after DKG completes)",
100 registry
101 ).unwrap(),
102 highest_round_generated: register_int_gauge_with_registry!(
103 "randomness_highest_round_generated",
104 "The highest round for which randomness has been generated for the current epoch",
105 registry
106 ).unwrap(),
107 num_rounds_pending: register_int_gauge_with_registry!(
108 "randomness_num_rounds_pending",
109 "The number of rounds of randomness that are pending generation/observation",
110 registry
111 ).unwrap(),
112 round_generation_latency: register_histogram_with_registry!(
113 "randomness_round_generation_latency",
114 "Time taken to generate a single round of randomness, from when the round is requested to when the full signature is aggregated",
115 LATENCY_SEC_BUCKETS.to_vec(),
116 registry
117 ).unwrap(),
118 round_observation_latency: register_histogram_with_registry!(
119 "randomness_round_observation_latency",
120 "Time taken from when partial signatures are sent for a round of randomness to when the value is observed in an executed checkpoint",
121 LATENCY_SEC_BUCKETS.to_vec(),
122 registry
123 ).unwrap(),
124 num_ignored_byzantine_peers: register_int_gauge_with_registry!(
125 "randomness_num_ignored_byzantine_peers",
126 "The number of byzantine peers that have been ignored by the randomness network loop in the current epoch",
127 registry
128 ).unwrap(),
129 }
130 .pipe(Arc::new)
131 }
132}