Skip to content

Commit

Permalink
Add peer_id label to metric
Browse files Browse the repository at this point in the history
  • Loading branch information
pawanjay176 authored and jxs committed Jan 4, 2024
1 parent 3970a28 commit f4dad1b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
4 changes: 2 additions & 2 deletions protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2057,8 +2057,8 @@ where
// before we add all the gossip from this heartbeat in order to gain a true measure of

Check failure on line 2057 in protocols/gossipsub/src/behaviour.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/rust-libp2p/rust-libp2p/protocols/gossipsub/src/behaviour.rs
// steady-state size of the queues.
if let Some(m) = &mut self.metrics {
for sender_queue in self.connected_peers.values_mut().map(|v| &v.sender) {
m.observe_priority_queue_size(sender_queue.priority_len());
for (peer_id, sender_queue) in self.connected_peers.iter().map(|(k, v)| (k, &v.sender)) {
m.observe_priority_queue_size(sender_queue.priority_len(), peer_id);
m.observe_non_priority_queue_size(sender_queue.non_priority_len());
}
}
Expand Down
28 changes: 21 additions & 7 deletions protocols/gossipsub/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
use std::collections::HashMap;

use libp2p_identity::PeerId;
use prometheus_client::encoding::{EncodeLabelSet, EncodeLabelValue};
use prometheus_client::metrics::counter::Counter;
use prometheus_client::metrics::family::{Family, MetricConstructor};
use prometheus_client::metrics::gauge::Gauge;
use prometheus_client::metrics::histogram::{linear_buckets, Histogram};
use prometheus_client::metrics::histogram::{exponential_buckets, linear_buckets, Histogram};
use prometheus_client::registry::Registry;

use crate::topic::TopicHash;
Expand Down Expand Up @@ -179,8 +180,8 @@ pub(crate) struct Metrics {
/// topic. A very high metric might indicate an underperforming network.
topic_iwant_msgs: Family<TopicHash, Counter>,

/// The size of the priority queue.
priority_queue_size: Histogram,
/// The size of the priority queue for each PeerId.
priority_queue_size: Family<PeerIdLabel, Histogram, HistBuilder>,
/// The size of the non-priority queue.
non_priority_queue_size: Histogram,
}
Expand Down Expand Up @@ -321,10 +322,14 @@ impl Metrics {
metric
};

let priority_queue_size = Histogram::new(linear_buckets(0.0, 25.0, 100));
let hist_builder = HistBuilder {
buckets: exponential_buckets(10.0, 2.0, 10).collect(),
};
let priority_queue_size: Family<_, _, HistBuilder> =
Family::new_with_constructor(hist_builder);
registry.register(
"priority_queue_size",
"Histogram of observed priority queue sizes",
"Histogram of observed priority queue sizes for all PeerIds",
priority_queue_size.clone(),
);

Expand Down Expand Up @@ -558,8 +563,12 @@ impl Metrics {
}

/// Observes a priority queue size.
pub(crate) fn observe_priority_queue_size(&mut self, len: usize) {
self.priority_queue_size.observe(len as f64);
pub(crate) fn observe_priority_queue_size(&mut self, len: usize, peer_id: &PeerId) {
self.priority_queue_size
.get_or_create(&PeerIdLabel {
peer_id: peer_id.to_string(),
})
.observe(len as f64);
}

/// Observes a non-priority queue size.
Expand Down Expand Up @@ -660,6 +669,11 @@ struct PenaltyLabel {
penalty: Penalty,
}

#[derive(PartialEq, Eq, Hash, EncodeLabelSet, Clone, Debug)]
struct PeerIdLabel {
peer_id: String,
}

#[derive(Clone)]
struct HistBuilder {
buckets: Vec<f64>,
Expand Down

0 comments on commit f4dad1b

Please sign in to comment.