referrerpolicy=no-referrer-when-downgrade

sc_transaction_pool/common/
metrics.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Transaction pool Prometheus metrics for implementation of Chain API.
20
21use prometheus_endpoint::{register, Counter, PrometheusError, Registry, U64};
22use std::sync::Arc;
23
24use crate::LOG_TARGET;
25
26/// Provides interface to register the specific metrics in the Prometheus register.
27pub(crate) trait MetricsRegistrant {
28	/// Registers the metrics at given Prometheus registry.
29	fn register(registry: &Registry) -> Result<Box<Self>, PrometheusError>;
30}
31
32/// Generic structure to keep a link to metrics register.
33pub(crate) struct GenericMetricsLink<M: MetricsRegistrant>(Arc<Option<Box<M>>>);
34
35impl<M: MetricsRegistrant> Default for GenericMetricsLink<M> {
36	fn default() -> Self {
37		Self(Arc::from(None))
38	}
39}
40
41impl<M: MetricsRegistrant> Clone for GenericMetricsLink<M> {
42	fn clone(&self) -> Self {
43		Self(self.0.clone())
44	}
45}
46
47impl<M: MetricsRegistrant> GenericMetricsLink<M> {
48	pub fn new(registry: Option<&Registry>) -> Self {
49		Self(Arc::new(registry.and_then(|registry| {
50			M::register(registry)
51				.map_err(|error| {
52					tracing::warn!(
53						target: LOG_TARGET,
54						%error,
55						"Failed to register prometheus metrics"
56					);
57				})
58				.ok()
59		})))
60	}
61
62	pub fn report(&self, do_this: impl FnOnce(&M)) {
63		if let Some(metrics) = self.0.as_ref() {
64			do_this(&**metrics);
65		}
66	}
67}
68
69/// Transaction pool api Prometheus metrics.
70pub struct ApiMetrics {
71	pub validations_scheduled: Counter<U64>,
72	pub validations_finished: Counter<U64>,
73}
74
75impl ApiMetrics {
76	/// Register the metrics at the given Prometheus registry.
77	pub fn register(registry: &Registry) -> Result<Self, PrometheusError> {
78		Ok(Self {
79			validations_scheduled: register(
80				Counter::new(
81					"substrate_sub_txpool_validations_scheduled",
82					"Total number of transactions scheduled for validation",
83				)?,
84				registry,
85			)?,
86			validations_finished: register(
87				Counter::new(
88					"substrate_sub_txpool_validations_finished",
89					"Total number of transactions that finished validation",
90				)?,
91				registry,
92			)?,
93		})
94	}
95}
96
97/// An extension trait for [`ApiMetrics`].
98pub trait ApiMetricsExt {
99	/// Report an event to the metrics.
100	fn report(&self, report: impl FnOnce(&ApiMetrics));
101}
102
103impl ApiMetricsExt for Option<Arc<ApiMetrics>> {
104	fn report(&self, report: impl FnOnce(&ApiMetrics)) {
105		if let Some(metrics) = self.as_ref() {
106			report(metrics)
107		}
108	}
109}