polkadot_dispute_distribution/
metrics.rs1use polkadot_node_subsystem_util::{
18 metrics,
19 metrics::{
20 prometheus,
21 prometheus::{Counter, CounterVec, Opts, PrometheusError, Registry, U64},
22 },
23};
24
25pub const SUCCEEDED: &'static str = "succeeded";
27
28pub const FAILED: &'static str = "failed";
30
31#[derive(Clone, Default)]
33pub struct Metrics(Option<MetricsInner>);
34
35#[derive(Clone)]
36struct MetricsInner {
37 sent_requests: CounterVec<U64>,
39
40 received_requests: Counter<U64>,
44
45 imported_requests: CounterVec<U64>,
49
50 time_dispute_request: prometheus::Histogram,
52}
53
54impl Metrics {
55 pub fn new_dummy() -> Self {
57 Metrics(None)
58 }
59
60 pub fn on_sent_request(&self, label: &'static str) {
62 if let Some(metrics) = &self.0 {
63 metrics.sent_requests.with_label_values(&[label]).inc()
64 }
65 }
66
67 pub fn on_received_request(&self) {
69 if let Some(metrics) = &self.0 {
70 metrics.received_requests.inc()
71 }
72 }
73
74 pub fn on_imported(&self, label: &'static str, num_requests: usize) {
76 if let Some(metrics) = &self.0 {
77 metrics
78 .imported_requests
79 .with_label_values(&[label])
80 .inc_by(num_requests as u64)
81 }
82 }
83
84 pub fn time_dispute_request(&self) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
86 self.0.as_ref().map(|metrics| metrics.time_dispute_request.start_timer())
87 }
88}
89
90impl metrics::Metrics for Metrics {
91 fn try_register(registry: &Registry) -> Result<Self, PrometheusError> {
92 let metrics = MetricsInner {
93 sent_requests: prometheus::register(
94 CounterVec::new(
95 Opts::new(
96 "polkadot_parachain_dispute_distribution_sent_requests",
97 "Total number of sent requests.",
98 ),
99 &["success"],
100 )?,
101 registry,
102 )?,
103 received_requests: prometheus::register(
104 Counter::new(
105 "polkadot_parachain_dispute_distribution_received_requests",
106 "Total number of received dispute requests.",
107 )?,
108 registry,
109 )?,
110 imported_requests: prometheus::register(
111 CounterVec::new(
112 Opts::new(
113 "polkadot_parachain_dispute_distribution_imported_requests",
114 "Total number of imported requests.",
115 ),
116 &["success"],
117 )?,
118 registry,
119 )?,
120 time_dispute_request: prometheus::register(
121 prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
122 "polkadot_parachain_dispute_distribution_time_dispute_request",
123 "Time needed for dispute votes to get confirmed/fail getting transmitted.",
124 ))?,
125 registry,
126 )?,
127 };
128 Ok(Metrics(Some(metrics)))
129 }
130}