Skip to content

Commit

Permalink
Add extra gossipsub metrics
Browse files Browse the repository at this point in the history
- update `eth2_libp2p` metrics from within internal ticker
  • Loading branch information
Tumas committed Dec 9, 2024
1 parent 273b9e8 commit ca00548
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 19 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion eth2_libp2p
1 change: 0 additions & 1 deletion metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ chrono = { workspace = true }
derive_more = { workspace = true }
directories = { workspace = true }
eth1_api = { workspace = true }
eth2_libp2p = { workspace = true }
fork_choice_control = { workspace = true }
futures = { workspace = true }
grandine_version = { workspace = true }
Expand Down
14 changes: 0 additions & 14 deletions metrics/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use axum::{
};
use directories::Directories;
use eth1_api::ApiController;
use eth2_libp2p::NetworkGlobals;
use fork_choice_control::Wait;
use futures::channel::mpsc::UnboundedSender;
use helper_functions::misc;
Expand Down Expand Up @@ -58,7 +57,6 @@ pub struct MetricsState<P: Preset, W: Wait> {
pub libp2p_registry: Option<Arc<Registry>>,
pub metrics: Arc<Metrics>,
pub metrics_to_metrics_tx: Option<UnboundedSender<MetricsToMetrics>>, // TODO: is still relevant, update naming if so
pub network_globals: Arc<NetworkGlobals>,
}

impl<P: Preset, W: Wait> FromRef<MetricsState<P, W>> for ApiController<P, W> {
Expand Down Expand Up @@ -91,12 +89,6 @@ impl<P: Preset, W: Wait> FromRef<MetricsState<P, W>> for Option<UnboundedSender<
}
}

impl<P: Preset, W: Wait> FromRef<MetricsState<P, W>> for Arc<NetworkGlobals> {
fn from_ref(state: &MetricsState<P, W>) -> Self {
state.network_globals.clone_arc()
}
}

#[derive(Debug, Error)]
pub enum Error {
#[error("internal error")]
Expand Down Expand Up @@ -127,7 +119,6 @@ pub async fn run_metrics_server<P: Preset, W: Wait>(
libp2p_registry: Option<Registry>,
metrics: Arc<Metrics>,
metrics_to_metrics_tx: Option<UnboundedSender<MetricsToMetrics>>,
network_globals: Arc<NetworkGlobals>,
) -> Result<()> {
let addr = SocketAddr::from(&config);

Expand All @@ -140,7 +131,6 @@ pub async fn run_metrics_server<P: Preset, W: Wait>(
libp2p_registry: libp2p_registry.map(Arc::new),
metrics,
metrics_to_metrics_tx,
network_globals,
};

let router = Router::new()
Expand Down Expand Up @@ -171,15 +161,11 @@ pub async fn prometheus_metrics<P: Preset, W: Wait>(
State(libp2p_registry): State<Option<Arc<Registry>>>,
State(metrics): State<Arc<Metrics>>,
State(metrics_to_metrics_tx): State<Option<UnboundedSender<MetricsToMetrics>>>,
State(network_globals): State<Arc<NetworkGlobals>>,
) -> Result<String, Error> {
let mut buffer = String::new();

metrics.set_live();

eth2_libp2p::metrics::scrape_discovery_metrics();
eth2_libp2p::metrics::scrape_sync_metrics(&network_globals);

// gossipsub metrics
if let Some(registry) = libp2p_registry {
prometheus_client::encoding::text::encode(&mut buffer, &registry)
Expand Down
17 changes: 16 additions & 1 deletion p2p/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ use crate::{
};

const GOSSIPSUB_PARAMETER_UPDATE_INTERVAL: Duration = Duration::from_secs(60);
const NETWORK_METRICS_UPDATE_INTERVAL: Duration = Duration::from_secs(1);

// > Clients MAY limit the number of blocks and sidecars in the response.
const MAX_FOR_DOS_PREVENTION: u64 = 64;
Expand Down Expand Up @@ -1897,8 +1898,22 @@ fn run_network_service<P: Preset>(
service_to_network_tx: UnboundedSender<ServiceOutboundMessage<P>>,
) {
tokio::spawn(async move {
let mut network_metrics_update_interval =
IntervalStream::new(tokio::time::interval(NETWORK_METRICS_UPDATE_INTERVAL)).fuse();

let metrics_enabled = service.network_globals().network_config.metrics_enabled;

loop {
select! {
tokio::select! {
_ = network_metrics_update_interval.select_next_some(), if metrics_enabled => {
eth2_libp2p::metrics::update_discovery_metrics();
eth2_libp2p::metrics::update_sync_metrics(service.network_globals());
eth2_libp2p::metrics::update_gossipsub_extended_metrics(
service.gossipsub(),
service.network_globals(),
);
},

network_event = service.next_event().fuse() => {
ServiceOutboundMessage::NetworkEvent(network_event).send(&service_to_network_tx);
}
Expand Down
1 change: 0 additions & 1 deletion runtime/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,6 @@ pub async fn run_after_genesis<P: Preset>(
.clone()
.expect("Metrics registry must be present for metrics server"),
metrics_to_metrics_tx,
network.network_globals().clone_arc(),
)),
None => Either::Right(core::future::pending()),
};
Expand Down

0 comments on commit ca00548

Please sign in to comment.