referrerpolicy=no-referrer-when-downgrade
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// 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/>.

use polkadot_node_subsystem::prometheus::HistogramVec;
use polkadot_node_subsystem_util::metrics::{
	self,
	prometheus::{
		self, prometheus::HistogramTimer, Counter, CounterVec, Histogram, Opts, PrometheusError,
		Registry, U64,
	},
};

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

#[derive(Clone)]
struct MetricsInner {
	/// Number of sent chunk requests.
	///
	/// Gets incremented on each sent chunk requests.
	///
	/// Split by chunk type:
	/// - `regular_chunks`
	/// - `systematic_chunks`
	chunk_requests_issued: CounterVec<U64>,

	/// Total number of bytes recovered
	///
	/// Gets incremented on each successful recovery
	recovered_bytes_total: Counter<U64>,

	/// A counter for finished chunk requests.
	///
	/// Split by the chunk type (`regular_chunks` or `systematic_chunks`)
	///
	/// Also split by result:
	/// - `no_such_chunk` ... peer did not have the requested chunk
	/// - `timeout` ... request timed out.
	/// - `error` ... Some networking issue except timeout
	/// - `invalid` ... Chunk was received, but not valid.
	/// - `success`
	chunk_requests_finished: CounterVec<U64>,

	/// A counter for successful chunk requests, split by the network protocol version.
	chunk_request_protocols: CounterVec<U64>,

	/// Number of sent available data requests.
	full_data_requests_issued: Counter<U64>,

	/// Counter for finished available data requests.
	///
	/// Split by the result type:
	///
	/// - `no_such_data` ... peer did not have the requested data
	/// - `timeout` ... request timed out.
	/// - `error` ... Some networking issue except timeout
	/// - `invalid` ... data was received, but not valid.
	/// - `success`
	full_data_requests_finished: CounterVec<U64>,

	/// The duration of request to response.
	///
	/// Split by chunk type (`regular_chunks` or `systematic_chunks`).
	time_chunk_request: HistogramVec,

	/// The duration between the pure recovery and verification.
	///
	/// Split by recovery type (`regular_chunks`, `systematic_chunks` or `full_from_backers`).
	time_erasure_recovery: HistogramVec,

	/// How much time it takes to reconstruct the available data from chunks.
	///
	/// Split by chunk type (`regular_chunks` or `systematic_chunks`), as the algorithms are
	/// different.
	time_erasure_reconstruct: HistogramVec,

	/// How much time it takes to re-encode the data into erasure chunks in order to verify
	/// the root hash of the provided Merkle tree. See `reconstructed_data_matches_root`.
	time_reencode_chunks: Histogram,

	/// Time of a full recovery, including erasure decoding or until we gave
	/// up.
	time_full_recovery: Histogram,

	/// Number of full recoveries that have been finished one way or the other.
	///
	/// Split by recovery `strategy_type` (`full_from_backers, systematic_chunks, regular_chunks,
	/// all`). `all` is used for failed recoveries that tried all available strategies.
	/// Also split by `result` type.
	full_recoveries_finished: CounterVec<U64>,

	/// Number of full recoveries that have been started on this subsystem.
	///
	/// Note: Those are only recoveries which could not get served locally already - so in other
	/// words: Only real recoveries.
	full_recoveries_started: Counter<U64>,
}

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

	/// Increment counter for chunk requests.
	pub fn on_chunk_request_issued(&self, chunk_type: &str) {
		if let Some(metrics) = &self.0 {
			metrics.chunk_requests_issued.with_label_values(&[chunk_type]).inc()
		}
	}

	/// Increment counter for full data requests.
	pub fn on_full_request_issued(&self) {
		if let Some(metrics) = &self.0 {
			metrics.full_data_requests_issued.inc()
		}
	}

	/// A chunk request timed out.
	pub fn on_chunk_request_timeout(&self, chunk_type: &str) {
		if let Some(metrics) = &self.0 {
			metrics
				.chunk_requests_finished
				.with_label_values(&[chunk_type, "timeout"])
				.inc()
		}
	}

	/// A full data request timed out.
	pub fn on_full_request_timeout(&self) {
		if let Some(metrics) = &self.0 {
			metrics.full_data_requests_finished.with_label_values(&["timeout"]).inc()
		}
	}

	/// A chunk request failed because validator did not have its chunk.
	pub fn on_chunk_request_no_such_chunk(&self, chunk_type: &str) {
		if let Some(metrics) = &self.0 {
			metrics
				.chunk_requests_finished
				.with_label_values(&[chunk_type, "no_such_chunk"])
				.inc()
		}
	}

	/// A full data request failed because the validator did not have it.
	pub fn on_full_request_no_such_data(&self) {
		if let Some(metrics) = &self.0 {
			metrics.full_data_requests_finished.with_label_values(&["no_such_data"]).inc()
		}
	}

	/// A chunk request failed for some non timeout related network error.
	pub fn on_chunk_request_error(&self, chunk_type: &str) {
		if let Some(metrics) = &self.0 {
			metrics.chunk_requests_finished.with_label_values(&[chunk_type, "error"]).inc()
		}
	}

	/// A full data request failed for some non timeout related network error.
	pub fn on_full_request_error(&self) {
		if let Some(metrics) = &self.0 {
			metrics.full_data_requests_finished.with_label_values(&["error"]).inc()
		}
	}

	/// A chunk request succeeded, but was not valid.
	pub fn on_chunk_request_invalid(&self, chunk_type: &str) {
		if let Some(metrics) = &self.0 {
			metrics
				.chunk_requests_finished
				.with_label_values(&[chunk_type, "invalid"])
				.inc()
		}
	}

	/// A full data request succeeded, but was not valid.
	pub fn on_full_request_invalid(&self) {
		if let Some(metrics) = &self.0 {
			metrics.full_data_requests_finished.with_label_values(&["invalid"]).inc()
		}
	}

	/// A chunk request succeeded.
	pub fn on_chunk_request_succeeded(&self, chunk_type: &str) {
		if let Some(metrics) = &self.0 {
			metrics
				.chunk_requests_finished
				.with_label_values(&[chunk_type, "success"])
				.inc()
		}
	}

	/// A chunk response was received on the v1 protocol.
	pub fn on_chunk_response_v1(&self) {
		if let Some(metrics) = &self.0 {
			metrics.chunk_request_protocols.with_label_values(&["v1"]).inc()
		}
	}

	/// A chunk response was received on the v2 protocol.
	pub fn on_chunk_response_v2(&self) {
		if let Some(metrics) = &self.0 {
			metrics.chunk_request_protocols.with_label_values(&["v2"]).inc()
		}
	}

	/// A full data request succeeded.
	pub fn on_full_request_succeeded(&self) {
		if let Some(metrics) = &self.0 {
			metrics.full_data_requests_finished.with_label_values(&["success"]).inc()
		}
	}

	/// Get a timer to time request/response duration.
	pub fn time_chunk_request(&self, chunk_type: &str) -> Option<HistogramTimer> {
		self.0.as_ref().map(|metrics| {
			metrics.time_chunk_request.with_label_values(&[chunk_type]).start_timer()
		})
	}

	/// Get a timer to time erasure code recover.
	pub fn time_erasure_recovery(&self, chunk_type: &str) -> Option<HistogramTimer> {
		self.0.as_ref().map(|metrics| {
			metrics.time_erasure_recovery.with_label_values(&[chunk_type]).start_timer()
		})
	}

	/// Get a timer for available data reconstruction.
	pub fn time_erasure_reconstruct(&self, chunk_type: &str) -> Option<HistogramTimer> {
		self.0.as_ref().map(|metrics| {
			metrics.time_erasure_reconstruct.with_label_values(&[chunk_type]).start_timer()
		})
	}

	/// Get a timer to time chunk encoding.
	pub fn time_reencode_chunks(&self) -> Option<HistogramTimer> {
		self.0.as_ref().map(|metrics| metrics.time_reencode_chunks.start_timer())
	}

	/// Get a timer to measure the time of the complete recovery process.
	pub fn time_full_recovery(&self) -> Option<HistogramTimer> {
		self.0.as_ref().map(|metrics| metrics.time_full_recovery.start_timer())
	}

	/// A full recovery succeeded.
	pub fn on_recovery_succeeded(&self, strategy_type: &str, bytes: usize) {
		if let Some(metrics) = &self.0 {
			metrics
				.full_recoveries_finished
				.with_label_values(&["success", strategy_type])
				.inc();
			metrics.recovered_bytes_total.inc_by(bytes as u64)
		}
	}

	/// A full recovery failed (data not available).
	pub fn on_recovery_failed(&self, strategy_type: &str) {
		if let Some(metrics) = &self.0 {
			metrics
				.full_recoveries_finished
				.with_label_values(&["failure", strategy_type])
				.inc()
		}
	}

	/// A full recovery failed (data was recovered, but invalid).
	pub fn on_recovery_invalid(&self, strategy_type: &str) {
		if let Some(metrics) = &self.0 {
			metrics
				.full_recoveries_finished
				.with_label_values(&["invalid", strategy_type])
				.inc()
		}
	}

	/// A recover was started.
	pub fn on_recovery_started(&self) {
		if let Some(metrics) = &self.0 {
			metrics.full_recoveries_started.inc()
		}
	}
}

impl metrics::Metrics for Metrics {
	fn try_register(registry: &Registry) -> Result<Self, PrometheusError> {
		let metrics = MetricsInner {
			chunk_requests_issued: prometheus::register(
				CounterVec::new(
					Opts::new("polkadot_parachain_availability_recovery_chunk_requests_issued",
					"Total number of issued chunk requests."),
					&["type"]
				)?,
				registry,
			)?,
			full_data_requests_issued: prometheus::register(
				Counter::new(
					"polkadot_parachain_availability_recovery_full_data_requests_issued",
					"Total number of issued full data requests.",
				)?,
				registry,
			)?,
			recovered_bytes_total: prometheus::register(
				Counter::new(
					"polkadot_parachain_availability_recovery_bytes_total",
					"Total number of bytes recovered",
				)?,
				registry,
			)?,
			chunk_requests_finished: prometheus::register(
				CounterVec::new(
					Opts::new(
						"polkadot_parachain_availability_recovery_chunk_requests_finished",
						"Total number of chunk requests finished.",
					),
					&["result", "type"],
				)?,
				registry,
			)?,
			chunk_request_protocols: prometheus::register(
				CounterVec::new(
					Opts::new(
						"polkadot_parachain_availability_recovery_chunk_request_protocols",
						"Total number of successful chunk requests, mapped by the protocol version (v1 or v2).",
					),
					&["protocol"],
				)?,
				registry,
			)?,
			full_data_requests_finished: prometheus::register(
				CounterVec::new(
					Opts::new(
						"polkadot_parachain_availability_recovery_full_data_requests_finished",
						"Total number of full data requests finished.",
					),
					&["result"],
				)?,
				registry,
			)?,
			time_chunk_request: prometheus::register(
				prometheus::HistogramVec::new(prometheus::HistogramOpts::new(
					"polkadot_parachain_availability_recovery_time_chunk_request",
					"Time spent waiting for a response to a chunk request",
				), &["type"])?,
				registry,
			)?,
			time_erasure_recovery: prometheus::register(
				prometheus::HistogramVec::new(prometheus::HistogramOpts::new(
					"polkadot_parachain_availability_recovery_time_erasure_recovery",
					"Time spent to recover the erasure code and verify the merkle root by re-encoding as erasure chunks",
				), &["type"])?,
				registry,
			)?,
			time_erasure_reconstruct: prometheus::register(
				prometheus::HistogramVec::new(prometheus::HistogramOpts::new(
					"polkadot_parachain_availability_recovery_time_erasure_reconstruct",
					"Time spent to reconstruct the data from chunks",
				), &["type"])?,
				registry,
			)?,
			time_reencode_chunks: prometheus::register(
				prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
					"polkadot_parachain_availability_reencode_chunks",
					"Time spent re-encoding the data as erasure chunks",
				))?,
				registry,
			)?,
			time_full_recovery: prometheus::register(
				prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
					"polkadot_parachain_availability_recovery_time_total",
					"Time a full recovery process took, either until failure or successful erasure decoding.",
				))?,
				registry,
			)?,
			full_recoveries_finished: prometheus::register(
				CounterVec::new(
					Opts::new(
						"polkadot_parachain_availability_recovery_recoveries_finished",
						"Total number of recoveries that finished.",
					),
					&["result", "strategy_type"],
				)?,
				registry,
			)?,
			full_recoveries_started: prometheus::register(
				Counter::new(
					"polkadot_parachain_availability_recovery_recoveries_started",
					"Total number of started recoveries.",
				)?,
				registry,
			)?,
		};
		Ok(Metrics(Some(metrics)))
	}
}