mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 10:01:17 +00:00
Add more metrics to prometheus (#5034)
* Add a few things * Add finality_grandpa_round * fix fg tests * Nitpicks * Nitpicks * Fix name of prometheus crate
This commit is contained in:
@@ -52,6 +52,7 @@ sp-consensus = { version = "0.8.0-alpha.2", path = "../../primitives/consensus/c
|
||||
sp-consensus-babe = { version = "0.8.0-alpha.2", path = "../../primitives/consensus/babe" }
|
||||
sp-core = { version = "2.0.0-alpha.2", path = "../../primitives/core" }
|
||||
sp-runtime = { version = "2.0.0-alpha.2", path = "../../primitives/runtime" }
|
||||
prometheus-endpoint = { package = "substrate-prometheus-endpoint", version = "0.8.0-alpha.2", path = "../../utils/prometheus" }
|
||||
thiserror = "1"
|
||||
unsigned-varint = { version = "0.3.1", features = ["futures", "futures-codec"] }
|
||||
void = "1.0.2"
|
||||
|
||||
@@ -41,6 +41,7 @@ use core::{fmt, iter};
|
||||
use std::{future::Future, pin::Pin};
|
||||
use std::{error::Error, fs, io::{self, Write}, net::Ipv4Addr, path::{Path, PathBuf}, sync::Arc};
|
||||
use zeroize::Zeroize;
|
||||
use prometheus_endpoint::Registry;
|
||||
|
||||
/// Network initialization parameters.
|
||||
pub struct Params<B: BlockT, H: ExHashT> {
|
||||
@@ -90,6 +91,9 @@ pub struct Params<B: BlockT, H: ExHashT> {
|
||||
|
||||
/// Type to check incoming block announcements.
|
||||
pub block_announce_validator: Box<dyn BlockAnnounceValidator<B> + Send>,
|
||||
|
||||
/// Registry for recording prometheus metrics to.
|
||||
pub metrics_registry: Option<Registry>,
|
||||
}
|
||||
|
||||
bitflags! {
|
||||
|
||||
@@ -45,6 +45,8 @@ pub enum Error {
|
||||
/// The second peer id that was found for the bootnode.
|
||||
second_id: PeerId,
|
||||
},
|
||||
/// Prometheus metrics error.
|
||||
Prometheus(prometheus_endpoint::PrometheusError)
|
||||
}
|
||||
|
||||
// Make `Debug` use the `Display` implementation.
|
||||
@@ -60,6 +62,7 @@ impl std::error::Error for Error {
|
||||
Error::Io(ref err) => Some(err),
|
||||
Error::Client(ref err) => Some(err),
|
||||
Error::DuplicateBootnode { .. } => None,
|
||||
Error::Prometheus(ref err) => Some(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@ use libp2p::swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent};
|
||||
use parking_lot::Mutex;
|
||||
use sc_peerset::PeersetHandle;
|
||||
use sp_runtime::{traits::{Block as BlockT, NumberFor}, ConsensusEngineId};
|
||||
use prometheus_endpoint::{Registry, Gauge, U64, register, PrometheusError};
|
||||
|
||||
use crate::{behaviour::{Behaviour, BehaviourOut}, config::{parse_str_addr, parse_addr}};
|
||||
use crate::{transport, config::NonReservedPeerMode, ReputationChange};
|
||||
@@ -294,6 +295,10 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkWorker<B, H> {
|
||||
from_worker,
|
||||
light_client_rqs: params.on_demand.and_then(|od| od.extract_receiver()),
|
||||
event_streams: Vec::new(),
|
||||
metrics: match params.metrics_registry {
|
||||
Some(registry) => Some(Metrics::register(®istry)?),
|
||||
None => None
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -727,6 +732,26 @@ pub struct NetworkWorker<B: BlockT + 'static, H: ExHashT> {
|
||||
light_client_rqs: Option<mpsc::UnboundedReceiver<RequestData<B>>>,
|
||||
/// Senders for events that happen on the network.
|
||||
event_streams: Vec<mpsc::UnboundedSender<Event>>,
|
||||
/// Prometheus network metrics.
|
||||
metrics: Option<Metrics>
|
||||
}
|
||||
|
||||
struct Metrics {
|
||||
is_major_syncing: Gauge<U64>,
|
||||
peers_count: Gauge<U64>,
|
||||
}
|
||||
|
||||
impl Metrics {
|
||||
fn register(registry: &Registry) -> Result<Self, PrometheusError> {
|
||||
Ok(Self {
|
||||
is_major_syncing: register(Gauge::new(
|
||||
"is_major_syncing", "Whether the node is performing a major sync or not.",
|
||||
)?, registry)?,
|
||||
peers_count: register(Gauge::new(
|
||||
"peers_count", "Number of network gossip peers",
|
||||
)?, registry)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: BlockT + 'static, H: ExHashT> Future for NetworkWorker<B, H> {
|
||||
@@ -818,16 +843,26 @@ impl<B: BlockT + 'static, H: ExHashT> Future for NetworkWorker<B, H> {
|
||||
};
|
||||
}
|
||||
|
||||
let num_connected_peers = this.network_service.user_protocol_mut().num_connected_peers();
|
||||
|
||||
// Update the variables shared with the `NetworkService`.
|
||||
this.num_connected.store(this.network_service.user_protocol_mut().num_connected_peers(), Ordering::Relaxed);
|
||||
this.num_connected.store(num_connected_peers, Ordering::Relaxed);
|
||||
{
|
||||
let external_addresses = Swarm::<B, H>::external_addresses(&this.network_service).cloned().collect();
|
||||
*this.external_addresses.lock() = external_addresses;
|
||||
}
|
||||
this.is_major_syncing.store(match this.network_service.user_protocol_mut().sync_state() {
|
||||
|
||||
let is_major_syncing = match this.network_service.user_protocol_mut().sync_state() {
|
||||
SyncState::Idle => false,
|
||||
SyncState::Downloading => true,
|
||||
}, Ordering::Relaxed);
|
||||
};
|
||||
|
||||
this.is_major_syncing.store(is_major_syncing, Ordering::Relaxed);
|
||||
|
||||
if let Some(metrics) = this.metrics.as_ref() {
|
||||
metrics.is_major_syncing.set(is_major_syncing as u64);
|
||||
metrics.peers_count.set(num_connected_peers as u64);
|
||||
}
|
||||
|
||||
Poll::Pending
|
||||
}
|
||||
|
||||
@@ -640,7 +640,8 @@ pub trait TestNetFactory: Sized {
|
||||
transaction_pool: Arc::new(EmptyTransactionPool),
|
||||
protocol_id: ProtocolId::from(&b"test-protocol-name"[..]),
|
||||
import_queue,
|
||||
block_announce_validator: Box::new(DefaultBlockAnnounceValidator::new(client.clone()))
|
||||
block_announce_validator: Box::new(DefaultBlockAnnounceValidator::new(client.clone())),
|
||||
metrics_registry: None,
|
||||
}).unwrap();
|
||||
|
||||
self.mut_peers(|peers| {
|
||||
@@ -713,7 +714,8 @@ pub trait TestNetFactory: Sized {
|
||||
transaction_pool: Arc::new(EmptyTransactionPool),
|
||||
protocol_id: ProtocolId::from(&b"test-protocol-name"[..]),
|
||||
import_queue,
|
||||
block_announce_validator: Box::new(DefaultBlockAnnounceValidator::new(client.clone()))
|
||||
block_announce_validator: Box::new(DefaultBlockAnnounceValidator::new(client.clone())),
|
||||
metrics_registry: None,
|
||||
}).unwrap();
|
||||
|
||||
self.mut_peers(|peers| {
|
||||
|
||||
Reference in New Issue
Block a user