referrerpolicy=no-referrer-when-downgrade

sc_network/litep2p/shim/request_response/
metrics.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Metrics for [`RequestResponseProtocol`](super::RequestResponseProtocol).
20
21use crate::{service::metrics::Metrics, types::ProtocolName};
22
23use std::time::Duration;
24
25/// Request-response metrics.
26pub struct RequestResponseMetrics {
27	/// Metrics.
28	metrics: Option<Metrics>,
29
30	/// Protocol name.
31	protocol: ProtocolName,
32}
33
34impl RequestResponseMetrics {
35	pub fn new(metrics: Option<Metrics>, protocol: ProtocolName) -> Self {
36		Self { metrics, protocol }
37	}
38
39	/// Register inbound request failure to Prometheus
40	pub fn register_inbound_request_failure(&self, reason: &str) {
41		if let Some(metrics) = &self.metrics {
42			metrics
43				.requests_in_failure_total
44				.with_label_values(&[&self.protocol, reason])
45				.inc();
46		}
47	}
48
49	/// Register inbound request success to Prometheus
50	pub fn register_inbound_request_success(&self, serve_time: Duration) {
51		if let Some(metrics) = &self.metrics {
52			metrics
53				.requests_in_success_total
54				.with_label_values(&[&self.protocol])
55				.observe(serve_time.as_secs_f64());
56		}
57	}
58
59	/// Register inbound request failure to Prometheus
60	pub fn register_outbound_request_failure(&self, reason: &str) {
61		if let Some(metrics) = &self.metrics {
62			metrics
63				.requests_out_failure_total
64				.with_label_values(&[&self.protocol, reason])
65				.inc();
66		}
67	}
68
69	/// Register inbound request success to Prometheus
70	pub fn register_outbound_request_success(&self, duration: Duration) {
71		if let Some(metrics) = &self.metrics {
72			metrics
73				.requests_out_success_total
74				.with_label_values(&[&self.protocol])
75				.observe(duration.as_secs_f64());
76		}
77	}
78}