Skip to content

Commit

Permalink
feat(network): feature gate libp2p metrics
Browse files Browse the repository at this point in the history
- this is enabled by default for a node though
  • Loading branch information
RolandSherwin committed Sep 4, 2023
1 parent 618f382 commit 757c0ce
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
8 changes: 4 additions & 4 deletions sn_networking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ version = "0.5.9"
default=[]
local-discovery=["libp2p/mdns"]
quic=["libp2p-quic"]
metrics=["libp2p-metrics", "prometheus-client", "hyper"]

[dependencies]
async-trait = "0.1"
bytes = { version = "1.0.1", features = ["serde"] }
futures = "~0.3.13"
hyper = { version = "0.14", features = ["server", "tcp", "http1"], optional = true}
itertools = "~0.10.1"
custom_debug = "~0.5.0"
libp2p = { version="0.52", features = ["tokio", "dns", "kad", "macros", "request-response", "cbor","identify", "autonat", "noise", "tcp", "yamux"] }
libp2p-metrics = { version = "0.13.1", features = ["identify", "kad", ], optional = true }
libp2p-quic = { version = "0.8.0-alpha", features = ["tokio"], optional = true }
prometheus-client = { version = "0.21.2", optional = true }
rand = { version = "~0.8.5", features = ["small_rng"] }
rmp-serde = "1.1.1"
serde = { version = "1.0.133", features = [ "derive", "rc" ]}
Expand All @@ -34,10 +38,6 @@ tokio = { version = "1.32.0", features = ["io-util", "macros", "parking_lot", "r
tracing = { version = "~0.1.26" }
xor_name = "5.0.0"

libp2p-metrics = { version = "0.13.1", features = ["identify", "kad", ] }
prometheus-client = "0.21.2"
hyper = { version = "0.14", features = ["server", "tcp", "http1"] }

[dev-dependencies]
bls = { package = "blsttc", version = "8.0.1" }
quickcheck = "1.0.3"
Expand Down
17 changes: 13 additions & 4 deletions sn_networking/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

#[cfg(feature = "metrics")]
use crate::metrics_service::metrics_server;
use crate::{
circular_vec::CircularVec,
cmd::SwarmCmd,
error::{Error, Result},
event::NetworkEvent,
event::{GetRecordResultMap, NodeEvent},
metrics_service::metrics_server,
multiaddr_pop_p2p,
record_store::{ClientRecordStore, NodeRecordStore, NodeRecordStoreConfig},
record_store_api::UnifiedRecordStore,
Expand All @@ -37,9 +38,11 @@ use libp2p::{
},
Multiaddr, PeerId, Transport,
};
#[cfg(feature = "metrics")]
use libp2p_metrics::Metrics;
#[cfg(feature = "quic")]
use libp2p_quic as quic;
#[cfg(feature = "metrics")]
use prometheus_client::registry::Registry;
use sn_protocol::messages::{Request, Response};
use std::{
Expand Down Expand Up @@ -97,6 +100,7 @@ pub struct SwarmDriver {
/// The peers that are closer to our PeerId. Includes self.
pub(crate) close_group: Vec<PeerId>,
pub(crate) replication_fetcher: ReplicationFetcher,
#[cfg(feature = "metrics")]
pub(crate) network_metrics: Metrics,

cmd_receiver: mpsc::Receiver<SwarmCmd>,
Expand Down Expand Up @@ -355,9 +359,13 @@ impl SwarmDriver {
};
let autonat = Toggle::from(autonat);

let mut metric_registry = Registry::default();
let metrics = Metrics::new(&mut metric_registry);
metrics_server(metric_registry);
#[cfg(feature = "metrics")]
let metrics = {
let mut metric_registry = Registry::default();
let metrics = Metrics::new(&mut metric_registry);
metrics_server(metric_registry);
metrics
};

let behaviour = NodeBehaviour {
request_response,
Expand All @@ -378,6 +386,7 @@ impl SwarmDriver {
bootstrap_ongoing: false,
close_group: Default::default(),
replication_fetcher: Default::default(),
#[cfg(feature = "metrics")]
network_metrics: metrics,
cmd_receiver: swarm_cmd_receiver,
event_sender: network_event_sender,
Expand Down
4 changes: 4 additions & 0 deletions sn_networking/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use libp2p::{
swarm::{dial_opts::DialOpts, SwarmEvent},
Multiaddr, PeerId,
};
#[cfg(feature = "metrics")]
use libp2p_metrics::Recorder;
use sn_protocol::{
messages::{Request, Response},
Expand Down Expand Up @@ -176,6 +177,7 @@ impl SwarmDriver {
) -> Result<()> {
// This does not record all the events. `SwarmEvent::Behaviour(_)` are skipped. Hence `.record()` has to be
// called individually on each behaviour.
#[cfg(feature = "metrics")]
self.network_metrics.record(&event);
match event {
SwarmEvent::Behaviour(NodeEvent::MsgReceived(event)) => {
Expand All @@ -187,6 +189,7 @@ impl SwarmDriver {
self.handle_kad_event(kad_event)?;
}
SwarmEvent::Behaviour(NodeEvent::Identify(iden)) => {
#[cfg(feature = "metrics")]
self.network_metrics.record(&(*iden));
match *iden {
libp2p::identify::Event::Received { peer_id, info } => {
Expand Down Expand Up @@ -482,6 +485,7 @@ impl SwarmDriver {

#[allow(clippy::result_large_err)]
fn handle_kad_event(&mut self, kad_event: KademliaEvent) -> Result<()> {
#[cfg(feature = "metrics")]
self.network_metrics.record(&kad_event);
match kad_event {
ref event @ KademliaEvent::OutboundQueryProgressed {
Expand Down
1 change: 1 addition & 0 deletions sn_networking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod cmd;
mod driver;
mod error;
mod event;
#[cfg(feature = "metrics")]
mod metrics_service;
mod record_store;
mod record_store_api;
Expand Down
2 changes: 1 addition & 1 deletion sn_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ path = "src/bin/faucet/main.rs"
default=["metrics"]
local-discovery=["sn_networking/local-discovery"]
otlp = ["sn_logging/otlp"]
metrics = ["sn_logging/process-metrics"]
metrics = ["sn_logging/process-metrics", "sn_networking/metrics"]
quic=["sn_networking/quic"]

[dependencies]
Expand Down

0 comments on commit 757c0ce

Please sign in to comment.