|
21 | 21 | use crate::protocol_stack; |
22 | 22 | use libp2p_identity::PeerId; |
23 | 23 | use libp2p_swarm::StreamProtocol; |
24 | | -use once_cell::sync::Lazy; |
25 | 24 | use prometheus_client::collector::Collector; |
26 | | -use prometheus_client::encoding::EncodeLabelSet; |
| 25 | +use prometheus_client::encoding::{DescriptorEncoder, EncodeLabelSet, EncodeMetric}; |
27 | 26 | use prometheus_client::metrics::counter::Counter; |
28 | | -use prometheus_client::metrics::family::ConstFamily; |
29 | 27 | 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; |
33 | 30 | use std::collections::HashMap; |
34 | 31 | use std::sync::{Arc, Mutex}; |
35 | 32 |
|
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 | | -}); |
63 | 33 | const ALLOWED_PROTOCOLS: &[StreamProtocol] = &[ |
64 | 34 | #[cfg(feature = "dcutr")] |
65 | 35 | libp2p_dcutr::PROTOCOL_NAME, |
@@ -187,10 +157,7 @@ impl Peers { |
187 | 157 | } |
188 | 158 |
|
189 | 159 | 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> { |
194 | 161 | let mut count_by_protocols: HashMap<String, i64> = Default::default(); |
195 | 162 | let mut count_by_listen_addresses: HashMap<String, i64> = Default::default(); |
196 | 163 | let mut count_by_observed_addresses: HashMap<String, i64> = Default::default(); |
@@ -240,40 +207,49 @@ impl Collector for Peers { |
240 | 207 | } |
241 | 208 | } |
242 | 209 |
|
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 | + } |
247 | 224 |
|
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 | + } |
252 | 238 |
|
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 | + } |
260 | 252 |
|
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(()) |
278 | 254 | } |
279 | 255 | } |
0 commit comments