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) block_number: prometheus::Histogram,
23pub(crate) block_header: prometheus::Histogram,
24pub(crate) block_weight: prometheus::Histogram,
25pub(crate) finalized_block_hash: prometheus::Histogram,
26pub(crate) finalized_block_number: prometheus::Histogram,
27pub(crate) ancestors: prometheus::Histogram,
28}
2930/// Chain API metrics.
31#[derive(Default, Clone)]
32pub struct Metrics(pub(crate) Option<MetricsInner>);
3334impl Metrics {
35pub fn on_request(&self, succeeded: bool) {
36if let Some(metrics) = &self.0 {
37if succeeded {
38 metrics.chain_api_requests.with_label_values(&["succeeded"]).inc();
39 } else {
40 metrics.chain_api_requests.with_label_values(&["failed"]).inc();
41 }
42 }
43 }
4445/// Provide a timer for `block_number` which observes on drop.
46pub fn time_block_number(&self) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
47self.0.as_ref().map(|metrics| metrics.block_number.start_timer())
48 }
4950/// Provide a timer for `block_header` which observes on drop.
51pub fn time_block_header(&self) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
52self.0.as_ref().map(|metrics| metrics.block_header.start_timer())
53 }
5455/// Provide a timer for `block_weight` which observes on drop.
56pub fn time_block_weight(&self) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
57self.0.as_ref().map(|metrics| metrics.block_weight.start_timer())
58 }
5960/// Provide a timer for `finalized_block_hash` which observes on drop.
61pub fn time_finalized_block_hash(
62&self,
63 ) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
64self.0.as_ref().map(|metrics| metrics.finalized_block_hash.start_timer())
65 }
6667/// Provide a timer for `finalized_block_number` which observes on drop.
68pub fn time_finalized_block_number(
69&self,
70 ) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
71self.0.as_ref().map(|metrics| metrics.finalized_block_number.start_timer())
72 }
7374/// Provide a timer for `ancestors` which observes on drop.
75pub fn time_ancestors(&self) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
76self.0.as_ref().map(|metrics| metrics.ancestors.start_timer())
77 }
78}
7980impl metrics::Metrics for Metrics {
81fn try_register(registry: &prometheus::Registry) -> Result<Self, prometheus::PrometheusError> {
82let metrics = MetricsInner {
83 chain_api_requests: prometheus::register(
84 prometheus::CounterVec::new(
85 prometheus::Opts::new(
86"polkadot_parachain_chain_api_requests_total",
87"Number of Chain API requests served.",
88 ),
89&["success"],
90 )?,
91 registry,
92 )?,
93 block_number: prometheus::register(
94 prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
95"polkadot_parachain_chain_api_block_number",
96"Time spent within `chain_api::block_number`",
97 ))?,
98 registry,
99 )?,
100 block_header: prometheus::register(
101 prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
102"polkadot_parachain_chain_api_block_headers",
103"Time spent within `chain_api::block_headers`",
104 ))?,
105 registry,
106 )?,
107 block_weight: prometheus::register(
108 prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
109"polkadot_parachain_chain_api_block_weight",
110"Time spent within `chain_api::block_weight`",
111 ))?,
112 registry,
113 )?,
114 finalized_block_hash: prometheus::register(
115 prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
116"polkadot_parachain_chain_api_finalized_block_hash",
117"Time spent within `chain_api::finalized_block_hash`",
118 ))?,
119 registry,
120 )?,
121 finalized_block_number: prometheus::register(
122 prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
123"polkadot_parachain_chain_api_finalized_block_number",
124"Time spent within `chain_api::finalized_block_number`",
125 ))?,
126 registry,
127 )?,
128 ancestors: prometheus::register(
129 prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
130"polkadot_parachain_chain_api_ancestors",
131"Time spent within `chain_api::ancestors`",
132 ))?,
133 registry,
134 )?,
135 };
136Ok(Metrics(Some(metrics)))
137 }
138}