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.
This commit is contained in:
Divma 2021-11-03 00:06:03 +00:00
parent c4ad0e3fb3
commit 7502970a7d
4 changed files with 16 additions and 2 deletions

View File

@ -114,6 +114,9 @@ pub struct Config {
/// List of extra topics to initially subscribe to as strings. /// List of extra topics to initially subscribe to as strings.
pub topics: Vec<GossipKind>, pub topics: Vec<GossipKind>,
/// Whether metrics are enabled.
pub metrics_enabled: bool,
} }
impl Default for Config { impl Default for Config {
@ -188,6 +191,7 @@ impl Default for Config {
import_all_attestations: false, import_all_attestations: false,
shutdown_after_sync: false, shutdown_after_sync: false,
topics: Vec::new(), topics: Vec::new(),
metrics_enabled: false,
} }
} }
} }

View File

@ -137,6 +137,8 @@ pub struct NetworkService<T: BeaconChainTypes> {
subscribe_all_subnets: bool, subscribe_all_subnets: bool,
/// Shutdown beacon node after sync is complete. /// Shutdown beacon node after sync is complete.
shutdown_after_sync: bool, shutdown_after_sync: bool,
/// Whether metrics are enabled or not.
metrics_enabled: bool,
/// A timer for updating various network metrics. /// A timer for updating various network metrics.
metrics_update: tokio::time::Interval, metrics_update: tokio::time::Interval,
/// gossipsub_parameter_update timer /// gossipsub_parameter_update timer
@ -263,6 +265,7 @@ impl<T: BeaconChainTypes> NetworkService<T> {
next_unsubscribe, next_unsubscribe,
subscribe_all_subnets: config.subscribe_all_subnets, subscribe_all_subnets: config.subscribe_all_subnets,
shutdown_after_sync: config.shutdown_after_sync, shutdown_after_sync: config.shutdown_after_sync,
metrics_enabled: config.metrics_enabled,
metrics_update, metrics_update,
gossipsub_parameter_update, gossipsub_parameter_update,
fork_context, fork_context,
@ -325,7 +328,7 @@ fn spawn_service<T: BeaconChainTypes>(
loop { loop {
// build the futures to check simultaneously // build the futures to check simultaneously
tokio::select! { tokio::select! {
_ = service.metrics_update.tick() => { _ = service.metrics_update.tick(), if service.metrics_enabled => {
// update various network metrics // update various network metrics
metric_update_counter +=1; metric_update_counter +=1;
if metric_update_counter % T::EthSpec::default_spec().seconds_per_slot == 0 { if metric_update_counter % T::EthSpec::default_spec().seconds_per_slot == 0 {

View File

@ -705,6 +705,10 @@ pub fn set_network_config(
config.private = true; config.private = true;
} }
if cli_args.is_present("metrics") {
config.metrics_enabled = true;
}
Ok(()) Ok(())
} }

View File

@ -619,7 +619,10 @@ fn metrics_flag() {
CommandLineTest::new() CommandLineTest::new()
.flag("metrics", None) .flag("metrics", None)
.run() .run()
.with_config(|config| assert!(config.http_metrics.enabled)); .with_config(|config| {
assert!(config.http_metrics.enabled);
assert!(config.network.metrics_enabled);
});
} }
#[test] #[test]
fn metrics_address_flag() { fn metrics_address_flag() {