sp_utils/
metrics.rs

1// This file is part of Substrate.
2
3// Copyright (C) 2020-2021 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 lazy_static::lazy_static;
21use prometheus::{
22	Registry, Error as PrometheusError,
23	core::{ AtomicU64, GenericGauge, GenericCounter },
24};
25
26#[cfg(feature = "metered")]
27use prometheus::{core::GenericCounterVec, Opts};
28
29
30lazy_static! {
31	pub static ref TOKIO_THREADS_TOTAL: GenericCounter<AtomicU64> = GenericCounter::new(
32		"tokio_threads_total", "Total number of threads created"
33	).expect("Creating of statics doesn't fail. qed");
34
35	pub static ref TOKIO_THREADS_ALIVE: GenericGauge<AtomicU64> = GenericGauge::new(
36		"tokio_threads_alive", "Number of threads alive right now"
37	).expect("Creating of statics doesn't fail. qed");
38}
39
40#[cfg(feature = "metered")]
41lazy_static! {
42	pub static ref UNBOUNDED_CHANNELS_COUNTER : GenericCounterVec<AtomicU64> = GenericCounterVec::new(
43		Opts::new("unbounded_channel_len", "Items in each mpsc::unbounded instance"),
44		&["entity", "action"] // 'name of channel, send|received|dropped
45	).expect("Creating of statics doesn't fail. qed");
46
47}
48
49
50/// Register the statics to report to registry
51pub fn register_globals(registry: &Registry) -> Result<(), PrometheusError> {
52	registry.register(Box::new(TOKIO_THREADS_ALIVE.clone()))?;
53	registry.register(Box::new(TOKIO_THREADS_TOTAL.clone()))?;
54
55	#[cfg(feature = "metered")]
56	registry.register(Box::new(UNBOUNDED_CHANNELS_COUNTER.clone()))?;
57
58	Ok(())
59}