1// This file is part of Substrate.
23// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
56// 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.
1011// 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.
1516// 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/>.
1819//! Statement store Prometheus metrics.
2021use std::sync::Arc;
2223use prometheus_endpoint::{register, Counter, PrometheusError, Registry, U64};
2425#[derive(Clone, Default)]
26pub struct MetricsLink(Arc<Option<Metrics>>);
2728impl MetricsLink {
29pub fn new(registry: Option<&Registry>) -> Self {
30Self(Arc::new(registry.and_then(|registry| {
31 Metrics::register(registry)
32 .map_err(|err| {
33log::warn!("Failed to register prometheus metrics: {}", err);
34 })
35 .ok()
36 })))
37 }
3839pub fn report(&self, do_this: impl FnOnce(&Metrics)) {
40if let Some(metrics) = self.0.as_ref() {
41 do_this(metrics);
42 }
43 }
44}
4546/// Statement store Prometheus metrics.
47pub struct Metrics {
48pub submitted_statements: Counter<U64>,
49pub validations_invalid: Counter<U64>,
50pub statements_pruned: Counter<U64>,
51}
5253impl Metrics {
54pub fn register(registry: &Registry) -> Result<Self, PrometheusError> {
55Ok(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}