sui_network/randomness/
metrics.rs

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