1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
34// 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.
89// 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.
1314// You should have received a copy of the GNU General Public License
15// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
1617use polkadot_node_metrics::metrics::{self, prometheus};
1819#[derive(Clone)]
20pub(crate) struct MetricsInner {
21pub(crate) chain_api_requests: prometheus::CounterVec<prometheus::U64>,
22pub(crate) make_runtime_api_request: prometheus::Histogram,
23}
2425/// Runtime API metrics.
26#[derive(Default, Clone)]
27pub struct Metrics(pub(crate) Option<MetricsInner>);
2829impl Metrics {
30pub fn on_request(&self, succeeded: bool) {
31if let Some(metrics) = &self.0 {
32if 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 }
3940pub fn on_cached_request(&self) {
41self.0
42.as_ref()
43 .map(|metrics| metrics.chain_api_requests.with_label_values(&["cached"]).inc());
44 }
4546/// Provide a timer for `make_runtime_api_request` which observes on drop.
47pub fn time_make_runtime_api_request(
48&self,
49 ) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
50self.0.as_ref().map(|metrics| metrics.make_runtime_api_request.start_timer())
51 }
52}
5354impl metrics::Metrics for Metrics {
55fn try_register(registry: &prometheus::Registry) -> Result<Self, prometheus::PrometheusError> {
56let 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 };
75Ok(Metrics(Some(metrics)))
76 }
77}