polkadot_node_metrics/lib.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
17//! Metrics helpers
18//!
19//! Collects a bunch of metrics providers and related features such as
20//! `Metronome` for usage with metrics collections.
21//!
22//! This crate also reexports Prometheus metric types which are expected to be implemented by
23//! subsystems.
24
25#![deny(missing_docs)]
26#![deny(unused_imports)]
27
28pub use metered;
29
30/// Cyclic metric collection support.
31pub mod metronome;
32pub use self::metronome::Metronome;
33
34#[cfg(feature = "runtime-metrics")]
35pub mod runtime;
36#[cfg(feature = "runtime-metrics")]
37pub use self::runtime::logger_hook;
38
39/// Export a dummy logger hook when the `runtime-metrics` feature is not enabled.
40#[cfg(not(feature = "runtime-metrics"))]
41pub fn logger_hook() -> impl FnOnce(&mut sc_cli::LoggerBuilder, &sc_service::Configuration) {
42 |_logger_builder, _config| {}
43}
44
45/// This module reexports Prometheus types and defines the [`Metrics`](metrics::Metrics) trait.
46pub mod metrics {
47 /// Reexport Substrate Prometheus types.
48 pub use prometheus_endpoint as prometheus;
49
50 /// Subsystem- or job-specific Prometheus metrics.
51 ///
52 /// Usually implemented as a wrapper for `Option<ActualMetrics>`
53 /// to ensure `Default` bounds or as a dummy type ().
54 /// Prometheus metrics internally hold an `Arc` reference, so cloning them is fine.
55 pub trait Metrics: Default + Clone {
56 /// Try to register metrics in the Prometheus registry.
57 fn try_register(
58 registry: &prometheus::Registry,
59 ) -> Result<Self, prometheus::PrometheusError>;
60
61 /// Convenience method to register metrics in the optional Prometheus registry.
62 ///
63 /// If no registry is provided, returns `Default::default()`. Otherwise, returns the same
64 /// thing that `try_register` does.
65 fn register(
66 registry: Option<&prometheus::Registry>,
67 ) -> Result<Self, prometheus::PrometheusError> {
68 match registry {
69 None => Ok(Self::default()),
70 Some(registry) => Self::try_register(registry),
71 }
72 }
73 }
74
75 // dummy impl
76 impl Metrics for () {
77 fn try_register(
78 _registry: &prometheus::Registry,
79 ) -> Result<(), prometheus::PrometheusError> {
80 Ok(())
81 }
82 }
83}
84
85#[cfg(all(feature = "runtime-metrics", not(feature = "runtime-benchmarks"), test))]
86mod tests;