From aed32b7f13bccbc669e5be011bec558a06364451 Mon Sep 17 00:00:00 2001 From: Max Inden Date: Sat, 8 Jul 2023 15:03:15 +0900 Subject: [PATCH 1/5] refactor(metrics/identify): update to new collector trait Updates to `prometheus-client` `Collector` trait refactor introduced with https://github.com/prometheus/client_rust/pull/149. --- misc/metrics/Cargo.toml | 1 - misc/metrics/src/identify.rs | 114 ++++++++++++++--------------------- 2 files changed, 45 insertions(+), 70 deletions(-) diff --git a/misc/metrics/Cargo.toml b/misc/metrics/Cargo.toml index 7a579408409..5a7580979f3 100644 --- a/misc/metrics/Cargo.toml +++ b/misc/metrics/Cargo.toml @@ -29,7 +29,6 @@ libp2p-relay = { workspace = true, optional = true } libp2p-swarm = { workspace = true } libp2p-identity = { workspace = true } prometheus-client = { version = "0.21.1"} -once_cell = "1.18.0" [target.'cfg(not(target_os = "unknown"))'.dependencies] libp2p-gossipsub = { workspace = true, optional = true } diff --git a/misc/metrics/src/identify.rs b/misc/metrics/src/identify.rs index 0e2527c09df..89a2e1a1f51 100644 --- a/misc/metrics/src/identify.rs +++ b/misc/metrics/src/identify.rs @@ -21,45 +21,15 @@ use crate::protocol_stack; use libp2p_identity::PeerId; use libp2p_swarm::StreamProtocol; -use once_cell::sync::Lazy; use prometheus_client::collector::Collector; -use prometheus_client::encoding::EncodeLabelSet; +use prometheus_client::encoding::{DescriptorEncoder, EncodeLabelSet, EncodeMetric}; use prometheus_client::metrics::counter::Counter; -use prometheus_client::metrics::family::ConstFamily; use prometheus_client::metrics::gauge::ConstGauge; -use prometheus_client::registry::{Descriptor, LocalMetric, Registry}; -use prometheus_client::MaybeOwned; -use std::borrow::Cow; +use prometheus_client::metrics::MetricType; +use prometheus_client::registry::Registry; use std::collections::HashMap; use std::sync::{Arc, Mutex}; -static PROTOCOLS_DESCRIPTOR: Lazy = Lazy::new(|| { - Descriptor::new( - "remote_protocols", - "Number of connected nodes supporting a specific protocol, with \"unrecognized\" for each peer supporting one or more unrecognized protocols", - None, - None, - vec![], - ) -}); -static LISTEN_ADDRESSES_DESCRIPTOR: Lazy = Lazy::new(|| { - Descriptor::new( - "remote_listen_addresses", - "Number of connected nodes advertising a specific listen address", - None, - None, - vec![], - ) -}); -static OBSERVED_ADDRESSES_DESCRIPTOR: Lazy = Lazy::new(|| { - Descriptor::new( - "local_observed_addresses", - "Number of connected nodes observing the local node at a specific address", - None, - None, - vec![], - ) -}); const ALLOWED_PROTOCOLS: &[StreamProtocol] = &[ #[cfg(feature = "dcutr")] libp2p_dcutr::PROTOCOL_NAME, @@ -188,10 +158,7 @@ impl Peers { } impl Collector for Peers { - fn collect<'a>( - &'a self, - ) -> Box, MaybeOwned<'a, Box>)> + 'a> - { + fn encode(&self, mut encoder: DescriptorEncoder) -> Result<(), std::fmt::Error> { let mut count_by_protocols: HashMap = Default::default(); let mut count_by_listen_addresses: HashMap = Default::default(); let mut count_by_observed_addresses: HashMap = Default::default(); @@ -241,40 +208,49 @@ impl Collector for Peers { } } - let count_by_protocols: Box = - Box::new(ConstFamily::new(count_by_protocols.into_iter().map( - |(protocol, count)| ([("protocol", protocol)], ConstGauge::new(count)), - ))); + { + let mut family_encoder = encoder.encode_descriptor( + "remote_protocols", + "Number of connected nodes supporting a specific protocol, with \"unrecognized\" for each peer supporting one or more unrecognized protocols", + None, + MetricType::Gauge, + )?; + for (protocol, count) in count_by_protocols.into_iter() { + let labels = [("protocol", protocol)]; + let metric_encoder = family_encoder.encode_family(&labels)?; + let metric = ConstGauge::new(count); + metric.encode(metric_encoder)?; + } + } - let count_by_listen_addresses: Box = - Box::new(ConstFamily::new(count_by_listen_addresses.into_iter().map( - |(protocol, count)| ([("listen_address", protocol)], ConstGauge::new(count)), - ))); + { + let mut family_encoder = encoder.encode_descriptor( + "remote_listen_addresses", + "Number of connected nodes advertising a specific listen address", + None, + MetricType::Gauge, + )?; + for (protocol, count) in count_by_listen_addresses.into_iter() { + let labels = [("listen_address", protocol)]; + let metric_encoder = family_encoder.encode_family(&labels)?; + ConstGauge::new(count).encode(metric_encoder)?; + } + } - let count_by_observed_addresses: Box = Box::new(ConstFamily::new( - count_by_observed_addresses - .into_iter() - .map(|(protocol, count)| { - ([("observed_address", protocol)], ConstGauge::new(count)) - }), - )); + { + let mut family_encoder = encoder.encode_descriptor( + "local_observed_addresses", + "Number of connected nodes observing the local node at a specific address", + None, + MetricType::Gauge, + )?; + for (protocol, count) in count_by_observed_addresses.into_iter() { + let labels = [("observed_address", protocol)]; + let metric_encoder = family_encoder.encode_family(&labels)?; + ConstGauge::new(count).encode(metric_encoder)?; + } + } - Box::new( - [ - ( - Cow::Borrowed(&*PROTOCOLS_DESCRIPTOR), - MaybeOwned::Owned(count_by_protocols), - ), - ( - Cow::Borrowed(&*LISTEN_ADDRESSES_DESCRIPTOR), - MaybeOwned::Owned(count_by_listen_addresses), - ), - ( - Cow::Borrowed(&*OBSERVED_ADDRESSES_DESCRIPTOR), - MaybeOwned::Owned(count_by_observed_addresses), - ), - ] - .into_iter(), - ) + Ok(()) } } From fd680c1edf8bd5c5b91f258fbc1ff927574caa55 Mon Sep 17 00:00:00 2001 From: Max Inden Date: Sat, 8 Jul 2023 15:05:19 +0900 Subject: [PATCH 2/5] Point to mxinden/client_rust:refactor-collector --- Cargo.lock | 7 ++----- Cargo.toml | 1 + 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cef33101b5f..6883f01135c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2839,7 +2839,6 @@ dependencies = [ "libp2p-ping", "libp2p-relay", "libp2p-swarm", - "once_cell", "prometheus-client", ] @@ -4176,8 +4175,7 @@ dependencies = [ [[package]] name = "prometheus-client" version = "0.21.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78c2f43e8969d51935d2a7284878ae053ba30034cd563f673cde37ba5205685e" +source = "git+https://github.com/mxinden/client_rust.git?branch=refactor-collector#f566b35a4f8107ab3483bb5c90a9507937724025" dependencies = [ "dtoa", "itoa", @@ -4188,8 +4186,7 @@ dependencies = [ [[package]] name = "prometheus-client-derive-encode" version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b6a5217beb0ad503ee7fa752d451c905113d70721b937126158f3106a48cc1" +source = "git+https://github.com/mxinden/client_rust.git?branch=refactor-collector#f566b35a4f8107ab3483bb5c90a9507937724025" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index db54152709f..af3b9c59ff9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -114,3 +114,4 @@ multihash = "0.19.0" # we import via `rust-multiaddr`. # This is expected to stay here until we move `libp2p-identity` to a separate repository which makes the dependency relationship more obvious. libp2p-identity = { path = "identity" } +prometheus-client = { git = 'https://github.com/mxinden/client_rust.git', branch = "refactor-collector" } From d17648cf76605076844d300b32992b55f8a8fee0 Mon Sep 17 00:00:00 2001 From: Max Inden Date: Wed, 12 Jul 2023 12:43:16 +0900 Subject: [PATCH 3/5] Update to latest commit on pull request --- Cargo.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6883f01135c..e667ed5f3ab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4174,8 +4174,8 @@ dependencies = [ [[package]] name = "prometheus-client" -version = "0.21.1" -source = "git+https://github.com/mxinden/client_rust.git?branch=refactor-collector#f566b35a4f8107ab3483bb5c90a9507937724025" +version = "0.21.2" +source = "git+https://github.com/mxinden/client_rust.git?branch=refactor-collector#9207512a47b8952055e00aac497eb230ba2fdd7c" dependencies = [ "dtoa", "itoa", @@ -4186,7 +4186,7 @@ dependencies = [ [[package]] name = "prometheus-client-derive-encode" version = "0.4.1" -source = "git+https://github.com/mxinden/client_rust.git?branch=refactor-collector#f566b35a4f8107ab3483bb5c90a9507937724025" +source = "git+https://github.com/mxinden/client_rust.git?branch=refactor-collector#9207512a47b8952055e00aac497eb230ba2fdd7c" dependencies = [ "proc-macro2", "quote", From a01fe502987f4e281386ff43022e9334bbfcd629 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Wed, 25 Oct 2023 14:13:29 +1100 Subject: [PATCH 4/5] Fix compile errors --- Cargo.lock | 11 +++++------ Cargo.toml | 9 +++++---- examples/metrics/Cargo.toml | 2 +- misc/metrics/Cargo.toml | 3 +-- misc/server/Cargo.toml | 2 +- protocols/gossipsub/Cargo.toml | 2 +- 6 files changed, 14 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c90ce3552f6..b91eaf8839b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4163,9 +4163,8 @@ dependencies = [ [[package]] name = "prometheus-client" -version = "0.21.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c99afa9a01501019ac3a14d71d9f94050346f55ca471ce90c799a15c58f61e2" +version = "0.22.0" +source = "git+https://github.com/prometheus/client_rust.git?branch=master#9d4ba09ec5bfc15522eb75145a5e850efd6fb628" dependencies = [ "dtoa", "itoa", @@ -4175,12 +4174,12 @@ dependencies = [ [[package]] name = "prometheus-client-derive-encode" -version = "0.4.1" -source = "git+https://github.com/mxinden/client_rust.git?branch=refactor-collector#9207512a47b8952055e00aac497eb230ba2fdd7c" +version = "0.4.2" +source = "git+https://github.com/prometheus/client_rust.git?branch=master#9d4ba09ec5bfc15522eb75145a5e850efd6fb628" dependencies = [ "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.38", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 4f66cc8d998..ed59aa334a3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -97,7 +97,6 @@ libp2p-pnet = { version = "0.24.0", path = "transports/pnet" } libp2p-quic = { version = "0.10.0", path = "transports/quic" } libp2p-relay = { version = "0.17.0", path = "protocols/relay" } libp2p-rendezvous = { version = "0.14.0", path = "protocols/rendezvous" } -libp2p-upnp = { version = "0.2.0", path = "protocols/upnp" } libp2p-request-response = { version = "0.26.0", path = "protocols/request-response" } libp2p-server = { version = "0.12.3", path = "misc/server" } libp2p-swarm = { version = "0.44.0", path = "swarm" } @@ -106,6 +105,7 @@ libp2p-swarm-test = { version = "0.3.0", path = "swarm-test" } libp2p-tcp = { version = "0.41.0", path = "transports/tcp" } libp2p-tls = { version = "0.3.0", path = "transports/tls" } libp2p-uds = { version = "0.40.0", path = "transports/uds" } +libp2p-upnp = { version = "0.2.0", path = "protocols/upnp" } libp2p-webrtc = { version = "0.6.1-alpha", path = "transports/webrtc" } libp2p-webrtc-utils = { version = "0.1.0", path = "misc/webrtc-utils" } libp2p-webrtc-websys = { version = "0.2.0-alpha", path = "transports/webrtc-websys" } @@ -113,12 +113,13 @@ libp2p-websocket = { version = "0.43.0", path = "transports/websocket" } libp2p-websocket-websys = { version = "0.3.0", path = "transports/websocket-websys" } libp2p-webtransport-websys = { version = "0.2.0", path = "transports/webtransport-websys" } libp2p-yamux = { version = "0.45.0", path = "muxers/yamux" } +multiaddr = "0.18.0" +multihash = "0.19.1" multistream-select = { version = "0.13.0", path = "misc/multistream-select" } +prometheus-client = "0.22.0" quick-protobuf-codec = { version = "0.2.0", path = "misc/quick-protobuf-codec" } quickcheck = { package = "quickcheck-ext", path = "misc/quickcheck-ext" } rw-stream-sink = { version = "0.4.0", path = "misc/rw-stream-sink" } -multiaddr = "0.18.0" -multihash = "0.19.1" [patch.crates-io] @@ -128,7 +129,7 @@ multihash = "0.19.1" # we import via `rust-multiaddr`. # This is expected to stay here until we move `libp2p-identity` to a separate repository which makes the dependency relationship more obvious. libp2p-identity = { path = "identity" } -prometheus-client = { git = 'https://github.com/mxinden/client_rust.git', branch = "refactor-collector" } +prometheus-client = { git = 'https://github.com/prometheus/client_rust.git', branch = "master" } [workspace.lints] rust.unreachable_pub = "warn" diff --git a/examples/metrics/Cargo.toml b/examples/metrics/Cargo.toml index 239e88bd424..b6431847514 100644 --- a/examples/metrics/Cargo.toml +++ b/examples/metrics/Cargo.toml @@ -15,7 +15,7 @@ hyper = { version = "0.14", features = ["server", "tcp", "http1"] } libp2p = { path = "../../libp2p", features = ["async-std", "metrics", "ping", "noise", "identify", "tcp", "yamux", "macros"] } log = "0.4.20" tokio = { version = "1", features = ["rt-multi-thread"] } -prometheus-client = "0.21.2" +prometheus-client = "0.22.0" [lints] workspace = true diff --git a/misc/metrics/Cargo.toml b/misc/metrics/Cargo.toml index 34f1733cd85..506f8a574ce 100644 --- a/misc/metrics/Cargo.toml +++ b/misc/metrics/Cargo.toml @@ -29,8 +29,7 @@ libp2p-kad = { workspace = true, optional = true } libp2p-ping = { workspace = true, optional = true } libp2p-relay = { workspace = true, optional = true } libp2p-swarm = { workspace = true } -libp2p-identity = { workspace = true } -prometheus-client = { version = "0.21.1"} +prometheus-client = { workspace = true } [dev-dependencies] libp2p-identity = { workspace = true, features = ["rand"] } diff --git a/misc/server/Cargo.toml b/misc/server/Cargo.toml index 4450be60217..ea5cf913e39 100644 --- a/misc/server/Cargo.toml +++ b/misc/server/Cargo.toml @@ -19,7 +19,7 @@ futures-timer = "3" hyper = { version = "0.14", features = ["server", "tcp", "http1"] } libp2p = { workspace = true, features = ["autonat", "dns", "tokio", "noise", "tcp", "yamux", "identify", "kad", "ping", "relay", "metrics", "rsa", "macros", "quic"] } log = "0.4" -prometheus-client = "0.21.2" +prometheus-client = { workspace = true } serde = "1.0.189" serde_derive = "1.0.125" serde_json = "1.0" diff --git a/protocols/gossipsub/Cargo.toml b/protocols/gossipsub/Cargo.toml index d3f715676ed..58ea50161a4 100644 --- a/protocols/gossipsub/Cargo.toml +++ b/protocols/gossipsub/Cargo.toml @@ -40,7 +40,7 @@ unsigned-varint = { version = "0.7.2", features = ["asynchronous_codec"] } void = "1.0.2" # Metrics dependencies -prometheus-client = "0.21.2" +prometheus-client = { workspace = true } [dev-dependencies] async-std = { version = "1.6.3", features = ["unstable"] } From 79c2393d15253ed0e1159bef47ce29a618906dc0 Mon Sep 17 00:00:00 2001 From: Max Inden Date: Wed, 25 Oct 2023 16:04:11 +0200 Subject: [PATCH 5/5] Update to released version --- Cargo.lock | 6 ++++-- Cargo.toml | 1 - examples/metrics/Cargo.toml | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d8641709558..61095bb524e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4180,7 +4180,8 @@ dependencies = [ [[package]] name = "prometheus-client" version = "0.22.0" -source = "git+https://github.com/prometheus/client_rust.git?branch=master#9d4ba09ec5bfc15522eb75145a5e850efd6fb628" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "510c4f1c9d81d556458f94c98f857748130ea9737bbd6053da497503b26ea63c" dependencies = [ "dtoa", "itoa", @@ -4191,7 +4192,8 @@ dependencies = [ [[package]] name = "prometheus-client-derive-encode" version = "0.4.2" -source = "git+https://github.com/prometheus/client_rust.git?branch=master#9d4ba09ec5bfc15522eb75145a5e850efd6fb628" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "440f724eba9f6996b75d63681b0a92b06947f1457076d503a4d2e2c8f56442b8" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 57ff23024f9..b9fa927f354 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -130,7 +130,6 @@ rw-stream-sink = { version = "0.4.0", path = "misc/rw-stream-sink" } # we import via `rust-multiaddr`. # This is expected to stay here until we move `libp2p-identity` to a separate repository which makes the dependency relationship more obvious. libp2p-identity = { path = "identity" } -prometheus-client = { git = 'https://github.com/prometheus/client_rust.git', branch = "master" } [workspace.lints] rust.unreachable_pub = "warn" diff --git a/examples/metrics/Cargo.toml b/examples/metrics/Cargo.toml index b6431847514..7d085662f2c 100644 --- a/examples/metrics/Cargo.toml +++ b/examples/metrics/Cargo.toml @@ -15,7 +15,7 @@ hyper = { version = "0.14", features = ["server", "tcp", "http1"] } libp2p = { path = "../../libp2p", features = ["async-std", "metrics", "ping", "noise", "identify", "tcp", "yamux", "macros"] } log = "0.4.20" tokio = { version = "1", features = ["rt-multi-thread"] } -prometheus-client = "0.22.0" +prometheus-client = { workspace = true } [lints] workspace = true