sc_utils/
metrics.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Metering primitives and globals
19
20use prometheus::{
21	core::{AtomicU64, GenericCounter, GenericGauge},
22	Error as PrometheusError, Registry,
23};
24use std::sync::LazyLock;
25
26use prometheus::{
27	core::{GenericCounterVec, GenericGaugeVec},
28	Opts,
29};
30
31pub static TOKIO_THREADS_TOTAL: LazyLock<GenericCounter<AtomicU64>> = LazyLock::new(|| {
32	GenericCounter::new("substrate_tokio_threads_total", "Total number of threads created")
33		.expect("Creating of statics doesn't fail. qed")
34});
35
36pub static TOKIO_THREADS_ALIVE: LazyLock<GenericGauge<AtomicU64>> = LazyLock::new(|| {
37	GenericGauge::new("substrate_tokio_threads_alive", "Number of threads alive right now")
38		.expect("Creating of statics doesn't fail. qed")
39});
40
41pub static UNBOUNDED_CHANNELS_COUNTER: LazyLock<GenericCounterVec<AtomicU64>> =
42	LazyLock::new(|| {
43		GenericCounterVec::new(
44			Opts::new(
45				"substrate_unbounded_channel_len",
46				"Items sent/received/dropped on each mpsc::unbounded instance",
47			),
48			&["entity", "action"], // name of channel, send|received|dropped
49		)
50		.expect("Creating of statics doesn't fail. qed")
51	});
52
53pub static UNBOUNDED_CHANNELS_SIZE: LazyLock<GenericGaugeVec<AtomicU64>> = LazyLock::new(|| {
54	GenericGaugeVec::new(
55		Opts::new(
56			"substrate_unbounded_channel_size",
57			"Size (number of messages to be processed) of each mpsc::unbounded instance",
58		),
59		&["entity"], // name of channel
60	)
61	.expect("Creating of statics doesn't fail. qed")
62});
63
64pub static SENT_LABEL: &'static str = "send";
65pub static RECEIVED_LABEL: &'static str = "received";
66pub static DROPPED_LABEL: &'static str = "dropped";
67
68/// Register the statics to report to registry
69pub fn register_globals(registry: &Registry) -> Result<(), PrometheusError> {
70	registry.register(Box::new(TOKIO_THREADS_ALIVE.clone()))?;
71	registry.register(Box::new(TOKIO_THREADS_TOTAL.clone()))?;
72	registry.register(Box::new(UNBOUNDED_CHANNELS_COUNTER.clone()))?;
73	registry.register(Box::new(UNBOUNDED_CHANNELS_SIZE.clone()))?;
74
75	Ok(())
76}