From 090dff3450cf4336b25aae297332069548f14618 Mon Sep 17 00:00:00 2001 From: Nick Date: Fri, 22 Dec 2023 16:11:12 +0300 Subject: [PATCH] feat(metrics): add libp2p bandwidth metrics (#1973) * add tests * add tests * review fixes * review fixes * feat(metrics): add libp2p bandwidth metrics --- nox/Cargo.toml | 2 +- nox/src/node.rs | 24 +++++++++++++++++------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/nox/Cargo.toml b/nox/Cargo.toml index 5398de1a12..c81ca4b86e 100644 --- a/nox/Cargo.toml +++ b/nox/Cargo.toml @@ -36,7 +36,7 @@ fluence-keypair = { workspace = true } avm-server = { workspace = true } air-interpreter-wasm = { workspace = true } -libp2p = { workspace = true } +libp2p = { workspace = true, features = ["metrics"] } libp2p-metrics = { workspace = true } libp2p-swarm = { workspace = true } libp2p-connection-limits = { workspace = true } diff --git a/nox/src/node.rs b/nox/src/node.rs index afd3637dd9..0106f0542b 100644 --- a/nox/src/node.rs +++ b/nox/src/node.rs @@ -193,6 +193,7 @@ impl Node { transport, config.external_addresses(), health_registry.as_mut(), + metrics_registry.as_mut(), )?; let (services_metrics_backend, services_metrics) = @@ -354,6 +355,7 @@ impl Node { transport: Boxed<(PeerId, StreamMuxerBox)>, external_addresses: Vec, health_registry: Option<&mut HealthCheckRegistry>, + metrics_registry: Option<&mut Registry>, ) -> eyre::Result<( Swarm, Connectivity, @@ -364,13 +366,21 @@ impl Node { let (behaviour, connectivity, particle_stream) = FluenceNetworkBehaviour::new(network_config, health_registry); - let mut swarm = SwarmBuilder::with_existing_identity(key_pair) - .with_tokio() - .with_other_transport(|_| transport)? - .with_behaviour(|_| behaviour)? - .with_swarm_config(|cfg| cfg.with_idle_connection_timeout(connection_idle_timeout)) - .build(); - + let mut swarm = match metrics_registry { + None => SwarmBuilder::with_existing_identity(key_pair) + .with_tokio() + .with_other_transport(|_| transport)? + .with_behaviour(|_| behaviour)? + .with_swarm_config(|cfg| cfg.with_idle_connection_timeout(connection_idle_timeout)) + .build(), + Some(registry) => SwarmBuilder::with_existing_identity(key_pair) + .with_tokio() + .with_other_transport(|_| transport)? + .with_bandwidth_metrics(registry) + .with_behaviour(|_| behaviour)? + .with_swarm_config(|cfg| cfg.with_idle_connection_timeout(connection_idle_timeout)) + .build(), + }; // Add external addresses to Swarm external_addresses.iter().cloned().for_each(|addr| { Swarm::add_external_address(&mut swarm, addr);