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 037a3c5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
6 changes: 3 additions & 3 deletions protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2057,9 +2057,9 @@ where
// before we add all the gossip from this heartbeat in order to gain a true measure of
// 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());
m.observe_non_priority_queue_size(sender_queue.non_priority_len());
for (peer_id, peer) in self.connected_peers.iter() {
m.observe_priority_queue_size(peer.sender.priority_len(), peer_id);
m.observe_non_priority_queue_size(peer.sender.non_priority_len(), peer_id);
}
}

Expand Down
39 changes: 28 additions & 11 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,10 +180,10 @@ 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,
non_priority_queue_size: Family<PeerIdLabel, Histogram, HistBuilder>,
}

impl Metrics {
Expand Down Expand Up @@ -321,14 +322,17 @@ 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::new_with_constructor(hist_builder.clone());
registry.register(
"priority_queue_size",
"Histogram of observed priority queue sizes",
"Histogram of observed priority queue sizes for all PeerIds",
priority_queue_size.clone(),
);

let non_priority_queue_size = Histogram::new(linear_buckets(0.0, 25.0, 100));
let non_priority_queue_size = Family::new_with_constructor(hist_builder);
registry.register(
"non_priority_queue_size",
"Histogram of observed non-priority queue sizes",
Expand Down Expand Up @@ -558,13 +562,21 @@ 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.
pub(crate) fn observe_non_priority_queue_size(&mut self, len: usize) {
self.non_priority_queue_size.observe(len as f64);
pub(crate) fn observe_non_priority_queue_size(&mut self, len: usize, peer_id: &PeerId) {
self.non_priority_queue_size
.get_or_create(&PeerIdLabel {
peer_id: peer_id.to_string(),
})
.observe(len as f64);
}

/// Observe a score of a mesh peer.
Expand Down Expand Up @@ -660,6 +672,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 037a3c5

Please sign in to comment.