Skip to content

Commit

Permalink
refactor(metrics/identify): update to new collector trait
Browse files Browse the repository at this point in the history
Updates to `prometheus-client` `Collector` trait refactor introduced with prometheus/client_rust#149.

Pull-Request: #4160.
  • Loading branch information
mxinden authored Oct 25, 2023
1 parent def98eb commit 7517934
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 83 deletions.
11 changes: 5 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -107,19 +106,21 @@ 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" }
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]

Expand Down
2 changes: 1 addition & 1 deletion examples/metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions misc/metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
114 changes: 45 additions & 69 deletions misc/metrics/src/identify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Descriptor> = 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<Descriptor> = Lazy::new(|| {
Descriptor::new(
"remote_listen_addresses",
"Number of connected nodes advertising a specific listen address",
None,
None,
vec![],
)
});
static OBSERVED_ADDRESSES_DESCRIPTOR: Lazy<Descriptor> = 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,
Expand Down Expand Up @@ -187,10 +157,7 @@ impl Peers {
}

impl Collector for Peers {
fn collect<'a>(
&'a self,
) -> Box<dyn Iterator<Item = (Cow<'a, Descriptor>, MaybeOwned<'a, Box<dyn LocalMetric>>)> + 'a>
{
fn encode(&self, mut encoder: DescriptorEncoder) -> Result<(), std::fmt::Error> {
let mut count_by_protocols: HashMap<String, i64> = Default::default();
let mut count_by_listen_addresses: HashMap<String, i64> = Default::default();
let mut count_by_observed_addresses: HashMap<String, i64> = Default::default();
Expand Down Expand Up @@ -240,40 +207,49 @@ impl Collector for Peers {
}
}

let count_by_protocols: Box<dyn LocalMetric> =
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<dyn LocalMetric> =
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<dyn LocalMetric> = 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(())
}
}
2 changes: 1 addition & 1 deletion misc/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion protocols/gossipsub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down

0 comments on commit 7517934

Please sign in to comment.