cumulus_relay_chain_rpc_interface/metrics.rs
1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Cumulus.
3// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
4
5// Cumulus is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9
10// Cumulus is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14
15// You should have received a copy of the GNU General Public License
16// along with Cumulus. If not, see <https://www.gnu.org/licenses/>.
17
18use prometheus::{Error as PrometheusError, HistogramTimer, Registry};
19use prometheus_endpoint::{HistogramOpts, HistogramVec, Opts};
20
21/// Gathers metrics about the blockchain RPC client.
22#[derive(Clone)]
23pub(crate) struct RelaychainRpcMetrics {
24 rpc_request: HistogramVec,
25}
26
27impl RelaychainRpcMetrics {
28 pub(crate) fn register(registry: &Registry) -> Result<Self, PrometheusError> {
29 Ok(Self {
30 rpc_request: prometheus_endpoint::register(
31 HistogramVec::new(
32 HistogramOpts {
33 common_opts: Opts::new(
34 "relay_chain_rpc_interface",
35 "Tracks stats about cumulus relay chain RPC interface",
36 ),
37 buckets: prometheus::exponential_buckets(0.001, 4.0, 9)
38 .expect("function parameters are constant and always valid; qed"),
39 },
40 &["method"],
41 )?,
42 registry,
43 )?,
44 })
45 }
46
47 pub(crate) fn start_request_timer(&self, method: &str) -> HistogramTimer {
48 self.rpc_request.with_label_values(&[method]).start_timer()
49 }
50}