sc_transaction_pool/
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.
20
21use std::sync::Arc;
22
23use prometheus_endpoint::{register, Counter, PrometheusError, Registry, U64};
24
25#[derive(Clone, Default)]
26pub struct MetricsLink(Arc<Option<Metrics>>);
27
28impl MetricsLink {
29	pub fn new(registry: Option<&Registry>) -> Self {
30		Self(Arc::new(registry.and_then(|registry| {
31			Metrics::register(registry)
32				.map_err(|err| {
33					log::warn!("Failed to register prometheus metrics: {}", err);
34				})
35				.ok()
36		})))
37	}
38
39	pub fn report(&self, do_this: impl FnOnce(&Metrics)) {
40		if let Some(metrics) = self.0.as_ref() {
41			do_this(metrics);
42		}
43	}
44}
45
46/// Transaction pool Prometheus metrics.
47pub struct Metrics {
48	pub submitted_transactions: Counter<U64>,
49	pub validations_invalid: Counter<U64>,
50	pub block_transactions_pruned: Counter<U64>,
51	pub block_transactions_resubmitted: Counter<U64>,
52}
53
54impl Metrics {
55	pub fn register(registry: &Registry) -> Result<Self, PrometheusError> {
56		Ok(Self {
57			submitted_transactions: register(
58				Counter::new(
59					"substrate_sub_txpool_submitted_transactions",
60					"Total number of transactions submitted",
61				)?,
62				registry,
63			)?,
64			validations_invalid: register(
65				Counter::new(
66					"substrate_sub_txpool_validations_invalid",
67					"Total number of transactions that were removed from the pool as invalid",
68				)?,
69				registry,
70			)?,
71			block_transactions_pruned: register(
72				Counter::new(
73					"substrate_sub_txpool_block_transactions_pruned",
74					"Total number of transactions that was requested to be pruned by block events",
75				)?,
76				registry,
77			)?,
78			block_transactions_resubmitted: register(
79				Counter::new(
80					"substrate_sub_txpool_block_transactions_resubmitted",
81					"Total number of transactions that was requested to be resubmitted by block events",
82				)?,
83				registry,
84			)?,
85		})
86	}
87}
88
89/// Transaction pool api Prometheus metrics.
90pub struct ApiMetrics {
91	pub validations_scheduled: Counter<U64>,
92	pub validations_finished: Counter<U64>,
93}
94
95impl ApiMetrics {
96	/// Register the metrics at the given Prometheus registry.
97	pub fn register(registry: &Registry) -> Result<Self, PrometheusError> {
98		Ok(Self {
99			validations_scheduled: register(
100				Counter::new(
101					"substrate_sub_txpool_validations_scheduled",
102					"Total number of transactions scheduled for validation",
103				)?,
104				registry,
105			)?,
106			validations_finished: register(
107				Counter::new(
108					"substrate_sub_txpool_validations_finished",
109					"Total number of transactions that finished validation",
110				)?,
111				registry,
112			)?,
113		})
114	}
115}
116
117/// An extension trait for [`ApiMetrics`].
118pub trait ApiMetricsExt {
119	/// Report an event to the metrics.
120	fn report(&self, report: impl FnOnce(&ApiMetrics));
121}
122
123impl ApiMetricsExt for Option<Arc<ApiMetrics>> {
124	fn report(&self, report: impl FnOnce(&ApiMetrics)) {
125		if let Some(metrics) = self.as_ref() {
126			report(metrics)
127		}
128	}
129}