sc_utils/
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//! Metering primitives and globals
20
21use prometheus::{
22	core::{AtomicU64, GenericCounter, GenericGauge},
23	Error as PrometheusError, Registry,
24};
25use std::sync::LazyLock;
26
27use prometheus::{
28	core::{GenericCounterVec, GenericGaugeVec},
29	Opts,
30};
31
32pub static TOKIO_THREADS_TOTAL: LazyLock<GenericCounter<AtomicU64>> = LazyLock::new(|| {
33	GenericCounter::new("substrate_tokio_threads_total", "Total number of threads created")
34		.expect("Creating of statics doesn't fail. qed")
35});
36
37pub static TOKIO_THREADS_ALIVE: LazyLock<GenericGauge<AtomicU64>> = LazyLock::new(|| {
38	GenericGauge::new("substrate_tokio_threads_alive", "Number of threads alive right now")
39		.expect("Creating of statics doesn't fail. qed")
40});
41
42pub static UNBOUNDED_CHANNELS_COUNTER: LazyLock<GenericCounterVec<AtomicU64>> =
43	LazyLock::new(|| {
44		GenericCounterVec::new(
45			Opts::new(
46				"substrate_unbounded_channel_len",
47				"Items sent/received/dropped on each mpsc::unbounded instance",
48			),
49			&["entity", "action"], // name of channel, send|received|dropped
50		)
51		.expect("Creating of statics doesn't fail. qed")
52	});
53
54pub static UNBOUNDED_CHANNELS_SIZE: LazyLock<GenericGaugeVec<AtomicU64>> = LazyLock::new(|| {
55	GenericGaugeVec::new(
56		Opts::new(
57			"substrate_unbounded_channel_size",
58			"Size (number of messages to be processed) of each mpsc::unbounded instance",
59		),
60		&["entity"], // name of channel
61	)
62	.expect("Creating of statics doesn't fail. qed")
63});
64
65pub static SENT_LABEL: &'static str = "send";
66pub static RECEIVED_LABEL: &'static str = "received";
67pub static DROPPED_LABEL: &'static str = "dropped";
68
69/// Register the statics to report to registry
70pub fn register_globals(registry: &Registry) -> Result<(), PrometheusError> {
71	registry.register(Box::new(TOKIO_THREADS_ALIVE.clone()))?;
72	registry.register(Box::new(TOKIO_THREADS_TOTAL.clone()))?;
73	registry.register(Box::new(UNBOUNDED_CHANNELS_COUNTER.clone()))?;
74	registry.register(Box::new(UNBOUNDED_CHANNELS_SIZE.clone()))?;
75
76	Ok(())
77}