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

            
17
//! Runtime declaration of the parachain metrics.
18

            
19
use polkadot_primitives::metric_definitions::{
20
	PARACHAIN_CREATE_INHERENT_BITFIELDS_SIGNATURE_CHECKS,
21
	PARACHAIN_INHERENT_DATA_BITFIELDS_PROCESSED, PARACHAIN_INHERENT_DATA_CANDIDATES_PROCESSED,
22
	PARACHAIN_INHERENT_DATA_DISPUTE_SETS_PROCESSED, PARACHAIN_INHERENT_DATA_WEIGHT,
23
	PARACHAIN_VERIFY_DISPUTE_SIGNATURE,
24
};
25
use polkadot_runtime_metrics::{Counter, CounterVec, Histogram};
26

            
27
pub struct Metrics {
28
	/// Samples inherent data weight.
29
	inherent_data_weight: CounterVec,
30
	/// Counts how many inherent bitfields processed in `process_inherent_data`.
31
	bitfields_processed: Counter,
32
	/// Counts how many parachain candidates processed in `process_inherent_data`.
33
	candidates_processed: CounterVec,
34
	/// Counts dispute statements sets processed in `process_inherent_data`.
35
	dispute_sets_processed: CounterVec,
36
	/// Counts bitfield signature checks in `process_inherent_data`.
37
	bitfields_signature_checks: CounterVec,
38

            
39
	/// Histogram with the time spent checking a validator signature of a dispute statement
40
	signature_timings: Histogram,
41
}
42

            
43
impl Metrics {
44
	/// Sample the inherent data weight metric before filtering.
45
259684
	pub fn on_before_filter(&self, value: u64) {
46
259684
		self.inherent_data_weight.with_label_values(&["before-filter"]).inc_by(value);
47
259684
	}
48

            
49
	/// Sample the inherent data weight metric after filtering.
50
	pub fn on_after_filter(&self, value: u64) {
51
		self.inherent_data_weight.with_label_values(&["after-filter"]).inc_by(value);
52
	}
53

            
54
	/// Increment the number of bitfields processed.
55
259684
	pub fn on_bitfields_processed(&self, value: u64) {
56
259684
		self.bitfields_processed.inc_by(value);
57
259684
	}
58

            
59
	/// Increment the number of parachain candidates included.
60
259684
	pub fn on_candidates_included(&self, value: u64) {
61
259684
		self.candidates_processed.with_label_values(&["included"]).inc_by(value);
62
259684
	}
63

            
64
	/// Increment the number of parachain candidates sanitized.
65
259684
	pub fn on_candidates_sanitized(&self, value: u64) {
66
259684
		self.candidates_processed.with_label_values(&["sanitized"]).inc_by(value);
67
259684
	}
68

            
69
	/// Increment the total number of parachain candidates received in `process_inherent_data`.
70
259684
	pub fn on_candidates_processed_total(&self, value: u64) {
71
259684
		self.candidates_processed.with_label_values(&["total"]).inc_by(value);
72
259684
	}
73

            
74
	/// Sample the relay chain freeze events causing runtime to not process candidates in
75
	/// `process_inherent_data`.
76
	pub fn on_relay_chain_freeze(&self) {
77
		self.dispute_sets_processed.with_label_values(&["frozen"]).inc();
78
	}
79

            
80
	/// Increment the number of disputes that have concluded as invalid.
81
	pub fn on_disputes_concluded_invalid(&self, value: u64) {
82
		self.dispute_sets_processed
83
			.with_label_values(&["concluded_invalid"])
84
			.inc_by(value);
85
	}
86

            
87
	/// Increment the number of disputes imported.
88
259684
	pub fn on_disputes_imported(&self, value: u64) {
89
259684
		self.dispute_sets_processed.with_label_values(&["imported"]).inc_by(value);
90
259684
	}
91

            
92
	pub fn on_valid_bitfield_signature(&self) {
93
		self.bitfields_signature_checks.with_label_values(&["valid"]).inc_by(1);
94
	}
95

            
96
	pub fn on_invalid_bitfield_signature(&self) {
97
		self.bitfields_signature_checks.with_label_values(&["invalid"]).inc_by(1);
98
	}
99

            
100
	pub fn on_signature_check_complete(&self, val: u128) {
101
		self.signature_timings.observe(val);
102
	}
103
}
104

            
105
pub const METRICS: Metrics = Metrics {
106
	inherent_data_weight: CounterVec::new(PARACHAIN_INHERENT_DATA_WEIGHT),
107
	bitfields_processed: Counter::new(PARACHAIN_INHERENT_DATA_BITFIELDS_PROCESSED),
108
	candidates_processed: CounterVec::new(PARACHAIN_INHERENT_DATA_CANDIDATES_PROCESSED),
109
	dispute_sets_processed: CounterVec::new(PARACHAIN_INHERENT_DATA_DISPUTE_SETS_PROCESSED),
110
	bitfields_signature_checks: CounterVec::new(
111
		PARACHAIN_CREATE_INHERENT_BITFIELDS_SIGNATURE_CHECKS,
112
	),
113
	signature_timings: Histogram::new(PARACHAIN_VERIFY_DISPUTE_SIGNATURE),
114
};