Skip to content

Commit

Permalink
fix: don't create libp2p metrics twice [fixes NET-348] (#1545)
Browse files Browse the repository at this point in the history
* remove doubling libp2p metrics gathering
  • Loading branch information
kmd-fl committed Mar 31, 2023
1 parent 4592c1a commit d023865
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
7 changes: 4 additions & 3 deletions crates/kademlia/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use std::error::Error;
use std::ops::Mul;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::{
cmp::min,
Expand Down Expand Up @@ -126,11 +127,11 @@ pub struct Kademlia {
waker: Option<Waker>,
// Timer to track timed out requests, and return errors ASAP
timer: Delay,
metrics: Option<Metrics>,
metrics: Option<Arc<Metrics>>,
}

impl Kademlia {
pub fn new(config: KademliaConfig, metrics: Option<Metrics>) -> (Self, KademliaApi) {
pub fn new(config: KademliaConfig, metrics: Option<Arc<Metrics>>) -> (Self, KademliaApi) {
let timer = Delay::new(config.query_timeout);

let store = MemoryStore::new(config.peer_id);
Expand Down Expand Up @@ -550,7 +551,7 @@ impl Kademlia {
}

fn inject_kad_event(&mut self, event: KademliaEvent) {
if let Some(metrics) = &self.metrics {
if let Some(metrics) = self.metrics.as_ref() {
metrics.record(&event);
}

Expand Down
5 changes: 3 additions & 2 deletions crates/server-config/src/network_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use libp2p::swarm::ConnectionLimits;
use libp2p::{core::Multiaddr, identity::Keypair, PeerId};
use libp2p_metrics::Metrics;
use std::sync::Arc;

use config_utils::to_peer_id;
use particle_protocol::ProtocolConfig;
Expand All @@ -30,7 +31,7 @@ pub struct NetworkConfig {
pub node_version: &'static str,
pub bootstrap_nodes: Vec<Multiaddr>,
pub bootstrap: BootstrapConfig,
pub libp2p_metrics: Option<Metrics>,
pub libp2p_metrics: Option<Arc<Metrics>>,
pub protocol_config: ProtocolConfig,
pub kademlia_config: KademliaConfig,
pub particle_queue_buffer: usize,
Expand All @@ -43,7 +44,7 @@ pub struct NetworkConfig {

impl NetworkConfig {
pub fn new(
libp2p_metrics: Option<Metrics>,
libp2p_metrics: Option<Arc<Metrics>>,
connectivity_metrics: Option<ConnectivityMetrics>,
connection_pool_metrics: Option<ConnectionPoolMetrics>,
key_pair: Keypair,
Expand Down
17 changes: 10 additions & 7 deletions particle-node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub struct Node<RT: AquaRuntime> {
sorcerer: Sorcerer,

registry: Option<Registry>,
libp2p_metrics: Option<Arc<Metrics>>,
services_metrics_backend: ServicesMetricsBackend,

metrics_listen_addr: SocketAddr,
Expand Down Expand Up @@ -127,7 +128,7 @@ impl<RT: AquaRuntime> Node<RT> {
} else {
None
};
let libp2p_metrics = metrics_registry.as_mut().map(Metrics::new);
let libp2p_metrics = metrics_registry.as_mut().map(|r| Arc::new(Metrics::new(r)));
let connectivity_metrics = metrics_registry.as_mut().map(ConnectivityMetrics::new);
let connection_pool_metrics = metrics_registry.as_mut().map(ConnectionPoolMetrics::new);
let plumber_metrics = metrics_registry.as_mut().map(ParticleExecutorMetrics::new);
Expand All @@ -148,7 +149,7 @@ impl<RT: AquaRuntime> Node<RT> {
.with_max_established(config.node_config.transport_config.max_established);

let network_config = NetworkConfig::new(
libp2p_metrics,
libp2p_metrics.clone(),
connectivity_metrics,
connection_pool_metrics,
key_pair,
Expand Down Expand Up @@ -283,6 +284,7 @@ impl<RT: AquaRuntime> Node<RT> {
spell_events_receiver,
sorcerer,
metrics_registry,
libp2p_metrics,
services_metrics_backend,
config.metrics_listen_addr(),
builtins_peer_id,
Expand Down Expand Up @@ -356,6 +358,7 @@ impl<RT: AquaRuntime> Node<RT> {
spell_events_receiver: mpsc::UnboundedReceiver<TriggerEvent>,
sorcerer: Sorcerer,
registry: Option<Registry>,
libp2p_metrics: Option<Arc<Metrics>>,
services_metrics_backend: ServicesMetricsBackend,
metrics_listen_addr: SocketAddr,
builtins_management_peer_id: PeerId,
Expand All @@ -377,6 +380,7 @@ impl<RT: AquaRuntime> Node<RT> {
sorcerer,

registry,
libp2p_metrics,
services_metrics_backend,
metrics_listen_addr,

Expand Down Expand Up @@ -411,15 +415,14 @@ impl<RT: AquaRuntime> Node<RT> {
let task_name = peer_id
.map(|x| format!("node-{x}"))
.unwrap_or("node".to_owned());
let libp2p_metrics = self.libp2p_metrics;

task::Builder::new().name(&task_name.clone()).spawn(async move {
let (mut metrics_fut, libp2p_metrics) = if let Some(mut registry) = registry {
let libp2p_metrics = Metrics::new(&mut registry);
let mut metrics_fut= if let Some(registry) = registry {
log::info!("metrics_listen_addr {}", metrics_listen_addr);
let fut = start_metrics_endpoint(registry, metrics_listen_addr).boxed();
(fut, Some(libp2p_metrics))
start_metrics_endpoint(registry, metrics_listen_addr).boxed()
} else {
(futures::future::pending().boxed(), None)
futures::future::pending().boxed()
};

let services_metrics_backend = services_metrics_backend.start();
Expand Down

0 comments on commit d023865

Please sign in to comment.