Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(metrics/identify): update to new collector trait #4160

Merged
merged 7 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
1 change: 0 additions & 1 deletion misc/metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
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 @@ -188,10 +158,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 @@ -241,40 +208,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(())
}
}