From d7c0f89acd0171a0d7132fd43f6954ba1ab75e3a Mon Sep 17 00:00:00 2001 From: John Turpish Date: Tue, 4 Oct 2022 15:27:06 -0400 Subject: [PATCH 01/19] Getting started --- Cargo.toml | 3 +- core/Cargo.toml | 2 +- misc/metrics/Cargo.toml | 6 ++-- misc/metrics/examples/metrics/main.rs | 48 ++++++++++++++++++++++++--- misc/metrics/src/identify.rs | 14 ++++++++ misc/metrics/src/swarm.rs | 12 ++++--- 6 files changed, 70 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 38afecb9372..ffa0e8c1173 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -109,11 +109,12 @@ libp2p-swarm-derive = { version = "0.30.1", path = "swarm-derive" } libp2p-uds = { version = "0.36.0", path = "transports/uds", optional = true } libp2p-wasm-ext = { version = "0.37.0", path = "transports/wasm-ext", optional = true } libp2p-yamux = { version = "0.41.0", path = "muxers/yamux", optional = true } -multiaddr = { version = "0.14.0" } +multiaddr = { path = "../rust-multiaddr" } parking_lot = "0.12.0" pin-project = "1.0.0" smallvec = "1.6.1" + [target.'cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))'.dependencies] libp2p-deflate = { version = "0.37.0", path = "transports/deflate", optional = true } libp2p-dns = { version = "0.37.0", path = "transports/dns", optional = true } diff --git a/core/Cargo.toml b/core/Cargo.toml index fe15116e816..ea04b6698e5 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -22,7 +22,7 @@ instant = "0.1.11" lazy_static = "1.2" libsecp256k1 = { version = "0.7.0", optional = true } log = "0.4" -multiaddr = { version = "0.14.0" } +multiaddr = { path = "../../rust-multiaddr" } multihash = { version = "0.16", default-features = false, features = ["std", "multihash-impl", "identity", "sha2"] } multistream-select = { version = "0.12", path = "../misc/multistream-select" } p256 = { version = "0.11.1", default-features = false, features = ["ecdsa"], optional = true } diff --git a/misc/metrics/Cargo.toml b/misc/metrics/Cargo.toml index 643d96e67ab..52c3a0911f8 100644 --- a/misc/metrics/Cargo.toml +++ b/misc/metrics/Cargo.toml @@ -23,9 +23,9 @@ libp2p-core = { version = "0.37.0", path = "../../core" } libp2p-dcutr = { version = "0.7.0", path = "../../protocols/dcutr", optional = true } libp2p-identify = { version = "0.40.1", path = "../../protocols/identify", optional = true } libp2p-kad = { version = "0.41.0", path = "../../protocols/kad", optional = true } -libp2p-ping = { version = "0.40.0", path = "../../protocols/ping", optional = true } -libp2p-relay = { version = "0.13.0", path = "../../protocols/relay", optional = true } -libp2p-swarm = { version = "0.40.0", path = "../../swarm" } +libp2p-ping = { path = "../../protocols/ping", optional = true } +libp2p-relay = { path = "../../protocols/relay", optional = true } +libp2p-swarm = { path = "../../swarm" } prometheus-client = "0.18.0" [target.'cfg(not(target_os = "unknown"))'.dependencies] diff --git a/misc/metrics/examples/metrics/main.rs b/misc/metrics/examples/metrics/main.rs index 51b0df8c2a4..f162cb6c5e1 100644 --- a/misc/metrics/examples/metrics/main.rs +++ b/misc/metrics/examples/metrics/main.rs @@ -52,9 +52,8 @@ use futures::executor::block_on; use futures::stream::StreamExt; use libp2p::core::Multiaddr; use libp2p::metrics::{Metrics, Recorder}; -use libp2p::ping; use libp2p::swarm::SwarmEvent; -use libp2p::{identity, PeerId, Swarm}; +use libp2p::{identify, identity, NetworkBehaviour, PeerId, ping, Swarm}; use prometheus_client::registry::Registry; use std::error::Error; use std::thread; @@ -71,8 +70,9 @@ fn main() -> Result<(), Box> { info!("Local peer id: {:?}", local_peer_id); let mut swarm = Swarm::new( - block_on(libp2p::development_transport(local_key))?, - ping::Behaviour::new(ping::Config::new().with_keep_alive(true)), + block_on(libp2p::development_transport(local_key.clone()))?, + // ping::Behaviour::new(ping::Config::new().with_keep_alive(true)), + MyBehaviour::new(local_key), local_peer_id, ); @@ -91,10 +91,14 @@ fn main() -> Result<(), Box> { block_on(async { loop { match swarm.select_next_some().await { - SwarmEvent::Behaviour(ping_event) => { + SwarmEvent::Behaviour(Event::Ping(ping_event)) => { info!("{:?}", ping_event); metrics.record(&ping_event); } + SwarmEvent::Behaviour(Event::Identify(identify_event)) => { + info!("{:?}", identify_event); + metrics.record(&identify_event); + } swarm_event => { info!("{:?}", swarm_event); metrics.record(&swarm_event); @@ -104,3 +108,37 @@ fn main() -> Result<(), Box> { }); Ok(()) } + +#[derive(NetworkBehaviour)] +#[behaviour(out_event = "Event")] +struct MyBehaviour { + identify: identify::Identify, + ping: ping::Behaviour, +} + +#[derive(Debug)] +enum Event { + Identify(identify::IdentifyEvent), + Ping(ping::Event), +} +impl From for Event { + fn from(event: identify::IdentifyEvent) -> Self { + Self::Identify(event) + } +} +impl From for Event { + fn from(event: ping::Event) -> Self { + Self::Ping(event) + } +} +impl MyBehaviour{ + fn new(local_key: libp2p::identity::Keypair) -> Self { + Self{ + ping: ping::Behaviour::new(ping::Config::new().with_keep_alive(true)), + identify: identify::Identify::new( identify::IdentifyConfig::new( + "/ipfs/0.1.0".into(), + local_key.public(), + )), + } + } +} diff --git a/misc/metrics/src/identify.rs b/misc/metrics/src/identify.rs index 730528167a8..50599e2f455 100644 --- a/misc/metrics/src/identify.rs +++ b/misc/metrics/src/identify.rs @@ -27,6 +27,7 @@ use prometheus_client::registry::Registry; use std::collections::HashMap; use std::iter; use std::sync::{Arc, Mutex}; +use prometheus_client::metrics::family::Family; pub struct Metrics { protocols: Protocols, @@ -36,6 +37,7 @@ pub struct Metrics { received_info_listen_addrs: Histogram, received_info_protocols: Histogram, sent: Counter, + listen_addresses: Family, Counter>, } impl Metrics { @@ -100,6 +102,13 @@ impl Metrics { Box::new(sent.clone()), ); + let listen_addresses = Family::default(); + sub_registry.register( + "listen_addresses", + "Number of listen addresses for remote peer per protocol stack", + Box::new(listen_addresses.clone()) + ); + Self { protocols, error, @@ -108,6 +117,7 @@ impl Metrics { received_info_listen_addrs, received_info_protocols, sent, + listen_addresses } } } @@ -167,6 +177,10 @@ impl super::Recorder for Metrics { .observe(info.protocols.len() as f64); self.received_info_listen_addrs .observe(info.listen_addrs.len() as f64); + for listen_addr in &info.listen_addrs { + let key : Vec<&'static str> = listen_addr.protocol_stack().collect(); + self.listen_addresses.get_or_create(&key).inc(); + } } libp2p_identify::IdentifyEvent::Sent { .. } => { self.sent.inc(); diff --git a/misc/metrics/src/swarm.rs b/misc/metrics/src/swarm.rs index e9c5a0493ce..7247c8c62f5 100644 --- a/misc/metrics/src/swarm.rs +++ b/misc/metrics/src/swarm.rs @@ -24,7 +24,7 @@ use prometheus_client::metrics::family::Family; use prometheus_client::registry::Registry; pub struct Metrics { - connections_incoming: Counter, + connections_incoming: Family, Counter>, connections_incoming_error: Family, connections_established: Family, @@ -45,10 +45,10 @@ impl Metrics { pub fn new(registry: &mut Registry) -> Self { let sub_registry = registry.sub_registry_with_prefix("swarm"); - let connections_incoming = Counter::default(); + let connections_incoming = Family::default(); sub_registry.register( "connections_incoming", - "Number of incoming connections", + "Number of incoming connections per address stack", Box::new(connections_incoming.clone()), ); @@ -156,8 +156,9 @@ impl super::Recorder { - self.connections_incoming.inc(); + libp2p_swarm::SwarmEvent::IncomingConnection { send_back_addr, .. } => { + let labels : Vec<&'static str> = send_back_addr.protocol_stack().collect(); + self.connections_incoming.get_or_create(&labels).inc(); } libp2p_swarm::SwarmEvent::IncomingConnectionError { error, .. } => { self.connections_incoming_error @@ -246,6 +247,7 @@ impl super::Recorder Date: Fri, 7 Oct 2022 13:25:17 -0400 Subject: [PATCH 02/19] Post merge fixup and re-check. --- misc/metrics/Cargo.toml | 2 +- misc/metrics/examples/metrics/main.rs | 40 ++++++++------------------- 2 files changed, 12 insertions(+), 30 deletions(-) diff --git a/misc/metrics/Cargo.toml b/misc/metrics/Cargo.toml index 6ca62b9f003..13d1c954ce3 100644 --- a/misc/metrics/Cargo.toml +++ b/misc/metrics/Cargo.toml @@ -21,7 +21,7 @@ dcutr = ["libp2p-dcutr"] [dependencies] libp2p-core = { version = "0.37.0", path = "../../core" } libp2p-dcutr = { version = "0.7.0", path = "../../protocols/dcutr", optional = true } -libp2p-identify = { version = "0.40.0", path = "../../protocols/identify", optional = true } +libp2p-identify = { path = "../../protocols/identify", optional = true } libp2p-kad = { version = "0.41.0", path = "../../protocols/kad", optional = true } libp2p-ping = { path = "../../protocols/ping", optional = true } libp2p-relay = { path = "../../protocols/relay", optional = true } diff --git a/misc/metrics/examples/metrics/main.rs b/misc/metrics/examples/metrics/main.rs index ceed7d93751..7362d5c5ebc 100644 --- a/misc/metrics/examples/metrics/main.rs +++ b/misc/metrics/examples/metrics/main.rs @@ -48,12 +48,13 @@ //! You should see a long list of metrics printed to the terminal. Check the //! `libp2p_ping` metrics, they should be `>0`. +use env_logger::Env; use futures::executor::block_on; use futures::stream::StreamExt; use libp2p::core::Multiaddr; use libp2p::metrics::{Metrics, Recorder}; use libp2p::swarm::SwarmEvent; -use libp2p::{identity, ping, NetworkBehaviour, PeerId, Swarm}; +use libp2p::{identify, identity, ping, NetworkBehaviour, PeerId, Swarm}; use libp2p_swarm::keep_alive; use log::info; use prometheus_client::registry::Registry; @@ -71,7 +72,7 @@ fn main() -> Result<(), Box> { let mut swarm = Swarm::new( block_on(libp2p::development_transport(local_key.clone()))?, - MyBehaviour::new(local_key), + Behaviour::new(local_key), local_peer_id, ); @@ -90,11 +91,11 @@ fn main() -> Result<(), Box> { block_on(async { loop { match swarm.select_next_some().await { - SwarmEvent::Behaviour(Event::Ping(ping_event)) => { + SwarmEvent::Behaviour(BehaviourEvent::Ping(ping_event)) => { info!("{:?}", ping_event); metrics.record(&ping_event); } - SwarmEvent::Behaviour(Event::Identify(identify_event)) => { + SwarmEvent::Behaviour(BehaviourEvent::Identify(identify_event)) => { info!("{:?}", identify_event); metrics.record(&identify_event); } @@ -112,42 +113,23 @@ fn main() -> Result<(), Box> { /// /// For illustrative purposes, this includes the [`keep_alive::Behaviour`]) behaviour so the ping actually happen /// and can be observed via the metrics. -#[derive(NetworkBehaviour, Default)] -#[behaviour(out_event = "Event")] +#[derive(NetworkBehaviour)] +// #[behaviour(out_event = "Event")] struct Behaviour { - identify: identify::Identify, + identify: identify::Behaviour, keep_alive: keep_alive::Behaviour, ping: ping::Behaviour, } -#[derive(Debug)] -enum Event { - Identify(identify::IdentifyEvent), - Ping(ping::Event), -} -impl From for Event { - fn from(event: identify::IdentifyEvent) -> Self { - Self::Identify(event) - } -} -impl From for Event { - fn from(event: ping::Event) -> Self { - Self::Ping(event) - } -} -impl From for Event { - fn from(event: keep_alive::Event) -> Self { - Self::KeepAlive(event) - } -} impl Behaviour{ fn new(local_key: libp2p::identity::Keypair) -> Self { Self{ - ping: ping::Behaviour::new(ping::Config::new().with_keep_alive(true)), - identify: identify::Identify::new( identify::IdentifyConfig::new( + ping: ping::Behaviour::default(), + identify: identify::Behaviour::new( identify::Config::new( "/ipfs/0.1.0".into(), local_key.public(), )), + keep_alive: keep_alive::Behaviour::default(), } } } From 14e906a8cd9394597769e2d27e409943069e5a2a Mon Sep 17 00:00:00 2001 From: John Turpish Date: Tue, 11 Oct 2022 17:27:55 -0400 Subject: [PATCH 03/19] Pulling protocol_stack functionality into this repo, by request. --- Cargo.toml | 3 +-- core/Cargo.toml | 2 +- misc/metrics/Cargo.toml | 8 ++++---- misc/metrics/src/identify.rs | 5 +++-- misc/metrics/src/lib.rs | 1 + misc/metrics/src/protocol_stack.rs | 13 +++++++++++++ misc/metrics/src/swarm.rs | 5 +++-- 7 files changed, 26 insertions(+), 11 deletions(-) create mode 100644 misc/metrics/src/protocol_stack.rs diff --git a/Cargo.toml b/Cargo.toml index 8bb91073573..417c4b8bb0a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -109,12 +109,11 @@ libp2p-swarm-derive = { version = "0.30.1", path = "swarm-derive" } libp2p-uds = { version = "0.36.0", path = "transports/uds", optional = true } libp2p-wasm-ext = { version = "0.37.0", path = "transports/wasm-ext", optional = true } libp2p-yamux = { version = "0.41.0", path = "muxers/yamux", optional = true } -multiaddr = { path = "../rust-multiaddr" } +multiaddr = { version = "0.14.0" } parking_lot = "0.12.0" pin-project = "1.0.0" smallvec = "1.6.1" - [target.'cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))'.dependencies] libp2p-deflate = { version = "0.37.0", path = "transports/deflate", optional = true } libp2p-dns = { version = "0.37.0", path = "transports/dns", optional = true } diff --git a/core/Cargo.toml b/core/Cargo.toml index 91c5900fc0a..c0881afe619 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -22,7 +22,7 @@ instant = "0.1.11" lazy_static = "1.2" libsecp256k1 = { version = "0.7.0", optional = true } log = "0.4" -multiaddr = { path = "../../rust-multiaddr" } +multiaddr = { version = "0.14.0" } multihash = { version = "0.16", default-features = false, features = ["std", "multihash-impl", "identity", "sha2"] } multistream-select = { version = "0.12", path = "../misc/multistream-select" } p256 = { version = "0.11.1", default-features = false, features = ["ecdsa"], optional = true } diff --git a/misc/metrics/Cargo.toml b/misc/metrics/Cargo.toml index 13d1c954ce3..91175a12383 100644 --- a/misc/metrics/Cargo.toml +++ b/misc/metrics/Cargo.toml @@ -21,11 +21,11 @@ dcutr = ["libp2p-dcutr"] [dependencies] libp2p-core = { version = "0.37.0", path = "../../core" } libp2p-dcutr = { version = "0.7.0", path = "../../protocols/dcutr", optional = true } -libp2p-identify = { path = "../../protocols/identify", optional = true } +libp2p-identify = { version = "0.40.0", path = "../../protocols/identify", optional = true } libp2p-kad = { version = "0.41.0", path = "../../protocols/kad", optional = true } -libp2p-ping = { path = "../../protocols/ping", optional = true } -libp2p-relay = { path = "../../protocols/relay", optional = true } -libp2p-swarm = { path = "../../swarm" } +libp2p-ping = { version = "0.40.0", path = "../../protocols/ping", optional = true } +libp2p-relay = { version = "0.13.0", path = "../../protocols/relay", optional = true } +libp2p-swarm = { version = "0.40.0", path = "../../swarm" } prometheus-client = "0.18.0" [target.'cfg(not(target_os = "unknown"))'.dependencies] diff --git a/misc/metrics/src/identify.rs b/misc/metrics/src/identify.rs index 0250dc02966..6d12bb40d1e 100644 --- a/misc/metrics/src/identify.rs +++ b/misc/metrics/src/identify.rs @@ -28,6 +28,7 @@ use std::collections::HashMap; use std::iter; use std::sync::{Arc, Mutex}; use prometheus_client::metrics::family::Family; +use super::protocol_stack::protocol_stack; pub struct Metrics { protocols: Protocols, @@ -37,7 +38,7 @@ pub struct Metrics { received_info_listen_addrs: Histogram, received_info_protocols: Histogram, sent: Counter, - listen_addresses: Family, Counter>, + listen_addresses: Family, Counter>, } impl Metrics { @@ -178,7 +179,7 @@ impl super::Recorder for Metrics { self.received_info_listen_addrs .observe(info.listen_addrs.len() as f64); for listen_addr in &info.listen_addrs { - let key : Vec<&'static str> = listen_addr.protocol_stack().collect(); + let key = protocol_stack(listen_addr); self.listen_addresses.get_or_create(&key).inc(); } } diff --git a/misc/metrics/src/lib.rs b/misc/metrics/src/lib.rs index bc9f45b8d76..af1f89dd6a4 100644 --- a/misc/metrics/src/lib.rs +++ b/misc/metrics/src/lib.rs @@ -39,6 +39,7 @@ mod ping; #[cfg(feature = "relay")] mod relay; mod swarm; +mod protocol_stack; use prometheus_client::registry::Registry; diff --git a/misc/metrics/src/protocol_stack.rs b/misc/metrics/src/protocol_stack.rs new file mode 100644 index 00000000000..182ae57b345 --- /dev/null +++ b/misc/metrics/src/protocol_stack.rs @@ -0,0 +1,13 @@ +use libp2p_core::multiaddr::{Multiaddr,Protocol}; + +//TODO: remove this file and replace with calls to Multiaddr::protocol_stack +// once that lands : https://github.com/multiformats/rust-multiaddr/pull/60 +// This does imply changing the keys from Vec to Vec<'static str> + +fn tag(proto: Protocol) -> Option { + format!("{}", proto).split("/").skip(1).next().map(str::to_owned) +} + +pub(crate) fn protocol_stack(ma: &Multiaddr) -> Vec { + ma.iter().filter_map(tag).collect() +} diff --git a/misc/metrics/src/swarm.rs b/misc/metrics/src/swarm.rs index 7247c8c62f5..912ca3bc331 100644 --- a/misc/metrics/src/swarm.rs +++ b/misc/metrics/src/swarm.rs @@ -22,9 +22,10 @@ use prometheus_client::encoding::text::Encode; use prometheus_client::metrics::counter::Counter; use prometheus_client::metrics::family::Family; use prometheus_client::registry::Registry; +use crate::protocol_stack::protocol_stack; pub struct Metrics { - connections_incoming: Family, Counter>, + connections_incoming: Family, Counter>, connections_incoming_error: Family, connections_established: Family, @@ -157,7 +158,7 @@ impl super::Recorder { - let labels : Vec<&'static str> = send_back_addr.protocol_stack().collect(); + let labels = protocol_stack(send_back_addr); self.connections_incoming.get_or_create(&labels).inc(); } libp2p_swarm::SwarmEvent::IncomingConnectionError { error, .. } => { From 808c439375c11e62d4fec9d027a970b9dbd774ab Mon Sep 17 00:00:00 2001 From: John Turpish Date: Thu, 13 Oct 2022 11:54:48 -0400 Subject: [PATCH 04/19] A couple of suggestions from PR --- misc/metrics/Cargo.toml | 5 ++-- misc/metrics/examples/metrics/main.rs | 1 - misc/metrics/src/identify.rs | 4 +-- misc/metrics/src/protocol_stack.rs | 43 +++++++++++++++++++++++---- misc/metrics/src/swarm.rs | 3 +- 5 files changed, 43 insertions(+), 13 deletions(-) diff --git a/misc/metrics/Cargo.toml b/misc/metrics/Cargo.toml index 91175a12383..50234845217 100644 --- a/misc/metrics/Cargo.toml +++ b/misc/metrics/Cargo.toml @@ -23,9 +23,10 @@ libp2p-core = { version = "0.37.0", path = "../../core" } libp2p-dcutr = { version = "0.7.0", path = "../../protocols/dcutr", optional = true } libp2p-identify = { version = "0.40.0", path = "../../protocols/identify", optional = true } libp2p-kad = { version = "0.41.0", path = "../../protocols/kad", optional = true } -libp2p-ping = { version = "0.40.0", path = "../../protocols/ping", optional = true } -libp2p-relay = { version = "0.13.0", path = "../../protocols/relay", optional = true } +libp2p-ping = { version = "0.40.0", path = "../../protocols/ping", optional = true } +libp2p-relay = { version = "0.13.0", path = "../../protocols/relay", optional = true } libp2p-swarm = { version = "0.40.0", path = "../../swarm" } +itertools = "0.10.5" prometheus-client = "0.18.0" [target.'cfg(not(target_os = "unknown"))'.dependencies] diff --git a/misc/metrics/examples/metrics/main.rs b/misc/metrics/examples/metrics/main.rs index 7362d5c5ebc..4b2f8c2d29c 100644 --- a/misc/metrics/examples/metrics/main.rs +++ b/misc/metrics/examples/metrics/main.rs @@ -114,7 +114,6 @@ fn main() -> Result<(), Box> { /// For illustrative purposes, this includes the [`keep_alive::Behaviour`]) behaviour so the ping actually happen /// and can be observed via the metrics. #[derive(NetworkBehaviour)] -// #[behaviour(out_event = "Event")] struct Behaviour { identify: identify::Behaviour, keep_alive: keep_alive::Behaviour, diff --git a/misc/metrics/src/identify.rs b/misc/metrics/src/identify.rs index 6d12bb40d1e..ef91b3d8d46 100644 --- a/misc/metrics/src/identify.rs +++ b/misc/metrics/src/identify.rs @@ -28,7 +28,7 @@ use std::collections::HashMap; use std::iter; use std::sync::{Arc, Mutex}; use prometheus_client::metrics::family::Family; -use super::protocol_stack::protocol_stack; +use crate::protocol_stack::protocol_stack; pub struct Metrics { protocols: Protocols, @@ -38,7 +38,7 @@ pub struct Metrics { received_info_listen_addrs: Histogram, received_info_protocols: Histogram, sent: Counter, - listen_addresses: Family, Counter>, + listen_addresses: Family, } impl Metrics { diff --git a/misc/metrics/src/protocol_stack.rs b/misc/metrics/src/protocol_stack.rs index 182ae57b345..161e56e15af 100644 --- a/misc/metrics/src/protocol_stack.rs +++ b/misc/metrics/src/protocol_stack.rs @@ -1,13 +1,44 @@ use libp2p_core::multiaddr::{Multiaddr,Protocol}; +use itertools::Itertools;//TODO: replace with std intersperse when that's stable -//TODO: remove this file and replace with calls to Multiaddr::protocol_stack +//TODO: remove most/all of this file and replace with calls to Multiaddr::protocol_stack // once that lands : https://github.com/multiformats/rust-multiaddr/pull/60 -// This does imply changing the keys from Vec to Vec<'static str> -fn tag(proto: Protocol) -> Option { - format!("{}", proto).split("/").skip(1).next().map(str::to_owned) +pub(crate) fn protocol_stack(ma: &Multiaddr) -> String { + ma.iter().map(tag).intersperse("/").collect() } -pub(crate) fn protocol_stack(ma: &Multiaddr) -> Vec { - ma.iter().filter_map(tag).collect() +pub fn tag(proto: Protocol) -> &'static str { + use Protocol::*; + match proto { + Dccp(_) => "dccp", + Dns(_) => "dns", + Dns4(_) => "dns4", + Dns6(_) => "dns6", + Dnsaddr(_) => "dnsaddr", + Http => "http", + Https => "https", + Ip4(_) => "ip4", + Ip6(_) => "ip6", + P2pWebRtcDirect => "p2p-webrtc-direct", + P2pWebRtcStar => "p2p-webrtc-star", + P2pWebSocketStar => "p2p-websocket-star", + Memory(_) => "memory", + Onion(_, _) => "onion", + Onion3(_) => "onion3", + P2p(_) => "p2p", + P2pCircuit => "p2p-circuit", + Quic => "quic", + Sctp(_) => "sctp", + Tcp(_) => "tcp", + Tls => "tls", + Udp(_) => "udp", + Udt => "udt", + Unix(_) => "unix", + Utp => "utp", + Ws(ref s) if s == "/" => "ws", + Ws(_) => "x-parity-ws", + Wss(ref s) if s == "/" => "wss", + Wss(_) => "x-parity-wss", + } } diff --git a/misc/metrics/src/swarm.rs b/misc/metrics/src/swarm.rs index 912ca3bc331..43fbb5b4f9f 100644 --- a/misc/metrics/src/swarm.rs +++ b/misc/metrics/src/swarm.rs @@ -25,7 +25,7 @@ use prometheus_client::registry::Registry; use crate::protocol_stack::protocol_stack; pub struct Metrics { - connections_incoming: Family, Counter>, + connections_incoming: Family, connections_incoming_error: Family, connections_established: Family, @@ -248,7 +248,6 @@ impl super::Recorder Date: Fri, 14 Oct 2022 01:30:14 -0400 Subject: [PATCH 05/19] Label wrapper --- misc/metrics/Cargo.toml | 1 - misc/metrics/src/identify.rs | 7 +++--- misc/metrics/src/protocol_stack.rs | 40 ++++++++++++++++++++++++++---- misc/metrics/src/swarm.rs | 7 +++--- 4 files changed, 41 insertions(+), 14 deletions(-) diff --git a/misc/metrics/Cargo.toml b/misc/metrics/Cargo.toml index 50234845217..1e96355606a 100644 --- a/misc/metrics/Cargo.toml +++ b/misc/metrics/Cargo.toml @@ -26,7 +26,6 @@ libp2p-kad = { version = "0.41.0", path = "../../protocols/kad", optional = true libp2p-ping = { version = "0.40.0", path = "../../protocols/ping", optional = true } libp2p-relay = { version = "0.13.0", path = "../../protocols/relay", optional = true } libp2p-swarm = { version = "0.40.0", path = "../../swarm" } -itertools = "0.10.5" prometheus-client = "0.18.0" [target.'cfg(not(target_os = "unknown"))'.dependencies] diff --git a/misc/metrics/src/identify.rs b/misc/metrics/src/identify.rs index ef91b3d8d46..5193e2d8142 100644 --- a/misc/metrics/src/identify.rs +++ b/misc/metrics/src/identify.rs @@ -28,7 +28,7 @@ use std::collections::HashMap; use std::iter; use std::sync::{Arc, Mutex}; use prometheus_client::metrics::family::Family; -use crate::protocol_stack::protocol_stack; +use crate::protocol_stack; pub struct Metrics { protocols: Protocols, @@ -38,7 +38,7 @@ pub struct Metrics { received_info_listen_addrs: Histogram, received_info_protocols: Histogram, sent: Counter, - listen_addresses: Family, + listen_addresses: Family, } impl Metrics { @@ -179,8 +179,7 @@ impl super::Recorder for Metrics { self.received_info_listen_addrs .observe(info.listen_addrs.len() as f64); for listen_addr in &info.listen_addrs { - let key = protocol_stack(listen_addr); - self.listen_addresses.get_or_create(&key).inc(); + self.listen_addresses.get_or_create(&protocol_stack::create_label(listen_addr)).inc(); } } libp2p_identify::Event::Sent { .. } => { diff --git a/misc/metrics/src/protocol_stack.rs b/misc/metrics/src/protocol_stack.rs index 161e56e15af..c0c0488d142 100644 --- a/misc/metrics/src/protocol_stack.rs +++ b/misc/metrics/src/protocol_stack.rs @@ -1,13 +1,27 @@ +use prometheus_client::encoding::text::Encode; use libp2p_core::multiaddr::{Multiaddr,Protocol}; -use itertools::Itertools;//TODO: replace with std intersperse when that's stable -//TODO: remove most/all of this file and replace with calls to Multiaddr::protocol_stack -// once that lands : https://github.com/multiformats/rust-multiaddr/pull/60 +#[derive(Encode, Hash, Clone, Eq, PartialEq)] +pub struct Label { + address_stack: String +} -pub(crate) fn protocol_stack(ma: &Multiaddr) -> String { - ma.iter().map(tag).intersperse("/").collect() +pub fn create_label(ma: &Multiaddr) -> Label { + // Has potential to allocate multiple times, but this line expresses the intent here. + // std::iter::once("/").chain(ma.iter().map(tag).intersperse("/")).collect() + let len = ma.iter().fold(0, |acc,proto|acc+tag(proto).len()+1); + let mut result = String::with_capacity(len); + for proto_tag in ma.iter().map(tag) { + result.push('/'); + result.push_str(proto_tag); + } + Label{ address_stack: result } } +//TODO: remove most/all of this file and replace with calls to Multiaddr::protocol_stack +// once that lands : https://github.com/multiformats/rust-multiaddr/pull/60 +// In the meantime there is no _ case in the match so one can easily detect mismatch in supported +// protocols when dependency version changes. pub fn tag(proto: Protocol) -> &'static str { use Protocol::*; match proto { @@ -42,3 +56,19 @@ pub fn tag(proto: Protocol) -> &'static str { Wss(_) => "x-parity-wss", } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn ip6_tcp_wss_p2p() { + let ma = Multiaddr::try_from("/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/8000/wss/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC").expect("testbad"); + let actual = create_label(&ma); + assert_eq!(actual.address_stack,"/ip6/tcp/wss/p2p"); + let mut buf = Vec::new(); + actual.encode(&mut buf).expect("encode failed"); + let actual = String::from_utf8(buf).expect("invalid utf-8?"); + assert_eq!(actual, "address_stack=\"/ip6/tcp/wss/p2p\""); + } +} diff --git a/misc/metrics/src/swarm.rs b/misc/metrics/src/swarm.rs index 43fbb5b4f9f..9209d622ce8 100644 --- a/misc/metrics/src/swarm.rs +++ b/misc/metrics/src/swarm.rs @@ -22,10 +22,10 @@ use prometheus_client::encoding::text::Encode; use prometheus_client::metrics::counter::Counter; use prometheus_client::metrics::family::Family; use prometheus_client::registry::Registry; -use crate::protocol_stack::protocol_stack; +use crate::protocol_stack; pub struct Metrics { - connections_incoming: Family, + connections_incoming: Family, connections_incoming_error: Family, connections_established: Family, @@ -158,8 +158,7 @@ impl super::Recorder { - let labels = protocol_stack(send_back_addr); - self.connections_incoming.get_or_create(&labels).inc(); + self.connections_incoming.get_or_create(&protocol_stack::create_label(send_back_addr)).inc(); } libp2p_swarm::SwarmEvent::IncomingConnectionError { error, .. } => { self.connections_incoming_error From f165e01b4dd867a50d02c14230c2ba924757ce8d Mon Sep 17 00:00:00 2001 From: John Turpish Date: Fri, 14 Oct 2022 01:53:15 -0400 Subject: [PATCH 06/19] MultiaddrExt --- misc/metrics/src/identify.rs | 2 +- misc/metrics/src/protocol_stack.rs | 45 ++++++++++++++++++++---------- misc/metrics/src/swarm.rs | 2 +- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/misc/metrics/src/identify.rs b/misc/metrics/src/identify.rs index 5193e2d8142..bf42977f897 100644 --- a/misc/metrics/src/identify.rs +++ b/misc/metrics/src/identify.rs @@ -179,7 +179,7 @@ impl super::Recorder for Metrics { self.received_info_listen_addrs .observe(info.listen_addrs.len() as f64); for listen_addr in &info.listen_addrs { - self.listen_addresses.get_or_create(&protocol_stack::create_label(listen_addr)).inc(); + self.listen_addresses.get_or_create(&listen_addr.into()).inc(); } } libp2p_identify::Event::Sent { .. } => { diff --git a/misc/metrics/src/protocol_stack.rs b/misc/metrics/src/protocol_stack.rs index c0c0488d142..6947749f516 100644 --- a/misc/metrics/src/protocol_stack.rs +++ b/misc/metrics/src/protocol_stack.rs @@ -1,27 +1,42 @@ use prometheus_client::encoding::text::Encode; use libp2p_core::multiaddr::{Multiaddr,Protocol}; +//TODO: remove this trait and tag() and replace with calls to the upstream method +// once that lands : https://github.com/multiformats/rust-multiaddr/pull/60 +// In the meantime there is no _ case in the match so one can easily detect mismatch in supported +// protocols when dependency version changes. +trait MultiaddrExt { + fn protocol_stack(&self) -> String; +} +impl MultiaddrExt for Multiaddr { + fn protocol_stack(&self) -> String { + // Has potential to allocate multiple times, but this line expresses the intent here. + // std::iter::once("/").chain(ma.iter().map(tag).intersperse("/")).collect() + let len = self.iter().fold(0, |acc, proto| acc + tag(proto).len() + 1); + let mut result = String::with_capacity(len); + for proto_tag in self.iter().map(tag) { + result.push('/'); + result.push_str(proto_tag); + } + result + } +} + #[derive(Encode, Hash, Clone, Eq, PartialEq)] pub struct Label { address_stack: String } - -pub fn create_label(ma: &Multiaddr) -> Label { - // Has potential to allocate multiple times, but this line expresses the intent here. - // std::iter::once("/").chain(ma.iter().map(tag).intersperse("/")).collect() - let len = ma.iter().fold(0, |acc,proto|acc+tag(proto).len()+1); - let mut result = String::with_capacity(len); - for proto_tag in ma.iter().map(tag) { - result.push('/'); - result.push_str(proto_tag); +impl From for Label { + fn from(ma: Multiaddr) -> Self { + Self:: from(&ma) + } +} +impl From<&Multiaddr> for Label { + fn from(ma: &Multiaddr) -> Self { + Self { address_stack: ma.protocol_stack() } } - Label{ address_stack: result } } -//TODO: remove most/all of this file and replace with calls to Multiaddr::protocol_stack -// once that lands : https://github.com/multiformats/rust-multiaddr/pull/60 -// In the meantime there is no _ case in the match so one can easily detect mismatch in supported -// protocols when dependency version changes. pub fn tag(proto: Protocol) -> &'static str { use Protocol::*; match proto { @@ -64,7 +79,7 @@ mod tests { #[test] fn ip6_tcp_wss_p2p() { let ma = Multiaddr::try_from("/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/8000/wss/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC").expect("testbad"); - let actual = create_label(&ma); + let actual : Label = ma.into(); assert_eq!(actual.address_stack,"/ip6/tcp/wss/p2p"); let mut buf = Vec::new(); actual.encode(&mut buf).expect("encode failed"); diff --git a/misc/metrics/src/swarm.rs b/misc/metrics/src/swarm.rs index 9209d622ce8..ca5de8d7c97 100644 --- a/misc/metrics/src/swarm.rs +++ b/misc/metrics/src/swarm.rs @@ -158,7 +158,7 @@ impl super::Recorder { - self.connections_incoming.get_or_create(&protocol_stack::create_label(send_back_addr)).inc(); + self.connections_incoming.get_or_create(&send_back_addr.into()).inc(); } libp2p_swarm::SwarmEvent::IncomingConnectionError { error, .. } => { self.connections_incoming_error From b2c74eddab760f258344cc3303e122dd0f506a08 Mon Sep 17 00:00:00 2001 From: John Turpish Date: Fri, 14 Oct 2022 07:53:17 -0400 Subject: [PATCH 07/19] not From --- misc/metrics/src/identify.rs | 2 +- misc/metrics/src/protocol_stack.rs | 27 +++++++++++---------------- misc/metrics/src/swarm.rs | 2 +- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/misc/metrics/src/identify.rs b/misc/metrics/src/identify.rs index bf42977f897..3370a3dc22d 100644 --- a/misc/metrics/src/identify.rs +++ b/misc/metrics/src/identify.rs @@ -179,7 +179,7 @@ impl super::Recorder for Metrics { self.received_info_listen_addrs .observe(info.listen_addrs.len() as f64); for listen_addr in &info.listen_addrs { - self.listen_addresses.get_or_create(&listen_addr.into()).inc(); + self.listen_addresses.get_or_create(&protocol_stack::Label::for_multi_address(&listen_addr)).inc(); } } libp2p_identify::Event::Sent { .. } => { diff --git a/misc/metrics/src/protocol_stack.rs b/misc/metrics/src/protocol_stack.rs index 6947749f516..27fac7021ca 100644 --- a/misc/metrics/src/protocol_stack.rs +++ b/misc/metrics/src/protocol_stack.rs @@ -1,6 +1,16 @@ use prometheus_client::encoding::text::Encode; use libp2p_core::multiaddr::{Multiaddr,Protocol}; +#[derive(Encode, Hash, Clone, Eq, PartialEq)] +pub struct Label { + address_stack: String +} +impl Label { + pub fn for_multi_address(ma: &Multiaddr) -> Self { + Self { address_stack: ma.protocol_stack() } + } +} + //TODO: remove this trait and tag() and replace with calls to the upstream method // once that lands : https://github.com/multiformats/rust-multiaddr/pull/60 // In the meantime there is no _ case in the match so one can easily detect mismatch in supported @@ -22,21 +32,6 @@ impl MultiaddrExt for Multiaddr { } } -#[derive(Encode, Hash, Clone, Eq, PartialEq)] -pub struct Label { - address_stack: String -} -impl From for Label { - fn from(ma: Multiaddr) -> Self { - Self:: from(&ma) - } -} -impl From<&Multiaddr> for Label { - fn from(ma: &Multiaddr) -> Self { - Self { address_stack: ma.protocol_stack() } - } -} - pub fn tag(proto: Protocol) -> &'static str { use Protocol::*; match proto { @@ -79,7 +74,7 @@ mod tests { #[test] fn ip6_tcp_wss_p2p() { let ma = Multiaddr::try_from("/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/8000/wss/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC").expect("testbad"); - let actual : Label = ma.into(); + let actual = Label::for_multi_address(&ma); assert_eq!(actual.address_stack,"/ip6/tcp/wss/p2p"); let mut buf = Vec::new(); actual.encode(&mut buf).expect("encode failed"); diff --git a/misc/metrics/src/swarm.rs b/misc/metrics/src/swarm.rs index ca5de8d7c97..9d3aae61233 100644 --- a/misc/metrics/src/swarm.rs +++ b/misc/metrics/src/swarm.rs @@ -158,7 +158,7 @@ impl super::Recorder { - self.connections_incoming.get_or_create(&send_back_addr.into()).inc(); + self.connections_incoming.get_or_create(&protocol_stack::Label::for_multi_address(&send_back_addr)).inc(); } libp2p_swarm::SwarmEvent::IncomingConnectionError { error, .. } => { self.connections_incoming_error From 50c8bb52c246567ccb4d0f280fb7537db0feb27d Mon Sep 17 00:00:00 2001 From: John Turpish Date: Fri, 14 Oct 2022 08:02:02 -0400 Subject: [PATCH 08/19] cargo fmt --- misc/metrics/examples/metrics/main.rs | 6 +++--- misc/metrics/src/identify.rs | 12 +++++++----- misc/metrics/src/lib.rs | 2 +- misc/metrics/src/protocol_stack.rs | 12 +++++++----- misc/metrics/src/swarm.rs | 6 ++++-- 5 files changed, 22 insertions(+), 16 deletions(-) diff --git a/misc/metrics/examples/metrics/main.rs b/misc/metrics/examples/metrics/main.rs index 4b2f8c2d29c..0015c407c3a 100644 --- a/misc/metrics/examples/metrics/main.rs +++ b/misc/metrics/examples/metrics/main.rs @@ -120,11 +120,11 @@ struct Behaviour { ping: ping::Behaviour, } -impl Behaviour{ +impl Behaviour { fn new(local_key: libp2p::identity::Keypair) -> Self { - Self{ + Self { ping: ping::Behaviour::default(), - identify: identify::Behaviour::new( identify::Config::new( + identify: identify::Behaviour::new(identify::Config::new( "/ipfs/0.1.0".into(), local_key.public(), )), diff --git a/misc/metrics/src/identify.rs b/misc/metrics/src/identify.rs index 3370a3dc22d..30b8fdefc78 100644 --- a/misc/metrics/src/identify.rs +++ b/misc/metrics/src/identify.rs @@ -18,17 +18,17 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. +use crate::protocol_stack; use libp2p_core::PeerId; use prometheus_client::encoding::text::{EncodeMetric, Encoder}; use prometheus_client::metrics::counter::Counter; +use prometheus_client::metrics::family::Family; use prometheus_client::metrics::histogram::{exponential_buckets, Histogram}; use prometheus_client::metrics::MetricType; use prometheus_client::registry::Registry; use std::collections::HashMap; use std::iter; use std::sync::{Arc, Mutex}; -use prometheus_client::metrics::family::Family; -use crate::protocol_stack; pub struct Metrics { protocols: Protocols, @@ -107,7 +107,7 @@ impl Metrics { sub_registry.register( "listen_addresses", "Number of listen addresses for remote peer per protocol stack", - Box::new(listen_addresses.clone()) + Box::new(listen_addresses.clone()), ); Self { @@ -118,7 +118,7 @@ impl Metrics { received_info_listen_addrs, received_info_protocols, sent, - listen_addresses + listen_addresses, } } } @@ -179,7 +179,9 @@ impl super::Recorder for Metrics { self.received_info_listen_addrs .observe(info.listen_addrs.len() as f64); for listen_addr in &info.listen_addrs { - self.listen_addresses.get_or_create(&protocol_stack::Label::for_multi_address(&listen_addr)).inc(); + self.listen_addresses + .get_or_create(&protocol_stack::Label::for_multi_address(&listen_addr)) + .inc(); } } libp2p_identify::Event::Sent { .. } => { diff --git a/misc/metrics/src/lib.rs b/misc/metrics/src/lib.rs index af1f89dd6a4..bbbff073e7c 100644 --- a/misc/metrics/src/lib.rs +++ b/misc/metrics/src/lib.rs @@ -36,10 +36,10 @@ mod identify; mod kad; #[cfg(feature = "ping")] mod ping; +mod protocol_stack; #[cfg(feature = "relay")] mod relay; mod swarm; -mod protocol_stack; use prometheus_client::registry::Registry; diff --git a/misc/metrics/src/protocol_stack.rs b/misc/metrics/src/protocol_stack.rs index 27fac7021ca..56b87694acb 100644 --- a/misc/metrics/src/protocol_stack.rs +++ b/misc/metrics/src/protocol_stack.rs @@ -1,13 +1,15 @@ +use libp2p_core::multiaddr::{Multiaddr, Protocol}; use prometheus_client::encoding::text::Encode; -use libp2p_core::multiaddr::{Multiaddr,Protocol}; #[derive(Encode, Hash, Clone, Eq, PartialEq)] pub struct Label { - address_stack: String + address_stack: String, } impl Label { pub fn for_multi_address(ma: &Multiaddr) -> Self { - Self { address_stack: ma.protocol_stack() } + Self { + address_stack: ma.protocol_stack(), + } } } @@ -75,10 +77,10 @@ mod tests { fn ip6_tcp_wss_p2p() { let ma = Multiaddr::try_from("/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/8000/wss/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC").expect("testbad"); let actual = Label::for_multi_address(&ma); - assert_eq!(actual.address_stack,"/ip6/tcp/wss/p2p"); + assert_eq!(actual.address_stack, "/ip6/tcp/wss/p2p"); let mut buf = Vec::new(); actual.encode(&mut buf).expect("encode failed"); let actual = String::from_utf8(buf).expect("invalid utf-8?"); - assert_eq!(actual, "address_stack=\"/ip6/tcp/wss/p2p\""); + assert_eq!(actual, r#"address_stack="/ip6/tcp/wss/p2p""#); } } diff --git a/misc/metrics/src/swarm.rs b/misc/metrics/src/swarm.rs index 9d3aae61233..587e92ac947 100644 --- a/misc/metrics/src/swarm.rs +++ b/misc/metrics/src/swarm.rs @@ -18,11 +18,11 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. +use crate::protocol_stack; use prometheus_client::encoding::text::Encode; use prometheus_client::metrics::counter::Counter; use prometheus_client::metrics::family::Family; use prometheus_client::registry::Registry; -use crate::protocol_stack; pub struct Metrics { connections_incoming: Family, @@ -158,7 +158,9 @@ impl super::Recorder { - self.connections_incoming.get_or_create(&protocol_stack::Label::for_multi_address(&send_back_addr)).inc(); + self.connections_incoming + .get_or_create(&protocol_stack::Label::for_multi_address(&send_back_addr)) + .inc(); } libp2p_swarm::SwarmEvent::IncomingConnectionError { error, .. } => { self.connections_incoming_error From d74e7e5a0ed5920deb0a1820f7161313293adc10 Mon Sep 17 00:00:00 2001 From: John Turpish Date: Fri, 14 Oct 2022 09:16:27 -0400 Subject: [PATCH 09/19] clippy --- misc/metrics/src/identify.rs | 2 +- misc/metrics/src/swarm.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/misc/metrics/src/identify.rs b/misc/metrics/src/identify.rs index 30b8fdefc78..fe24c3a2fe0 100644 --- a/misc/metrics/src/identify.rs +++ b/misc/metrics/src/identify.rs @@ -180,7 +180,7 @@ impl super::Recorder for Metrics { .observe(info.listen_addrs.len() as f64); for listen_addr in &info.listen_addrs { self.listen_addresses - .get_or_create(&protocol_stack::Label::for_multi_address(&listen_addr)) + .get_or_create(&protocol_stack::Label::for_multi_address(listen_addr)) .inc(); } } diff --git a/misc/metrics/src/swarm.rs b/misc/metrics/src/swarm.rs index 587e92ac947..d92516824c5 100644 --- a/misc/metrics/src/swarm.rs +++ b/misc/metrics/src/swarm.rs @@ -159,7 +159,7 @@ impl super::Recorder { self.connections_incoming - .get_or_create(&protocol_stack::Label::for_multi_address(&send_back_addr)) + .get_or_create(&protocol_stack::Label::for_multi_address(send_back_addr)) .inc(); } libp2p_swarm::SwarmEvent::IncomingConnectionError { error, .. } => { From e5974ca0af1ee303caebb3ee46f962a9d52010ec Mon Sep 17 00:00:00 2001 From: John Turpish Date: Fri, 14 Oct 2022 09:17:50 -0400 Subject: [PATCH 10/19] changelog --- misc/metrics/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/misc/metrics/CHANGELOG.md b/misc/metrics/CHANGELOG.md index fe428745d70..5480fcc0198 100644 --- a/misc/metrics/CHANGELOG.md +++ b/misc/metrics/CHANGELOG.md @@ -1,5 +1,7 @@ # 0.10.0 [unreleased] +- Added `protocol_stack` metrics. + - Update to `libp2p-swarm` `v0.40.0`. - Update to `libp2p-dcutr` `v0.7.0`. From 3900f691861795e5a04a9094a1608e98afc10f31 Mon Sep 17 00:00:00 2001 From: John Turpish Date: Mon, 17 Oct 2022 11:53:35 -0400 Subject: [PATCH 11/19] Version bump. --- misc/metrics/CHANGELOG.md | 6 ++++-- misc/metrics/Cargo.toml | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/misc/metrics/CHANGELOG.md b/misc/metrics/CHANGELOG.md index c1645b74f79..33a304196b4 100644 --- a/misc/metrics/CHANGELOG.md +++ b/misc/metrics/CHANGELOG.md @@ -1,6 +1,8 @@ -# 0.10.0 +# 0.10.1 + +- Added `protocol_stack` metrics. See [PR 2982]. -- Added `protocol_stack` metrics. +# 0.10.0 - Update to `libp2p-swarm` `v0.40.0`. diff --git a/misc/metrics/Cargo.toml b/misc/metrics/Cargo.toml index 1e96355606a..dc9cfabeee8 100644 --- a/misc/metrics/Cargo.toml +++ b/misc/metrics/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-metrics" edition = "2021" rust-version = "1.56.1" description = "Metrics for libp2p" -version = "0.10.0" +version = "0.10.1" authors = ["Max Inden "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" From 9a227cae17d34626f1dde18b39720d5db10fbb2e Mon Sep 17 00:00:00 2001 From: John Turpish Date: Wed, 19 Oct 2022 16:04:46 -0400 Subject: [PATCH 12/19] PR comment --- misc/metrics/CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/misc/metrics/CHANGELOG.md b/misc/metrics/CHANGELOG.md index 33a304196b4..c2aae1e0169 100644 --- a/misc/metrics/CHANGELOG.md +++ b/misc/metrics/CHANGELOG.md @@ -1,7 +1,9 @@ -# 0.10.1 +# 0.10.1 [unreleased] - Added `protocol_stack` metrics. See [PR 2982]. +[PR 2982]: https://github.com/libp2p/rust-libp2p/pull/2982/ + # 0.10.0 - Update to `libp2p-swarm` `v0.40.0`. From 16c802b2c857fc040393e269f9f999690095a585 Mon Sep 17 00:00:00 2001 From: John Turpish Date: Wed, 2 Nov 2022 17:32:00 -0400 Subject: [PATCH 13/19] A couple PR comments. --- misc/metrics/examples/metrics/main.rs | 9 +++++---- misc/metrics/src/identify.rs | 4 ++-- misc/metrics/src/protocol_stack.rs | 13 ++++++++----- misc/metrics/src/swarm.rs | 7 +++++-- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/misc/metrics/examples/metrics/main.rs b/misc/metrics/examples/metrics/main.rs index 0015c407c3a..160af9fd2d7 100644 --- a/misc/metrics/examples/metrics/main.rs +++ b/misc/metrics/examples/metrics/main.rs @@ -68,11 +68,12 @@ fn main() -> Result<(), Box> { let local_key = identity::Keypair::generate_ed25519(); let local_peer_id = PeerId::from(local_key.public()); + let local_pub_key = local_key.public(); info!("Local peer id: {:?}", local_peer_id); let mut swarm = Swarm::new( - block_on(libp2p::development_transport(local_key.clone()))?, - Behaviour::new(local_key), + block_on(libp2p::development_transport(local_key))?, + Behaviour::new(local_pub_key), local_peer_id, ); @@ -121,12 +122,12 @@ struct Behaviour { } impl Behaviour { - fn new(local_key: libp2p::identity::Keypair) -> Self { + fn new(local_pub_key: libp2p::identity::PublicKey) -> Self { Self { ping: ping::Behaviour::default(), identify: identify::Behaviour::new(identify::Config::new( "/ipfs/0.1.0".into(), - local_key.public(), + local_pub_key, )), keep_alive: keep_alive::Behaviour::default(), } diff --git a/misc/metrics/src/identify.rs b/misc/metrics/src/identify.rs index fe24c3a2fe0..0f580ecfde2 100644 --- a/misc/metrics/src/identify.rs +++ b/misc/metrics/src/identify.rs @@ -38,7 +38,7 @@ pub struct Metrics { received_info_listen_addrs: Histogram, received_info_protocols: Histogram, sent: Counter, - listen_addresses: Family, + listen_addresses: Family, } impl Metrics { @@ -180,7 +180,7 @@ impl super::Recorder for Metrics { .observe(info.listen_addrs.len() as f64); for listen_addr in &info.listen_addrs { self.listen_addresses - .get_or_create(&protocol_stack::Label::for_multi_address(listen_addr)) + .get_or_create(&listen_addr.into()) .inc(); } } diff --git a/misc/metrics/src/protocol_stack.rs b/misc/metrics/src/protocol_stack.rs index 56b87694acb..1968000c007 100644 --- a/misc/metrics/src/protocol_stack.rs +++ b/misc/metrics/src/protocol_stack.rs @@ -2,11 +2,11 @@ use libp2p_core::multiaddr::{Multiaddr, Protocol}; use prometheus_client::encoding::text::Encode; #[derive(Encode, Hash, Clone, Eq, PartialEq)] -pub struct Label { +pub struct Labels { address_stack: String, } -impl Label { - pub fn for_multi_address(ma: &Multiaddr) -> Self { +impl From<&Multiaddr> for Labels { + fn from(ma: &Multiaddr) -> Self { Self { address_stack: ma.protocol_stack(), } @@ -17,7 +17,7 @@ impl Label { // once that lands : https://github.com/multiformats/rust-multiaddr/pull/60 // In the meantime there is no _ case in the match so one can easily detect mismatch in supported // protocols when dependency version changes. -trait MultiaddrExt { +pub trait MultiaddrExt { fn protocol_stack(&self) -> String; } impl MultiaddrExt for Multiaddr { @@ -37,6 +37,7 @@ impl MultiaddrExt for Multiaddr { pub fn tag(proto: Protocol) -> &'static str { use Protocol::*; match proto { + Certhash(_) => "certhash", Dccp(_) => "dccp", Dns(_) => "dns", Dns4(_) => "dns4", @@ -50,6 +51,7 @@ pub fn tag(proto: Protocol) -> &'static str { P2pWebRtcStar => "p2p-webrtc-star", P2pWebSocketStar => "p2p-websocket-star", Memory(_) => "memory", + Noise => "noise", Onion(_, _) => "onion", Onion3(_) => "onion3", P2p(_) => "p2p", @@ -62,6 +64,7 @@ pub fn tag(proto: Protocol) -> &'static str { Udt => "udt", Unix(_) => "unix", Utp => "utp", + WebRTC => "webrtc", Ws(ref s) if s == "/" => "ws", Ws(_) => "x-parity-ws", Wss(ref s) if s == "/" => "wss", @@ -76,7 +79,7 @@ mod tests { #[test] fn ip6_tcp_wss_p2p() { let ma = Multiaddr::try_from("/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/8000/wss/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC").expect("testbad"); - let actual = Label::for_multi_address(&ma); + let actual = Labels::for_multi_address(&ma); assert_eq!(actual.address_stack, "/ip6/tcp/wss/p2p"); let mut buf = Vec::new(); actual.encode(&mut buf).expect("encode failed"); diff --git a/misc/metrics/src/swarm.rs b/misc/metrics/src/swarm.rs index d92516824c5..9a0618e7579 100644 --- a/misc/metrics/src/swarm.rs +++ b/misc/metrics/src/swarm.rs @@ -23,9 +23,10 @@ use prometheus_client::encoding::text::Encode; use prometheus_client::metrics::counter::Counter; use prometheus_client::metrics::family::Family; use prometheus_client::registry::Registry; +use protocol_stack::MultiaddrExt; pub struct Metrics { - connections_incoming: Family, + connections_incoming: Family, connections_incoming_error: Family, connections_established: Family, @@ -147,6 +148,7 @@ impl super::Recorder super::Recorder { self.connections_incoming - .get_or_create(&protocol_stack::Label::for_multi_address(send_back_addr)) + .get_or_create(&send_back_addr.into()) .inc(); } libp2p_swarm::SwarmEvent::IncomingConnectionError { error, .. } => { @@ -249,6 +251,7 @@ impl super::Recorder Date: Thu, 3 Nov 2022 10:28:47 -0400 Subject: [PATCH 14/19] Use protocol stack for a few more metrics. --- misc/metrics/src/swarm.rs | 48 +++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/misc/metrics/src/swarm.rs b/misc/metrics/src/swarm.rs index 9a0618e7579..b11cd64b4da 100644 --- a/misc/metrics/src/swarm.rs +++ b/misc/metrics/src/swarm.rs @@ -32,15 +32,15 @@ pub struct Metrics { connections_established: Family, connections_closed: Family, - new_listen_addr: Counter, - expired_listen_addr: Counter, + new_listen_addr: Family, + expired_listen_addr: Family, - listener_closed: Counter, + listener_closed: Family, listener_error: Counter, dial_attempt: Counter, outgoing_connection_error: Family, - connected_to_banned_peer: Counter, + connected_to_banned_peer: Family, } impl Metrics { @@ -61,21 +61,21 @@ impl Metrics { Box::new(connections_incoming_error.clone()), ); - let new_listen_addr = Counter::default(); + let new_listen_addr = Family::default(); sub_registry.register( "new_listen_addr", "Number of new listen addresses", Box::new(new_listen_addr.clone()), ); - let expired_listen_addr = Counter::default(); + let expired_listen_addr = Family::default(); sub_registry.register( "expired_listen_addr", "Number of expired listen addresses", Box::new(expired_listen_addr.clone()), ); - let listener_closed = Counter::default(); + let listener_closed = Family::default(); sub_registry.register( "listener_closed", "Number of listeners closed", @@ -103,7 +103,7 @@ impl Metrics { Box::new(outgoing_connection_error.clone()), ); - let connected_to_banned_peer = Counter::default(); + let connected_to_banned_peer = Family::default(); sub_registry.register( "connected_to_banned_peer", "Number of connection attempts to banned peer", @@ -156,6 +156,7 @@ impl super::Recorder super::Recorder { + libp2p_swarm::SwarmEvent::IncomingConnectionError { + error, + send_back_addr, + .. + } => { self.connections_incoming_error .get_or_create(&IncomingConnectionErrorLabels { error: error.into(), + proto_stack: send_back_addr.protocol_stack(), }) .inc(); } @@ -226,17 +232,23 @@ impl super::Recorder { - self.connected_to_banned_peer.inc(); + libp2p_swarm::SwarmEvent::BannedPeer { endpoint, .. } => { + self.connected_to_banned_peer + .get_or_create(&endpoint.get_remote_address().into()) + .inc(); } - libp2p_swarm::SwarmEvent::NewListenAddr { .. } => { - self.new_listen_addr.inc(); + libp2p_swarm::SwarmEvent::NewListenAddr { address, .. } => { + self.new_listen_addr.get_or_create(&address.into()).inc(); } - libp2p_swarm::SwarmEvent::ExpiredListenAddr { .. } => { - self.expired_listen_addr.inc(); + libp2p_swarm::SwarmEvent::ExpiredListenAddr { address, .. } => { + self.expired_listen_addr + .get_or_create(&address.into()) + .inc(); } - libp2p_swarm::SwarmEvent::ListenerClosed { .. } => { - self.listener_closed.inc(); + libp2p_swarm::SwarmEvent::ListenerClosed { addresses, .. } => { + for address in addresses { + self.listener_closed.get_or_create(&address.into()).inc(); + } } libp2p_swarm::SwarmEvent::ListenerError { .. } => { self.listener_error.inc(); @@ -257,6 +269,7 @@ struct ConnectionEstablishedLabels { #[derive(Encode, Hash, Clone, Eq, PartialEq)] struct ConnectionClosedLabels { role: Role, + proto_stack: String, } #[derive(Encode, Hash, Clone, Eq, PartialEq)] @@ -304,6 +317,7 @@ enum OutgoingConnectionErrorError { #[derive(Encode, Hash, Clone, Eq, PartialEq)] struct IncomingConnectionErrorLabels { error: PendingInboundConnectionError, + proto_stack: String, } #[derive(Encode, Hash, Clone, Eq, PartialEq)] From 6983a597755600b583885d772a124c1bccc90e7a Mon Sep 17 00:00:00 2001 From: John Turpish Date: Fri, 4 Nov 2022 14:47:54 -0400 Subject: [PATCH 15/19] metric label naming consistency --- misc/metrics/src/protocol_stack.rs | 4 ++-- misc/metrics/src/swarm.rs | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/misc/metrics/src/protocol_stack.rs b/misc/metrics/src/protocol_stack.rs index 1968000c007..84db0178d8f 100644 --- a/misc/metrics/src/protocol_stack.rs +++ b/misc/metrics/src/protocol_stack.rs @@ -3,12 +3,12 @@ use prometheus_client::encoding::text::Encode; #[derive(Encode, Hash, Clone, Eq, PartialEq)] pub struct Labels { - address_stack: String, + protocols: String, } impl From<&Multiaddr> for Labels { fn from(ma: &Multiaddr) -> Self { Self { - address_stack: ma.protocol_stack(), + protocols: ma.protocol_stack(), } } } diff --git a/misc/metrics/src/swarm.rs b/misc/metrics/src/swarm.rs index b11cd64b4da..514cf2f699f 100644 --- a/misc/metrics/src/swarm.rs +++ b/misc/metrics/src/swarm.rs @@ -148,7 +148,7 @@ impl super::Recorder super::Recorder super::Recorder super::Recorder Date: Fri, 4 Nov 2022 16:16:22 -0400 Subject: [PATCH 16/19] remove impl From --- misc/metrics/src/identify.rs | 2 +- misc/metrics/src/protocol_stack.rs | 4 ++-- misc/metrics/src/swarm.rs | 14 +++++++++----- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/misc/metrics/src/identify.rs b/misc/metrics/src/identify.rs index 0f580ecfde2..bbaf2b62c27 100644 --- a/misc/metrics/src/identify.rs +++ b/misc/metrics/src/identify.rs @@ -180,7 +180,7 @@ impl super::Recorder for Metrics { .observe(info.listen_addrs.len() as f64); for listen_addr in &info.listen_addrs { self.listen_addresses - .get_or_create(&listen_addr.into()) + .get_or_create(&protocol_stack::Labels::new(&listen_addr)) .inc(); } } diff --git a/misc/metrics/src/protocol_stack.rs b/misc/metrics/src/protocol_stack.rs index 84db0178d8f..b8633855d1f 100644 --- a/misc/metrics/src/protocol_stack.rs +++ b/misc/metrics/src/protocol_stack.rs @@ -5,8 +5,8 @@ use prometheus_client::encoding::text::Encode; pub struct Labels { protocols: String, } -impl From<&Multiaddr> for Labels { - fn from(ma: &Multiaddr) -> Self { +impl Labels { + pub fn new(ma: &Multiaddr) -> Self { Self { protocols: ma.protocol_stack(), } diff --git a/misc/metrics/src/swarm.rs b/misc/metrics/src/swarm.rs index 514cf2f699f..4bb4507120d 100644 --- a/misc/metrics/src/swarm.rs +++ b/misc/metrics/src/swarm.rs @@ -162,7 +162,7 @@ impl super::Recorder { self.connections_incoming - .get_or_create(&send_back_addr.into()) + .get_or_create(&protocol_stack::Labels::new(&send_back_addr)) .inc(); } libp2p_swarm::SwarmEvent::IncomingConnectionError { @@ -234,20 +234,24 @@ impl super::Recorder { self.connected_to_banned_peer - .get_or_create(&endpoint.get_remote_address().into()) + .get_or_create(&protocol_stack::Labels::new(endpoint.get_remote_address())) .inc(); } libp2p_swarm::SwarmEvent::NewListenAddr { address, .. } => { - self.new_listen_addr.get_or_create(&address.into()).inc(); + self.new_listen_addr + .get_or_create(&protocol_stack::Labels::new(&address)) + .inc(); } libp2p_swarm::SwarmEvent::ExpiredListenAddr { address, .. } => { self.expired_listen_addr - .get_or_create(&address.into()) + .get_or_create(&protocol_stack::Labels::new(&address)) .inc(); } libp2p_swarm::SwarmEvent::ListenerClosed { addresses, .. } => { for address in addresses { - self.listener_closed.get_or_create(&address.into()).inc(); + self.listener_closed + .get_or_create(&protocol_stack::Labels::new(&address)) + .inc(); } } libp2p_swarm::SwarmEvent::ListenerError { .. } => { From ece8c9b05378743a48d94c1f4f4a6c2ee4784bae Mon Sep 17 00:00:00 2001 From: John Turpish Date: Tue, 8 Nov 2022 11:13:08 -0500 Subject: [PATCH 17/19] PR comments & clippy --- misc/metrics/CHANGELOG.md | 2 +- misc/metrics/src/protocol_stack.rs | 4 +++- misc/metrics/src/swarm.rs | 8 ++++---- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/misc/metrics/CHANGELOG.md b/misc/metrics/CHANGELOG.md index ec83af8bb19..30317d15d3e 100644 --- a/misc/metrics/CHANGELOG.md +++ b/misc/metrics/CHANGELOG.md @@ -16,7 +16,7 @@ - Update to `libp2p-gossipsub` `v0.43.0`. -- Added `protocol_stack` metrics. See [PR 2982]. +- Add `protocol_stack` metrics. See [PR 2982]. [PR 2982]: https://github.com/libp2p/rust-libp2p/pull/2982/ diff --git a/misc/metrics/src/protocol_stack.rs b/misc/metrics/src/protocol_stack.rs index b8633855d1f..eb67214657a 100644 --- a/misc/metrics/src/protocol_stack.rs +++ b/misc/metrics/src/protocol_stack.rs @@ -5,6 +5,7 @@ use prometheus_client::encoding::text::Encode; pub struct Labels { protocols: String, } + impl Labels { pub fn new(ma: &Multiaddr) -> Self { Self { @@ -20,6 +21,7 @@ impl Labels { pub trait MultiaddrExt { fn protocol_stack(&self) -> String; } + impl MultiaddrExt for Multiaddr { fn protocol_stack(&self) -> String { // Has potential to allocate multiple times, but this line expresses the intent here. @@ -34,7 +36,7 @@ impl MultiaddrExt for Multiaddr { } } -pub fn tag(proto: Protocol) -> &'static str { +fn tag(proto: Protocol) -> &'static str { use Protocol::*; match proto { Certhash(_) => "certhash", diff --git a/misc/metrics/src/swarm.rs b/misc/metrics/src/swarm.rs index 4bb4507120d..4a8795a231a 100644 --- a/misc/metrics/src/swarm.rs +++ b/misc/metrics/src/swarm.rs @@ -162,7 +162,7 @@ impl super::Recorder { self.connections_incoming - .get_or_create(&protocol_stack::Labels::new(&send_back_addr)) + .get_or_create(&protocol_stack::Labels::new(send_back_addr)) .inc(); } libp2p_swarm::SwarmEvent::IncomingConnectionError { @@ -239,18 +239,18 @@ impl super::Recorder { self.new_listen_addr - .get_or_create(&protocol_stack::Labels::new(&address)) + .get_or_create(&protocol_stack::Labels::new(address)) .inc(); } libp2p_swarm::SwarmEvent::ExpiredListenAddr { address, .. } => { self.expired_listen_addr - .get_or_create(&protocol_stack::Labels::new(&address)) + .get_or_create(&protocol_stack::Labels::new(address)) .inc(); } libp2p_swarm::SwarmEvent::ListenerClosed { addresses, .. } => { for address in addresses { self.listener_closed - .get_or_create(&protocol_stack::Labels::new(&address)) + .get_or_create(&protocol_stack::Labels::new(address)) .inc(); } } From 0f6bdd1dd97552cc711cfd7d610c260a24c67331 Mon Sep 17 00:00:00 2001 From: John Turpish Date: Mon, 14 Nov 2022 17:10:18 -0500 Subject: [PATCH 18/19] Fix a clippy and a no-default-feature use stmt --- misc/metrics/CHANGELOG.md | 1 + misc/metrics/examples/metrics/main.rs | 4 ++-- misc/metrics/src/identify.rs | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/misc/metrics/CHANGELOG.md b/misc/metrics/CHANGELOG.md index 30317d15d3e..58460094ae8 100644 --- a/misc/metrics/CHANGELOG.md +++ b/misc/metrics/CHANGELOG.md @@ -151,6 +151,7 @@ - Drop support for gossipsub in wasm32-unknown-unknown target (see [PR 2506]). [PR 2442]: https://github.com/libp2p/rust-libp2p/pull/2442 + [PR 2506]: https://github.com/libp2p/rust-libp2p/pull/2506 # 0.3.0 [2022-01-27] diff --git a/misc/metrics/examples/metrics/main.rs b/misc/metrics/examples/metrics/main.rs index 160af9fd2d7..89184ee726e 100644 --- a/misc/metrics/examples/metrics/main.rs +++ b/misc/metrics/examples/metrics/main.rs @@ -53,8 +53,8 @@ use futures::executor::block_on; use futures::stream::StreamExt; use libp2p::core::Multiaddr; use libp2p::metrics::{Metrics, Recorder}; -use libp2p::swarm::SwarmEvent; -use libp2p::{identify, identity, ping, NetworkBehaviour, PeerId, Swarm}; +use libp2p::swarm::{NetworkBehaviour, SwarmEvent}; +use libp2p::{identify, identity, ping, PeerId, Swarm}; use libp2p_swarm::keep_alive; use log::info; use prometheus_client::registry::Registry; diff --git a/misc/metrics/src/identify.rs b/misc/metrics/src/identify.rs index bbaf2b62c27..4822116cc82 100644 --- a/misc/metrics/src/identify.rs +++ b/misc/metrics/src/identify.rs @@ -180,7 +180,7 @@ impl super::Recorder for Metrics { .observe(info.listen_addrs.len() as f64); for listen_addr in &info.listen_addrs { self.listen_addresses - .get_or_create(&protocol_stack::Labels::new(&listen_addr)) + .get_or_create(&protocol_stack::Labels::new(listen_addr)) .inc(); } } From f787368e61df0df3493599e4758a91f789cce3c3 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 15 Nov 2022 11:12:39 +1100 Subject: [PATCH 19/19] Move `Labels` to usage site The labels are specific to the metric they are used it whereas the `protocol_stack` module is more generic and shouldn't have a dependency on prometheus. --- misc/metrics/src/identify.rs | 13 ++++++++--- misc/metrics/src/protocol_stack.rs | 24 ++++---------------- misc/metrics/src/swarm.rs | 35 +++++++++++++++++++++--------- 3 files changed, 39 insertions(+), 33 deletions(-) diff --git a/misc/metrics/src/identify.rs b/misc/metrics/src/identify.rs index 4822116cc82..688c67a6190 100644 --- a/misc/metrics/src/identify.rs +++ b/misc/metrics/src/identify.rs @@ -20,7 +20,7 @@ use crate::protocol_stack; use libp2p_core::PeerId; -use prometheus_client::encoding::text::{EncodeMetric, Encoder}; +use prometheus_client::encoding::text::{Encode, EncodeMetric, Encoder}; use prometheus_client::metrics::counter::Counter; use prometheus_client::metrics::family::Family; use prometheus_client::metrics::histogram::{exponential_buckets, Histogram}; @@ -38,7 +38,7 @@ pub struct Metrics { received_info_listen_addrs: Histogram, received_info_protocols: Histogram, sent: Counter, - listen_addresses: Family, + listen_addresses: Family, } impl Metrics { @@ -180,7 +180,9 @@ impl super::Recorder for Metrics { .observe(info.listen_addrs.len() as f64); for listen_addr in &info.listen_addrs { self.listen_addresses - .get_or_create(&protocol_stack::Labels::new(listen_addr)) + .get_or_create(&AddressLabels { + protocols: protocol_stack::as_string(listen_addr), + }) .inc(); } } @@ -206,6 +208,11 @@ impl super::Recorder>>>, diff --git a/misc/metrics/src/protocol_stack.rs b/misc/metrics/src/protocol_stack.rs index fb5d8f2ed38..1715b51f034 100644 --- a/misc/metrics/src/protocol_stack.rs +++ b/misc/metrics/src/protocol_stack.rs @@ -1,5 +1,4 @@ use libp2p_core::multiaddr::Multiaddr; -use prometheus_client::encoding::text::Encode; pub fn as_string(ma: &Multiaddr) -> String { let len = ma @@ -13,19 +12,6 @@ pub fn as_string(ma: &Multiaddr) -> String { protocols } -#[derive(Encode, Hash, Clone, Eq, PartialEq)] -pub struct Labels { - protocols: String, -} - -impl Labels { - pub fn new(ma: &Multiaddr) -> Self { - Self { - protocols: as_string(ma), - } - } -} - #[cfg(test)] mod tests { use super::*; @@ -33,11 +19,9 @@ mod tests { #[test] fn ip6_tcp_wss_p2p() { let ma = Multiaddr::try_from("/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/8000/wss/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC").expect("testbad"); - let actual = Labels::new(&ma); - assert_eq!(actual.protocols, "/ip6/tcp/wss/p2p"); - let mut buf = Vec::new(); - actual.encode(&mut buf).expect("encode failed"); - let actual = String::from_utf8(buf).expect("invalid utf-8?"); - assert_eq!(actual, r#"protocols="/ip6/tcp/wss/p2p""#); + + let protocol_stack = as_string(&ma); + + assert_eq!(protocol_stack, "/ip6/tcp/wss/p2p"); } } diff --git a/misc/metrics/src/swarm.rs b/misc/metrics/src/swarm.rs index fbce148ffca..c4fa8712d14 100644 --- a/misc/metrics/src/swarm.rs +++ b/misc/metrics/src/swarm.rs @@ -25,21 +25,21 @@ use prometheus_client::metrics::family::Family; use prometheus_client::registry::Registry; pub struct Metrics { - connections_incoming: Family, + connections_incoming: Family, connections_incoming_error: Family, connections_established: Family, connections_closed: Family, - new_listen_addr: Family, - expired_listen_addr: Family, + new_listen_addr: Family, + expired_listen_addr: Family, - listener_closed: Family, + listener_closed: Family, listener_error: Counter, dial_attempt: Counter, outgoing_connection_error: Family, - connected_to_banned_peer: Family, + connected_to_banned_peer: Family, } impl Metrics { @@ -161,7 +161,9 @@ impl super::Recorder { self.connections_incoming - .get_or_create(&protocol_stack::Labels::new(send_back_addr)) + .get_or_create(&AddressLabels { + protocols: protocol_stack::as_string(send_back_addr), + }) .inc(); } libp2p_swarm::SwarmEvent::IncomingConnectionError { @@ -233,23 +235,31 @@ impl super::Recorder { self.connected_to_banned_peer - .get_or_create(&protocol_stack::Labels::new(endpoint.get_remote_address())) + .get_or_create(&AddressLabels { + protocols: protocol_stack::as_string(endpoint.get_remote_address()), + }) .inc(); } libp2p_swarm::SwarmEvent::NewListenAddr { address, .. } => { self.new_listen_addr - .get_or_create(&protocol_stack::Labels::new(address)) + .get_or_create(&AddressLabels { + protocols: protocol_stack::as_string(address), + }) .inc(); } libp2p_swarm::SwarmEvent::ExpiredListenAddr { address, .. } => { self.expired_listen_addr - .get_or_create(&protocol_stack::Labels::new(address)) + .get_or_create(&AddressLabels { + protocols: protocol_stack::as_string(address), + }) .inc(); } libp2p_swarm::SwarmEvent::ListenerClosed { addresses, .. } => { for address in addresses { self.listener_closed - .get_or_create(&protocol_stack::Labels::new(address)) + .get_or_create(&AddressLabels { + protocols: protocol_stack::as_string(address), + }) .inc(); } } @@ -275,6 +285,11 @@ struct ConnectionClosedLabels { protocols: String, } +#[derive(Encode, Hash, Clone, Eq, PartialEq)] +struct AddressLabels { + protocols: String, +} + #[derive(Encode, Hash, Clone, Eq, PartialEq)] enum Role { Dialer,