diff --git a/Cargo.lock b/Cargo.lock index e7b67a47724..61095bb524e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2762,7 +2762,6 @@ dependencies = [ "libp2p-ping", "libp2p-relay", "libp2p-swarm", - "once_cell", "prometheus-client", ] @@ -4180,9 +4179,9 @@ dependencies = [ [[package]] name = "prometheus-client" -version = "0.21.2" +version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c99afa9a01501019ac3a14d71d9f94050346f55ca471ce90c799a15c58f61e2" +checksum = "510c4f1c9d81d556458f94c98f857748130ea9737bbd6053da497503b26ea63c" dependencies = [ "dtoa", "itoa", @@ -4192,13 +4191,13 @@ dependencies = [ [[package]] name = "prometheus-client-derive-encode" -version = "0.4.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b6a5217beb0ad503ee7fa752d451c905113d70721b937126158f3106a48cc1" +checksum = "440f724eba9f6996b75d63681b0a92b06947f1457076d503a4d2e2c8f56442b8" dependencies = [ "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.38", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 699cb0619cd..b9fa927f354 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -98,7 +98,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" } @@ -107,6 +106,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" } @@ -114,12 +114,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] diff --git a/examples/metrics/Cargo.toml b/examples/metrics/Cargo.toml index 239e88bd424..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.21.2" +prometheus-client = { workspace = true } [lints] workspace = true diff --git a/misc/metrics/Cargo.toml b/misc/metrics/Cargo.toml index 3f2845dafb8..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 } -once_cell = "1.18.0" -prometheus-client = { version = "0.21.2"} +prometheus-client = { workspace = true } [dev-dependencies] libp2p-identity = { workspace = true, features = ["rand"] } diff --git a/misc/metrics/src/identify.rs b/misc/metrics/src/identify.rs index 4e320e7b0a0..4dac6ea6774 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, @@ -187,10 +157,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(); @@ -240,40 +207,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(()) } } 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"] }