1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2021 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.

use polkadot_node_subsystem_util::{
	metrics,
	metrics::{
		prometheus,
		prometheus::{Counter, CounterVec, Opts, PrometheusError, Registry, U64},
	},
};

/// Label for success counters.
pub const SUCCEEDED: &'static str = "succeeded";

/// Label for fail counters.
pub const FAILED: &'static str = "failed";

/// Dispute Distribution metrics.
#[derive(Clone, Default)]
pub struct Metrics(Option<MetricsInner>);

#[derive(Clone)]
struct MetricsInner {
	/// Number of sent dispute requests (succeeded and failed).
	sent_requests: CounterVec<U64>,

	/// Number of requests received.
	///
	/// This is all requests coming in, regardless of whether they are processed or dropped.
	received_requests: Counter<U64>,

	/// Number of requests for which `ImportStatements` returned.
	///
	/// We both have successful imports and failed imports here.
	imported_requests: CounterVec<U64>,

	/// The duration of issued dispute request to response.
	time_dispute_request: prometheus::Histogram,
}

impl Metrics {
	/// Create new dummy metrics, not reporting anything.
	pub fn new_dummy() -> Self {
		Metrics(None)
	}

	/// Increment counter on finished request sending.
	pub fn on_sent_request(&self, label: &'static str) {
		if let Some(metrics) = &self.0 {
			metrics.sent_requests.with_label_values(&[label]).inc()
		}
	}

	/// Increment counter on served disputes.
	pub fn on_received_request(&self) {
		if let Some(metrics) = &self.0 {
			metrics.received_requests.inc()
		}
	}

	/// Statements have been imported.
	pub fn on_imported(&self, label: &'static str) {
		if let Some(metrics) = &self.0 {
			metrics.imported_requests.with_label_values(&[label]).inc()
		}
	}

	/// Get a timer to time request/response duration.
	pub fn time_dispute_request(&self) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
		self.0.as_ref().map(|metrics| metrics.time_dispute_request.start_timer())
	}
}

impl metrics::Metrics for Metrics {
	fn try_register(registry: &Registry) -> Result<Self, PrometheusError> {
		let metrics = MetricsInner {
			sent_requests: prometheus::register(
				CounterVec::new(
					Opts::new(
						"parachain_dispute_distribution_sent_requests",
						"Total number of sent requests.",
					),
					&["success"],
				)?,
				registry,
			)?,
			received_requests: prometheus::register(
				Counter::new(
					"parachain_dispute_distribution_received_requests",
					"Total number of received dispute requests.",
				)?,
				registry,
			)?,
			imported_requests: prometheus::register(
				CounterVec::new(
					Opts::new(
						"parachain_dispute_distribution_imported_requests",
						"Total number of imported requests.",
					),
					&["success"],
				)?,
				registry,
			)?,
			time_dispute_request: prometheus::register(
				prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
					"parachain_dispute_distribution_time_dispute_request",
					"Time needed for dispute votes to get confirmed/fail getting transmitted.",
				))?,
				registry,
			)?,
		};
		Ok(Metrics(Some(metrics)))
	}
}