referrerpolicy=no-referrer-when-downgrade

polkadot_dispute_distribution/
metrics.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Polkadot is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.
16
17use polkadot_node_subsystem_util::{
18	metrics,
19	metrics::{
20		prometheus,
21		prometheus::{Counter, CounterVec, Opts, PrometheusError, Registry, U64},
22	},
23};
24
25/// Label for success counters.
26pub const SUCCEEDED: &'static str = "succeeded";
27
28/// Label for fail counters.
29pub const FAILED: &'static str = "failed";
30
31/// Dispute Distribution metrics.
32#[derive(Clone, Default)]
33pub struct Metrics(Option<MetricsInner>);
34
35#[derive(Clone)]
36struct MetricsInner {
37	/// Number of sent dispute requests (succeeded and failed).
38	sent_requests: CounterVec<U64>,
39
40	/// Number of requests received.
41	///
42	/// This is all requests coming in, regardless of whether they are processed or dropped.
43	received_requests: Counter<U64>,
44
45	/// Number of requests for which `ImportStatements` returned.
46	///
47	/// We both have successful imports and failed imports here.
48	imported_requests: CounterVec<U64>,
49
50	/// The duration of issued dispute request to response.
51	time_dispute_request: prometheus::Histogram,
52}
53
54impl Metrics {
55	/// Create new dummy metrics, not reporting anything.
56	pub fn new_dummy() -> Self {
57		Metrics(None)
58	}
59
60	/// Increment counter on finished request sending.
61	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	/// Increment counter on served disputes.
68	pub fn on_received_request(&self) {
69		if let Some(metrics) = &self.0 {
70			metrics.received_requests.inc()
71		}
72	}
73
74	/// Statements have been imported.
75	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	/// Get a timer to time request/response duration.
85	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}