// This file is part of Substrate.
// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
use std::{convert::TryFrom, time::SystemTime};
use crate::{NetworkStatus, NetworkState, NetworkStatusSinks, config::Configuration};
use futures_timer::Delay;
use prometheus_endpoint::{register, Gauge, U64, Registry, PrometheusError, Opts, GaugeVec};
use sc_telemetry::{telemetry, SUBSTRATE_INFO};
use sp_api::ProvideRuntimeApi;
use sp_runtime::traits::{NumberFor, Block, SaturatedConversion, UniqueSaturatedInto};
use sp_transaction_pool::{PoolStatus, MaintainedTransactionPool};
use sp_utils::metrics::register_globals;
use sp_utils::mpsc::TracingUnboundedReceiver;
use sc_client_api::{ClientInfo, UsageProvider};
use sc_network::config::Role;
use std::sync::Arc;
use std::time::Duration;
use wasm_timer::Instant;
struct PrometheusMetrics {
// generic info
block_height: GaugeVec,
number_leaves: Gauge,
ready_transactions_number: Gauge,
// I/O
database_cache: Gauge,
state_cache: Gauge,
state_db: GaugeVec,
}
impl PrometheusMetrics {
fn setup(
registry: &Registry,
name: &str,
version: &str,
roles: u64,
) -> Result {
register(Gauge::::with_opts(
Opts::new(
"build_info",
"A metric with a constant '1' value labeled by name, version"
)
.const_label("name", name)
.const_label("version", version)
)?, ®istry)?.set(1);
register(Gauge::::new(
"node_roles", "The roles the node is running as",
)?, ®istry)?.set(roles);
register_globals(registry)?;
let start_time_since_epoch = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)
.unwrap_or_default();
register(Gauge::::new(
"process_start_time_seconds",
"Number of seconds between the UNIX epoch and the moment the process started",
)?, registry)?.set(start_time_since_epoch.as_secs());
Ok(Self {
// generic internals
block_height: register(GaugeVec::new(
Opts::new("block_height", "Block height info of the chain"),
&["status"]
)?, registry)?,
number_leaves: register(Gauge::new(
"number_leaves", "Number of known chain leaves (aka forks)",
)?, registry)?,
ready_transactions_number: register(Gauge::new(
"ready_transactions_number", "Number of transactions in the ready queue",
)?, registry)?,
// I/ O
database_cache: register(Gauge::new(
"database_cache_bytes", "RocksDB cache size in bytes",
)?, registry)?,
state_cache: register(Gauge::new(
"state_cache_bytes", "State cache size in bytes",
)?, registry)?,
state_db: register(GaugeVec::new(
Opts::new("state_db_cache_bytes", "State DB cache in bytes"),
&["subtype"]
)?, registry)?,
})
}
}
/// A `MetricsService` periodically sends general client and
/// network state to the telemetry as well as (optionally)
/// a Prometheus endpoint.
pub struct MetricsService {
metrics: Option,
last_update: Instant,
last_total_bytes_inbound: u64,
last_total_bytes_outbound: u64,
}
impl MetricsService {
/// Creates a `MetricsService` that only sends information
/// to the telemetry.
pub fn new() -> Self {
MetricsService {
metrics: None,
last_total_bytes_inbound: 0,
last_total_bytes_outbound: 0,
last_update: Instant::now(),
}
}
/// Creates a `MetricsService` that sends metrics
/// to prometheus alongside the telemetry.
pub fn with_prometheus(
registry: &Registry,
config: &Configuration,
) -> Result {
let role_bits = match config.role {
Role::Full => 1u64,
Role::Light => 2u64,
Role::Sentry { .. } => 3u64,
Role::Authority { .. } => 4u64,
};
PrometheusMetrics::setup(
registry,
&config.network.node_name,
&config.impl_version,
role_bits,
)
.map(|p| MetricsService {
metrics: Some(p),
last_total_bytes_inbound: 0,
last_total_bytes_outbound: 0,
last_update: Instant::now(),
})
}
/// Returns a never-ending `Future` that performs the
/// metric and telemetry updates with information from
/// the given sources.
pub async fn run(
mut self,
client: Arc,
transactions: Arc,
network: NetworkStatusSinks,
) where
TBl: Block,
TCl: ProvideRuntimeApi + UsageProvider,
TExPool: MaintainedTransactionPool::Hash>,
{
let mut timer = Delay::new(Duration::from_secs(0));
let timer_interval = Duration::from_secs(5);
// Metric and telemetry update interval.
let net_status_interval = timer_interval;
let net_state_interval = Duration::from_secs(30);
// Source of network information.
let mut net_status_rx = Some(network.status_stream(net_status_interval));
let mut net_state_rx = Some(network.state_stream(net_state_interval));
loop {
// Wait for the next tick of the timer.
(&mut timer).await;
// Try to get the latest network information.
let mut net_status = None;
let mut net_state = None;
if let Some(rx) = net_status_rx.as_mut() {
match Self::latest(rx) {
Ok(status) => { net_status = status; }
Err(()) => { net_status_rx = None; }
}
}
if let Some(rx) = net_state_rx.as_mut() {
match Self::latest(rx) {
Ok(state) => { net_state = state; }
Err(()) => { net_state_rx = None; }
}
}
// Update / Send the metrics.
self.update(
&client.usage_info(),
&transactions.status(),
net_status,
net_state,
);
// Schedule next tick.
timer.reset(timer_interval);
}
}
// Try to get the latest value from a receiver, dropping intermediate values.
fn latest(rx: &mut TracingUnboundedReceiver) -> Result