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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright (C) 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/>.

//! Prometheus metrics related to the overseer and its channels.

use super::*;
pub use polkadot_node_metrics::metrics::{self, prometheus, Metrics as MetricsTrait};

/// Overseer Prometheus metrics.
#[derive(Clone)]
struct MetricsInner {
	activated_heads_total: prometheus::Counter<prometheus::U64>,
	deactivated_heads_total: prometheus::Counter<prometheus::U64>,
	messages_relayed_total: prometheus::Counter<prometheus::U64>,

	to_subsystem_bounded_tof: prometheus::HistogramVec,
	to_subsystem_bounded_sent: prometheus::GaugeVec<prometheus::U64>,
	to_subsystem_bounded_received: prometheus::GaugeVec<prometheus::U64>,
	to_subsystem_bounded_blocked: prometheus::GaugeVec<prometheus::U64>,

	to_subsystem_unbounded_tof: prometheus::HistogramVec,
	to_subsystem_unbounded_sent: prometheus::GaugeVec<prometheus::U64>,
	to_subsystem_unbounded_received: prometheus::GaugeVec<prometheus::U64>,

	signals_sent: prometheus::GaugeVec<prometheus::U64>,
	signals_received: prometheus::GaugeVec<prometheus::U64>,

	#[cfg(any(target_os = "linux", feature = "jemalloc-allocator"))]
	memory_stats_resident: prometheus::Gauge<prometheus::U64>,
	#[cfg(any(target_os = "linux", feature = "jemalloc-allocator"))]
	memory_stats_allocated: prometheus::Gauge<prometheus::U64>,
}

/// A shareable metrics type for usage with the overseer.
#[derive(Default, Clone)]
pub struct Metrics(Option<MetricsInner>);

impl Metrics {
	pub(crate) fn on_head_activated(&self) {
		if let Some(metrics) = &self.0 {
			metrics.activated_heads_total.inc();
		}
	}

	pub(crate) fn on_head_deactivated(&self) {
		if let Some(metrics) = &self.0 {
			metrics.deactivated_heads_total.inc();
		}
	}

	pub(crate) fn on_message_relayed(&self) {
		if let Some(metrics) = &self.0 {
			metrics.messages_relayed_total.inc();
		}
	}

	#[cfg(any(target_os = "linux", feature = "jemalloc-allocator"))]
	pub(crate) fn memory_stats_snapshot(
		&self,
		memory_stats: memory_stats::MemoryAllocationSnapshot,
	) {
		if let Some(metrics) = &self.0 {
			metrics.memory_stats_allocated.set(memory_stats.allocated as u64);
			metrics.memory_stats_resident.set(memory_stats.resident as u64);
		}
	}

	pub(crate) fn channel_metrics_snapshot(
		&self,
		collection: impl IntoIterator<Item = (&'static str, SubsystemMeterReadouts)>,
	) {
		if let Some(metrics) = &self.0 {
			collection
				.into_iter()
				.for_each(|(name, readouts): (_, SubsystemMeterReadouts)| {
					metrics
						.to_subsystem_bounded_sent
						.with_label_values(&[name])
						.set(readouts.bounded.sent as u64);

					metrics
						.to_subsystem_bounded_received
						.with_label_values(&[name])
						.set(readouts.bounded.received as u64);

					metrics
						.to_subsystem_bounded_blocked
						.with_label_values(&[name])
						.set(readouts.bounded.blocked as u64);

					metrics
						.to_subsystem_unbounded_sent
						.with_label_values(&[name])
						.set(readouts.unbounded.sent as u64);

					metrics
						.to_subsystem_unbounded_received
						.with_label_values(&[name])
						.set(readouts.unbounded.received as u64);

					metrics
						.signals_sent
						.with_label_values(&[name])
						.set(readouts.signals.sent as u64);

					metrics
						.signals_received
						.with_label_values(&[name])
						.set(readouts.signals.received as u64);

					let hist_bounded = metrics.to_subsystem_bounded_tof.with_label_values(&[name]);
					for tof in readouts.bounded.tof {
						hist_bounded.observe(tof.as_f64());
					}

					let hist_unbounded =
						metrics.to_subsystem_unbounded_tof.with_label_values(&[name]);
					for tof in readouts.unbounded.tof {
						hist_unbounded.observe(tof.as_f64());
					}
				});
		}
	}
}

impl MetricsTrait for Metrics {
	fn try_register(registry: &prometheus::Registry) -> Result<Self, prometheus::PrometheusError> {
		let metrics = MetricsInner {
			activated_heads_total: prometheus::register(
				prometheus::Counter::new(
					"polkadot_parachain_activated_heads_total",
					"Number of activated heads.",
				)?,
				registry,
			)?,
			deactivated_heads_total: prometheus::register(
				prometheus::Counter::new(
					"polkadot_parachain_deactivated_heads_total",
					"Number of deactivated heads.",
				)?,
				registry,
			)?,
			messages_relayed_total: prometheus::register(
				prometheus::Counter::new(
					"polkadot_parachain_messages_relayed_total",
					"Number of messages relayed by Overseer.",
				)?,
				registry,
			)?,
			to_subsystem_bounded_tof: prometheus::register(
				prometheus::HistogramVec::new(
					prometheus::HistogramOpts::new(
						"polkadot_parachain_subsystem_bounded_tof",
						"Duration spent in a particular channel from entrance to removal",
					)
					.buckets(vec![
						0.0001, 0.0004, 0.0016, 0.0064, 0.0256, 0.1024, 0.4096, 1.6384, 3.2768,
						4.9152, 6.5536,
					]),
					&["subsystem_name"],
				)?,
				registry,
			)?,
			to_subsystem_bounded_sent: prometheus::register(
				prometheus::GaugeVec::<prometheus::U64>::new(
					prometheus::Opts::new(
						"polkadot_parachain_subsystem_bounded_sent",
						"Number of elements sent to subsystems' bounded queues",
					),
					&["subsystem_name"],
				)?,
				registry,
			)?,
			to_subsystem_bounded_received: prometheus::register(
				prometheus::GaugeVec::<prometheus::U64>::new(
					prometheus::Opts::new(
						"polkadot_parachain_subsystem_bounded_received",
						"Number of elements received by subsystems' bounded queues",
					),
					&["subsystem_name"],
				)?,
				registry,
			)?,
			to_subsystem_bounded_blocked: prometheus::register(
				prometheus::GaugeVec::<prometheus::U64>::new(
					prometheus::Opts::new(
						"polkadot_parachain_subsystem_bounded_blocked",
						"Number of times senders blocked while sending messages to a subsystem",
					),
					&["subsystem_name"],
				)?,
				registry,
			)?,
			to_subsystem_unbounded_tof: prometheus::register(
				prometheus::HistogramVec::new(
					prometheus::HistogramOpts::new(
						"polkadot_parachain_subsystem_unbounded_tof",
						"Duration spent in a particular channel from entrance to removal",
					)
					.buckets(vec![
						0.0001, 0.0004, 0.0016, 0.0064, 0.0256, 0.1024, 0.4096, 1.6384, 3.2768,
						4.9152, 6.5536,
					]),
					&["subsystem_name"],
				)?,
				registry,
			)?,
			to_subsystem_unbounded_sent: prometheus::register(
				prometheus::GaugeVec::<prometheus::U64>::new(
					prometheus::Opts::new(
						"polkadot_parachain_subsystem_unbounded_sent",
						"Number of elements sent to subsystems' unbounded queues",
					),
					&["subsystem_name"],
				)?,
				registry,
			)?,
			to_subsystem_unbounded_received: prometheus::register(
				prometheus::GaugeVec::<prometheus::U64>::new(
					prometheus::Opts::new(
						"polkadot_parachain_subsystem_unbounded_received",
						"Number of elements received by subsystems' unbounded queues",
					),
					&["subsystem_name"],
				)?,
				registry,
			)?,
			signals_sent: prometheus::register(
				prometheus::GaugeVec::<prometheus::U64>::new(
					prometheus::Opts::new(
						"polkadot_parachain_overseer_signals_sent",
						"Number of signals sent by overseer to subsystems",
					),
					&["subsystem_name"],
				)?,
				registry,
			)?,
			signals_received: prometheus::register(
				prometheus::GaugeVec::<prometheus::U64>::new(
					prometheus::Opts::new(
						"polkadot_parachain_overseer_signals_received",
						"Number of signals received by subsystems from overseer",
					),
					&["subsystem_name"],
				)?,
				registry,
			)?,
			#[cfg(any(target_os = "linux", feature = "jemalloc-allocator"))]
			memory_stats_allocated: prometheus::register(
				prometheus::Gauge::<prometheus::U64>::new(
					"polkadot_memory_allocated",
					"Total bytes allocated by the node",
				)?,
				registry,
			)?,
			#[cfg(any(target_os = "linux", feature = "jemalloc-allocator"))]
			memory_stats_resident: prometheus::register(
				prometheus::Gauge::<prometheus::U64>::new(
					"polkadot_memory_resident",
					"Bytes allocated by the node, and held in RAM",
				)?,
				registry,
			)?,
		};
		Ok(Metrics(Some(metrics)))
	}
}

impl fmt::Debug for Metrics {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		f.write_str("Metrics {{...}}")
	}
}