diff --git a/Cargo.lock b/Cargo.lock index aa0fb49b9..1dc437afa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6313,6 +6313,7 @@ dependencies = [ "ream-events-beacon", "ream-executor", "ream-light-client", + "ream-metrics", "ream-network-spec", "ream-network-state-lean", "ream-peer", diff --git a/crates/common/metrics/src/lib.rs b/crates/common/metrics/src/lib.rs index fdc1544d3..6a6d124c0 100644 --- a/crates/common/metrics/src/lib.rs +++ b/crates/common/metrics/src/lib.rs @@ -145,6 +145,28 @@ lazy_static::lazy_static! { &[], default_registry() ).expect("failed to create PQ_SIGNATURE_ATTESTATION_VERIFICATION_TIME histogram vec"); + + // Network Metrics + pub static ref LEAN_PEER_COUNT: IntGaugeVec = register_int_gauge_vec_with_registry!( + "lean_connected_peers", + "Number of connected peers", + &[], + default_registry() + ).expect("failed to create LEAN_PEER_COUNT int gauge vec"); + + pub static ref LEAN_CONNECTION_EVENT_TOTAL: IntCounterVec = register_int_counter_vec_with_registry!( + "lean_peer_connection_events_total", + "Number of connection events", + &[], + default_registry() + ).expect("failed to create LEAN_CONNECTION_EVENT_TOTAL int counter vec"); + + pub static ref LEAN_DISCONNECTION_EVENT_TOTAL: IntCounterVec = register_int_counter_vec_with_registry!( + "lean_peer_disconnection_events_total", + "Number of disconnection events", + &[], + default_registry() + ).expect("failed to create LEAN_DISCONNECTION_EVENT_TOTAL int counter vec"); } /// Set the value of a gauge metric diff --git a/crates/networking/p2p/Cargo.toml b/crates/networking/p2p/Cargo.toml index c19b0ab26..972d122e5 100644 --- a/crates/networking/p2p/Cargo.toml +++ b/crates/networking/p2p/Cargo.toml @@ -49,6 +49,7 @@ ream-discv5.workspace = true ream-events-beacon.workspace = true ream-executor.workspace = true ream-light-client.workspace = true +ream-metrics.workspace = true ream-network-spec.workspace = true ream-network-state-lean.workspace = true ream-peer.workspace = true diff --git a/crates/networking/p2p/src/network/lean/mod.rs b/crates/networking/p2p/src/network/lean/mod.rs index 80eba59d5..109211235 100644 --- a/crates/networking/p2p/src/network/lean/mod.rs +++ b/crates/networking/p2p/src/network/lean/mod.rs @@ -24,6 +24,10 @@ use libp2p::{ use libp2p_identity::{Keypair, PeerId, secp256k1}; use ream_chain_lean::{messages::LeanChainServiceMessage, p2p_request::LeanP2PRequest}; use ream_executor::ReamExecutor; +use ream_metrics::{ + LEAN_CONNECTION_EVENT_TOTAL, LEAN_DISCONNECTION_EVENT_TOTAL, LEAN_PEER_COUNT, + inc_int_counter_vec, set_int_gauge_vec, +}; use ream_network_spec::networks::{Devnet, lean_network_spec}; use ream_network_state_lean::{NetworkState, cached_peer::CachedPeer}; use ream_peer::{ConnectionState, Direction}; @@ -225,6 +229,7 @@ impl LeanNetworkService { pub async fn start(&mut self, bootnodes: Bootnodes) -> anyhow::Result<()> { info!("LeanNetworkService started"); + set_int_gauge_vec(&LEAN_PEER_COUNT, 0, &[]); self.connect_to_bootnodes(bootnodes.to_multiaddrs_lean()) .await; @@ -375,6 +380,12 @@ impl LeanNetworkService { ConnectionState::Connected, direction, ); + set_int_gauge_vec( + &LEAN_PEER_COUNT, + self.network_state.connected_peers() as i64, + &[], + ); + inc_int_counter_vec(&LEAN_CONNECTION_EVENT_TOTAL, &[]); info!( "Connected to peer: {peer_id:?} {:?}", @@ -395,6 +406,12 @@ impl LeanNetworkService { ConnectionState::Disconnected, direction, ); + set_int_gauge_vec( + &LEAN_PEER_COUNT, + self.network_state.connected_peers() as i64, + &[], + ); + inc_int_counter_vec(&LEAN_DISCONNECTION_EVENT_TOTAL, &[]); info!("Disconnected from peer: {peer_id:?}"); Some(ReamNetworkEvent::PeerDisconnected(peer_id))