From 7502970a7de349a57266914a5081a4ea727a4451 Mon Sep 17 00:00:00 2001 From: Divma Date: Wed, 3 Nov 2021 00:06:03 +0000 Subject: [PATCH] Do not compute metrics in the network service if the cli flag is not set (#2765) ## Issue Addressed The computation of metrics in the network service can be expensive. This disables the computation unless the cli flag `metrics` is set. ## Additional Info Metrics in other parts of the network are still updated, since most are simple metrics and checking if metrics are enabled each time each metric is updated doesn't seem like a gain. --- beacon_node/lighthouse_network/src/config.rs | 4 ++++ beacon_node/network/src/service.rs | 5 ++++- beacon_node/src/config.rs | 4 ++++ lighthouse/tests/beacon_node.rs | 5 ++++- 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/beacon_node/lighthouse_network/src/config.rs b/beacon_node/lighthouse_network/src/config.rs index 4c6524084b..e18fd00aeb 100644 --- a/beacon_node/lighthouse_network/src/config.rs +++ b/beacon_node/lighthouse_network/src/config.rs @@ -114,6 +114,9 @@ pub struct Config { /// List of extra topics to initially subscribe to as strings. pub topics: Vec, + + /// Whether metrics are enabled. + pub metrics_enabled: bool, } impl Default for Config { @@ -188,6 +191,7 @@ impl Default for Config { import_all_attestations: false, shutdown_after_sync: false, topics: Vec::new(), + metrics_enabled: false, } } } diff --git a/beacon_node/network/src/service.rs b/beacon_node/network/src/service.rs index 6eeb17123b..ce8aca4725 100644 --- a/beacon_node/network/src/service.rs +++ b/beacon_node/network/src/service.rs @@ -137,6 +137,8 @@ pub struct NetworkService { subscribe_all_subnets: bool, /// Shutdown beacon node after sync is complete. shutdown_after_sync: bool, + /// Whether metrics are enabled or not. + metrics_enabled: bool, /// A timer for updating various network metrics. metrics_update: tokio::time::Interval, /// gossipsub_parameter_update timer @@ -263,6 +265,7 @@ impl NetworkService { next_unsubscribe, subscribe_all_subnets: config.subscribe_all_subnets, shutdown_after_sync: config.shutdown_after_sync, + metrics_enabled: config.metrics_enabled, metrics_update, gossipsub_parameter_update, fork_context, @@ -325,7 +328,7 @@ fn spawn_service( loop { // build the futures to check simultaneously tokio::select! { - _ = service.metrics_update.tick() => { + _ = service.metrics_update.tick(), if service.metrics_enabled => { // update various network metrics metric_update_counter +=1; if metric_update_counter % T::EthSpec::default_spec().seconds_per_slot == 0 { diff --git a/beacon_node/src/config.rs b/beacon_node/src/config.rs index c086d71ab0..12c7d59b5d 100644 --- a/beacon_node/src/config.rs +++ b/beacon_node/src/config.rs @@ -705,6 +705,10 @@ pub fn set_network_config( config.private = true; } + if cli_args.is_present("metrics") { + config.metrics_enabled = true; + } + Ok(()) } diff --git a/lighthouse/tests/beacon_node.rs b/lighthouse/tests/beacon_node.rs index 2456c8d505..a7ccdbebd3 100644 --- a/lighthouse/tests/beacon_node.rs +++ b/lighthouse/tests/beacon_node.rs @@ -619,7 +619,10 @@ fn metrics_flag() { CommandLineTest::new() .flag("metrics", None) .run() - .with_config(|config| assert!(config.http_metrics.enabled)); + .with_config(|config| { + assert!(config.http_metrics.enabled); + assert!(config.network.metrics_enabled); + }); } #[test] fn metrics_address_flag() {