Skip to content

Commit 7517934

Browse files
authored
refactor(metrics/identify): update to new collector trait
Updates to `prometheus-client` `Collector` trait refactor introduced with prometheus/client_rust#149. Pull-Request: #4160.
1 parent def98eb commit 7517934

File tree

7 files changed

+58
-83
lines changed

7 files changed

+58
-83
lines changed

Cargo.lock

Lines changed: 5 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ libp2p-pnet = { version = "0.24.0", path = "transports/pnet" }
9898
libp2p-quic = { version = "0.10.0", path = "transports/quic" }
9999
libp2p-relay = { version = "0.17.0", path = "protocols/relay" }
100100
libp2p-rendezvous = { version = "0.14.0", path = "protocols/rendezvous" }
101-
libp2p-upnp = { version = "0.2.0", path = "protocols/upnp" }
102101
libp2p-request-response = { version = "0.26.0", path = "protocols/request-response" }
103102
libp2p-server = { version = "0.12.3", path = "misc/server" }
104103
libp2p-swarm = { version = "0.44.0", path = "swarm" }
@@ -107,19 +106,21 @@ libp2p-swarm-test = { version = "0.3.0", path = "swarm-test" }
107106
libp2p-tcp = { version = "0.41.0", path = "transports/tcp" }
108107
libp2p-tls = { version = "0.3.0", path = "transports/tls" }
109108
libp2p-uds = { version = "0.40.0", path = "transports/uds" }
109+
libp2p-upnp = { version = "0.2.0", path = "protocols/upnp" }
110110
libp2p-webrtc = { version = "0.6.1-alpha", path = "transports/webrtc" }
111111
libp2p-webrtc-utils = { version = "0.1.0", path = "misc/webrtc-utils" }
112112
libp2p-webrtc-websys = { version = "0.2.0-alpha", path = "transports/webrtc-websys" }
113113
libp2p-websocket = { version = "0.43.0", path = "transports/websocket" }
114114
libp2p-websocket-websys = { version = "0.3.0", path = "transports/websocket-websys" }
115115
libp2p-webtransport-websys = { version = "0.2.0", path = "transports/webtransport-websys" }
116116
libp2p-yamux = { version = "0.45.0", path = "muxers/yamux" }
117+
multiaddr = "0.18.0"
118+
multihash = "0.19.1"
117119
multistream-select = { version = "0.13.0", path = "misc/multistream-select" }
120+
prometheus-client = "0.22.0"
118121
quick-protobuf-codec = { version = "0.2.0", path = "misc/quick-protobuf-codec" }
119122
quickcheck = { package = "quickcheck-ext", path = "misc/quickcheck-ext" }
120123
rw-stream-sink = { version = "0.4.0", path = "misc/rw-stream-sink" }
121-
multiaddr = "0.18.0"
122-
multihash = "0.19.1"
123124

124125
[patch.crates-io]
125126

examples/metrics/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ hyper = { version = "0.14", features = ["server", "tcp", "http1"] }
1515
libp2p = { path = "../../libp2p", features = ["async-std", "metrics", "ping", "noise", "identify", "tcp", "yamux", "macros"] }
1616
log = "0.4.20"
1717
tokio = { version = "1", features = ["rt-multi-thread"] }
18-
prometheus-client = "0.21.2"
18+
prometheus-client = { workspace = true }
1919

2020
[lints]
2121
workspace = true

misc/metrics/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ libp2p-kad = { workspace = true, optional = true }
2929
libp2p-ping = { workspace = true, optional = true }
3030
libp2p-relay = { workspace = true, optional = true }
3131
libp2p-swarm = { workspace = true }
32-
once_cell = "1.18.0"
33-
prometheus-client = { version = "0.21.2"}
32+
prometheus-client = { workspace = true }
3433

3534
[dev-dependencies]
3635
libp2p-identity = { workspace = true, features = ["rand"] }

misc/metrics/src/identify.rs

Lines changed: 45 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -21,45 +21,15 @@
2121
use crate::protocol_stack;
2222
use libp2p_identity::PeerId;
2323
use libp2p_swarm::StreamProtocol;
24-
use once_cell::sync::Lazy;
2524
use prometheus_client::collector::Collector;
26-
use prometheus_client::encoding::EncodeLabelSet;
25+
use prometheus_client::encoding::{DescriptorEncoder, EncodeLabelSet, EncodeMetric};
2726
use prometheus_client::metrics::counter::Counter;
28-
use prometheus_client::metrics::family::ConstFamily;
2927
use prometheus_client::metrics::gauge::ConstGauge;
30-
use prometheus_client::registry::{Descriptor, LocalMetric, Registry};
31-
use prometheus_client::MaybeOwned;
32-
use std::borrow::Cow;
28+
use prometheus_client::metrics::MetricType;
29+
use prometheus_client::registry::Registry;
3330
use std::collections::HashMap;
3431
use std::sync::{Arc, Mutex};
3532

36-
static PROTOCOLS_DESCRIPTOR: Lazy<Descriptor> = Lazy::new(|| {
37-
Descriptor::new(
38-
"remote_protocols",
39-
"Number of connected nodes supporting a specific protocol, with \"unrecognized\" for each peer supporting one or more unrecognized protocols",
40-
None,
41-
None,
42-
vec![],
43-
)
44-
});
45-
static LISTEN_ADDRESSES_DESCRIPTOR: Lazy<Descriptor> = Lazy::new(|| {
46-
Descriptor::new(
47-
"remote_listen_addresses",
48-
"Number of connected nodes advertising a specific listen address",
49-
None,
50-
None,
51-
vec![],
52-
)
53-
});
54-
static OBSERVED_ADDRESSES_DESCRIPTOR: Lazy<Descriptor> = Lazy::new(|| {
55-
Descriptor::new(
56-
"local_observed_addresses",
57-
"Number of connected nodes observing the local node at a specific address",
58-
None,
59-
None,
60-
vec![],
61-
)
62-
});
6333
const ALLOWED_PROTOCOLS: &[StreamProtocol] = &[
6434
#[cfg(feature = "dcutr")]
6535
libp2p_dcutr::PROTOCOL_NAME,
@@ -187,10 +157,7 @@ impl Peers {
187157
}
188158

189159
impl Collector for Peers {
190-
fn collect<'a>(
191-
&'a self,
192-
) -> Box<dyn Iterator<Item = (Cow<'a, Descriptor>, MaybeOwned<'a, Box<dyn LocalMetric>>)> + 'a>
193-
{
160+
fn encode(&self, mut encoder: DescriptorEncoder) -> Result<(), std::fmt::Error> {
194161
let mut count_by_protocols: HashMap<String, i64> = Default::default();
195162
let mut count_by_listen_addresses: HashMap<String, i64> = Default::default();
196163
let mut count_by_observed_addresses: HashMap<String, i64> = Default::default();
@@ -240,40 +207,49 @@ impl Collector for Peers {
240207
}
241208
}
242209

243-
let count_by_protocols: Box<dyn LocalMetric> =
244-
Box::new(ConstFamily::new(count_by_protocols.into_iter().map(
245-
|(protocol, count)| ([("protocol", protocol)], ConstGauge::new(count)),
246-
)));
210+
{
211+
let mut family_encoder = encoder.encode_descriptor(
212+
"remote_protocols",
213+
"Number of connected nodes supporting a specific protocol, with \"unrecognized\" for each peer supporting one or more unrecognized protocols",
214+
None,
215+
MetricType::Gauge,
216+
)?;
217+
for (protocol, count) in count_by_protocols.into_iter() {
218+
let labels = [("protocol", protocol)];
219+
let metric_encoder = family_encoder.encode_family(&labels)?;
220+
let metric = ConstGauge::new(count);
221+
metric.encode(metric_encoder)?;
222+
}
223+
}
247224

248-
let count_by_listen_addresses: Box<dyn LocalMetric> =
249-
Box::new(ConstFamily::new(count_by_listen_addresses.into_iter().map(
250-
|(protocol, count)| ([("listen_address", protocol)], ConstGauge::new(count)),
251-
)));
225+
{
226+
let mut family_encoder = encoder.encode_descriptor(
227+
"remote_listen_addresses",
228+
"Number of connected nodes advertising a specific listen address",
229+
None,
230+
MetricType::Gauge,
231+
)?;
232+
for (protocol, count) in count_by_listen_addresses.into_iter() {
233+
let labels = [("listen_address", protocol)];
234+
let metric_encoder = family_encoder.encode_family(&labels)?;
235+
ConstGauge::new(count).encode(metric_encoder)?;
236+
}
237+
}
252238

253-
let count_by_observed_addresses: Box<dyn LocalMetric> = Box::new(ConstFamily::new(
254-
count_by_observed_addresses
255-
.into_iter()
256-
.map(|(protocol, count)| {
257-
([("observed_address", protocol)], ConstGauge::new(count))
258-
}),
259-
));
239+
{
240+
let mut family_encoder = encoder.encode_descriptor(
241+
"local_observed_addresses",
242+
"Number of connected nodes observing the local node at a specific address",
243+
None,
244+
MetricType::Gauge,
245+
)?;
246+
for (protocol, count) in count_by_observed_addresses.into_iter() {
247+
let labels = [("observed_address", protocol)];
248+
let metric_encoder = family_encoder.encode_family(&labels)?;
249+
ConstGauge::new(count).encode(metric_encoder)?;
250+
}
251+
}
260252

261-
Box::new(
262-
[
263-
(
264-
Cow::Borrowed(&*PROTOCOLS_DESCRIPTOR),
265-
MaybeOwned::Owned(count_by_protocols),
266-
),
267-
(
268-
Cow::Borrowed(&*LISTEN_ADDRESSES_DESCRIPTOR),
269-
MaybeOwned::Owned(count_by_listen_addresses),
270-
),
271-
(
272-
Cow::Borrowed(&*OBSERVED_ADDRESSES_DESCRIPTOR),
273-
MaybeOwned::Owned(count_by_observed_addresses),
274-
),
275-
]
276-
.into_iter(),
277-
)
253+
Ok(())
278254
}
279255
}

misc/server/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ futures-timer = "3"
1919
hyper = { version = "0.14", features = ["server", "tcp", "http1"] }
2020
libp2p = { workspace = true, features = ["autonat", "dns", "tokio", "noise", "tcp", "yamux", "identify", "kad", "ping", "relay", "metrics", "rsa", "macros", "quic"] }
2121
log = "0.4"
22-
prometheus-client = "0.21.2"
22+
prometheus-client = { workspace = true }
2323
serde = "1.0.189"
2424
serde_derive = "1.0.125"
2525
serde_json = "1.0"

protocols/gossipsub/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ unsigned-varint = { version = "0.7.2", features = ["asynchronous_codec"] }
4040
void = "1.0.2"
4141

4242
# Metrics dependencies
43-
prometheus-client = "0.21.2"
43+
prometheus-client = { workspace = true }
4444

4545
[dev-dependencies]
4646
async-std = { version = "1.6.3", features = ["unstable"] }

0 commit comments

Comments
 (0)