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

            
21
use lazy_static::lazy_static;
22
use prometheus::{
23
	core::{AtomicU64, GenericCounter, GenericGauge},
24
	Error as PrometheusError, Registry,
25
};
26

            
27
use prometheus::{
28
	core::{GenericCounterVec, GenericGaugeVec},
29
	Opts,
30
};
31

            
32
lazy_static! {
33
	pub static ref TOKIO_THREADS_TOTAL: GenericCounter<AtomicU64> =
34
		GenericCounter::new("substrate_tokio_threads_total", "Total number of threads created")
35
			.expect("Creating of statics doesn't fail. qed");
36
	pub static ref TOKIO_THREADS_ALIVE: GenericGauge<AtomicU64> =
37
		GenericGauge::new("substrate_tokio_threads_alive", "Number of threads alive right now")
38
			.expect("Creating of statics doesn't fail. qed");
39
}
40

            
41
lazy_static! {
42
	pub static ref UNBOUNDED_CHANNELS_COUNTER: GenericCounterVec<AtomicU64> = GenericCounterVec::new(
43
		Opts::new(
44
			"substrate_unbounded_channel_len",
45
			"Items sent/received/dropped on each mpsc::unbounded instance"
46
		),
47
		&["entity", "action"], // name of channel, send|received|dropped
48
	).expect("Creating of statics doesn't fail. qed");
49
	pub static ref UNBOUNDED_CHANNELS_SIZE: GenericGaugeVec<AtomicU64> = GenericGaugeVec::new(
50
		Opts::new(
51
			"substrate_unbounded_channel_size",
52
			"Size (number of messages to be processed) of each mpsc::unbounded instance",
53
		),
54
		&["entity"], // name of channel
55
	).expect("Creating of statics doesn't fail. qed");
56
}
57

            
58
pub static SENT_LABEL: &'static str = "send";
59
pub static RECEIVED_LABEL: &'static str = "received";
60
pub static DROPPED_LABEL: &'static str = "dropped";
61

            
62
/// Register the statics to report to registry
63
pub fn register_globals(registry: &Registry) -> Result<(), PrometheusError> {
64
	registry.register(Box::new(TOKIO_THREADS_ALIVE.clone()))?;
65
	registry.register(Box::new(TOKIO_THREADS_TOTAL.clone()))?;
66
	registry.register(Box::new(UNBOUNDED_CHANNELS_COUNTER.clone()))?;
67
	registry.register(Box::new(UNBOUNDED_CHANNELS_SIZE.clone()))?;
68

            
69
	Ok(())
70
}