referrerpolicy=no-referrer-when-downgrade

polkadot_node_core_runtime_api/
metrics.rs

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
17use polkadot_node_metrics::metrics::{self, prometheus};
18
19#[derive(Clone)]
20pub(crate) struct MetricsInner {
21	pub(crate) chain_api_requests: prometheus::CounterVec<prometheus::U64>,
22	pub(crate) make_runtime_api_request: prometheus::Histogram,
23}
24
25/// Runtime API metrics.
26#[derive(Default, Clone)]
27pub struct Metrics(pub(crate) Option<MetricsInner>);
28
29impl Metrics {
30	pub fn on_request(&self, succeeded: bool) {
31		if let Some(metrics) = &self.0 {
32			if succeeded {
33				metrics.chain_api_requests.with_label_values(&["succeeded"]).inc();
34			} else {
35				metrics.chain_api_requests.with_label_values(&["failed"]).inc();
36			}
37		}
38	}
39
40	pub fn on_cached_request(&self) {
41		self.0
42			.as_ref()
43			.map(|metrics| metrics.chain_api_requests.with_label_values(&["cached"]).inc());
44	}
45
46	/// Provide a timer for `make_runtime_api_request` which observes on drop.
47	pub fn time_make_runtime_api_request(
48		&self,
49	) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
50		self.0.as_ref().map(|metrics| metrics.make_runtime_api_request.start_timer())
51	}
52}
53
54impl metrics::Metrics for Metrics {
55	fn try_register(registry: &prometheus::Registry) -> Result<Self, prometheus::PrometheusError> {
56		let metrics = MetricsInner {
57			chain_api_requests: prometheus::register(
58				prometheus::CounterVec::new(
59					prometheus::Opts::new(
60						"polkadot_parachain_runtime_api_requests_total",
61						"Number of Runtime API requests served.",
62					),
63					&["success"],
64				)?,
65				registry,
66			)?,
67			make_runtime_api_request: prometheus::register(
68				prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
69					"polkadot_parachain_runtime_api_make_runtime_api_request",
70					"Time spent within `runtime_api::make_runtime_api_request`",
71				))?,
72				registry,
73			)?,
74		};
75		Ok(Metrics(Some(metrics)))
76	}
77}