referrerpolicy=no-referrer-when-downgrade

sc_statement_store/
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//! Statement store 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/// Statement store Prometheus metrics.
47pub struct Metrics {
48	pub submitted_statements: Counter<U64>,
49	pub validations_invalid: Counter<U64>,
50	pub statements_pruned: Counter<U64>,
51}
52
53impl Metrics {
54	pub fn register(registry: &Registry) -> Result<Self, PrometheusError> {
55		Ok(Self {
56			submitted_statements: register(
57				Counter::new(
58					"substrate_sub_statement_store_submitted_statements",
59					"Total number of statements submitted",
60				)?,
61				registry,
62			)?,
63			validations_invalid: register(
64				Counter::new(
65					"substrate_sub_statement_store_validations_invalid",
66					"Total number of statements that were removed from the pool as invalid",
67				)?,
68				registry,
69			)?,
70			statements_pruned: register(
71				Counter::new(
72					"substrate_sub_statement_store_block_statements",
73					"Total number of statements that was requested to be pruned by block events",
74				)?,
75				registry,
76			)?,
77		})
78	}
79}