From 95b373e80094b09ab5dc6c1e351deccf68ff7dd9 Mon Sep 17 00:00:00 2001 From: Aaryamann Challani <43716372+rymnc@users.noreply.github.com> Date: Fri, 30 Aug 2024 13:39:54 +0530 Subject: [PATCH] chore(p2p_service): add metrics for number of blocks requested over p2p req/res protocol (#2135) ## Linked Issues/PRs - #2023 - #2112 ## Description We bubble up the usage of a new function `log_metrics` to clean up how metrics are being logged in the p2p module. additionally, we also specify a `Gauge` for how many blocks worth of data has been requested, so we can re-use those numbers in our simulations to find the most optimal lookup method. ## Checklist - [x] Breaking changes are clearly marked as such in the PR description and changelog - [x] New behavior is reflected in tests - [x] [The specification](https://github.com/FuelLabs/fuel-specs/) matches the implemented behavior (link update PR if changes are needed) ### Before requesting review - [x] I have reviewed the code myself - [ ] I have created follow-up issues caused by this PR and linked them here ### After merging, notify other teams [Add or remove entries as needed] - [ ] [Rust SDK](https://github.com/FuelLabs/fuels-rs/) - [ ] [Sway compiler](https://github.com/FuelLabs/sway/) - [ ] [Platform documentation](https://github.com/FuelLabs/devrel-requests/issues/new?assignees=&labels=new+request&projects=&template=NEW-REQUEST.yml&title=%5BRequest%5D%3A+) (for out-of-organization contributors, the person merging the PR will do this) - [ ] Someone else? --- CHANGELOG.md | 4 ++++ crates/metrics/src/p2p_metrics.rs | 26 +++++++++++++++++++-- crates/services/p2p/src/p2p_service.rs | 15 +++++++++---- crates/services/p2p/src/service.rs | 31 +++++++++++++++++++++++++- 4 files changed, 69 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8267a01c499..00b02fa32d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Added +- [2135](https://github.com/FuelLabs/fuel-core/pull/2135): Added metrics logging for number of blocks served over the p2p req/res protocol. + + ## [Version 0.35.0] ### Added diff --git a/crates/metrics/src/p2p_metrics.rs b/crates/metrics/src/p2p_metrics.rs index b9123bed64b..a139cfb6ee3 100644 --- a/crates/metrics/src/p2p_metrics.rs +++ b/crates/metrics/src/p2p_metrics.rs @@ -1,16 +1,24 @@ use crate::global_registry; -use prometheus_client::metrics::counter::Counter; +use prometheus_client::metrics::{ + counter::Counter, + gauge::Gauge, +}; use std::sync::OnceLock; pub struct P2PMetrics { pub unique_peers: Counter, + pub blocks_requested: Gauge, } impl P2PMetrics { fn new() -> Self { let unique_peers = Counter::default(); + let blocks_requested = Gauge::default(); - let metrics = P2PMetrics { unique_peers }; + let metrics = P2PMetrics { + unique_peers, + blocks_requested, + }; let mut registry = global_registry().registry.lock(); registry.register( @@ -19,6 +27,12 @@ impl P2PMetrics { metrics.unique_peers.clone(), ); + registry.register( + "Blocks_Requested", + "A Gauge which keeps track of how many blocks were requested and served over the p2p req/res protocol", + metrics.blocks_requested.clone() + ); + metrics } } @@ -28,3 +42,11 @@ static P2P_METRICS: OnceLock = OnceLock::new(); pub fn p2p_metrics() -> &'static P2PMetrics { P2P_METRICS.get_or_init(P2PMetrics::new) } + +pub fn increment_unique_peers() { + p2p_metrics().unique_peers.inc(); +} + +pub fn set_blocks_requested(count: usize) { + p2p_metrics().blocks_requested.set(count as i64); +} diff --git a/crates/services/p2p/src/p2p_service.rs b/crates/services/p2p/src/p2p_service.rs index 41ef9fb8109..e1afcad3a60 100644 --- a/crates/services/p2p/src/p2p_service.rs +++ b/crates/services/p2p/src/p2p_service.rs @@ -35,7 +35,7 @@ use crate::{ }, TryPeerId, }; -use fuel_core_metrics::p2p_metrics::p2p_metrics; +use fuel_core_metrics::p2p_metrics::increment_unique_peers; use fuel_core_types::{ fuel_types::BlockHeight, services::p2p::peer_reputation::AppScore, @@ -271,6 +271,15 @@ impl FuelP2PService { } } + pub fn update_metrics(&self, update_fn: T) + where + T: FnOnce(), + { + if self.metrics { + update_fn(); + } + } + #[cfg(feature = "test-helpers")] pub fn multiaddrs(&self) -> Vec { let local_peer = self.local_peer_id; @@ -644,9 +653,7 @@ impl FuelP2PService { fn handle_identify_event(&mut self, event: identify::Event) -> Option { match event { identify::Event::Received { peer_id, info } => { - if self.metrics { - p2p_metrics().unique_peers.inc(); - } + self.update_metrics(increment_unique_peers); let mut addresses = info.listen_addrs; let agent_version = info.agent_version; diff --git a/crates/services/p2p/src/service.rs b/crates/services/p2p/src/service.rs index f0b2be9f437..bf875289743 100644 --- a/crates/services/p2p/src/service.rs +++ b/crates/services/p2p/src/service.rs @@ -26,6 +26,7 @@ use crate::{ }, }; use anyhow::anyhow; +use fuel_core_metrics::p2p_metrics::set_blocks_requested; use fuel_core_services::{ stream::BoxStream, RunnableService, @@ -196,9 +197,20 @@ pub trait TaskP2PService: Send { ) -> anyhow::Result<()>; fn update_block_height(&mut self, height: BlockHeight) -> anyhow::Result<()>; + + fn update_metrics(&self, update_fn: T) + where + T: FnOnce(); } impl TaskP2PService for FuelP2PService { + fn update_metrics(&self, update_fn: T) + where + T: FnOnce(), + { + FuelP2PService::update_metrics(self, update_fn) + } + fn get_all_peer_info(&self) -> Vec<(&PeerId, &PeerInfo)> { self.peer_manager().get_all_peers().collect() } @@ -427,6 +439,13 @@ where V: AtomicView + 'static, V::LatestView: P2pDb, { + fn update_metrics(&self, update_fn: T) + where + T: FnOnce(), + { + self.p2p_service.update_metrics(update_fn) + } + fn process_request( &mut self, request_message: RequestMessage, @@ -464,8 +483,11 @@ where // If there are other types of data we send over p2p req/res protocol, then this needs // to be generalized let max_len = self.max_headers_per_request; + let range_len = range.len(); + + self.update_metrics(|| set_blocks_requested(range_len)); - if range.len() > max_len { + if range_len > max_len { tracing::error!( requested_length = range.len(), max_len, @@ -1031,6 +1053,13 @@ pub mod tests { } impl TaskP2PService for FakeP2PService { + fn update_metrics(&self, _: T) + where + T: FnOnce(), + { + unimplemented!() + } + fn get_all_peer_info(&self) -> Vec<(&PeerId, &PeerInfo)> { self.peer_info.iter().map(|tup| (&tup.0, &tup.1)).collect() }