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

Add metric 'Number of bytes received from each topic (deduplicated)' #6431

Open
wants to merge 9 commits into
base: unstable
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -1837,7 +1837,7 @@ where

// Record the received message with the metrics
if let Some(metrics) = self.metrics.as_mut() {
metrics.msg_recvd(&message.topic);
metrics.msg_recvd(&message.topic, message.get_size());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you use get_size() and above we use raw_protobuf_len what motivates the difference?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use raw_protobuf_len to obtain the byte length of received message. Message is transformed data from RawMessage and is missing some fields compared to RawMessage, so message.get_size() returns a smaller byte size than the actually received bytes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah the get_size function is unrequired we can just use raw_protobuf_len

}

// Consider the message as delivered for gossip promises.
Expand Down
18 changes: 14 additions & 4 deletions beacon_node/lighthouse_network/gossipsub/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ pub(crate) struct Metrics {
topic_msg_recv_counts_unfiltered: Family<TopicHash, Counter>,
/// Number of gossipsub messages received on each topic (after filtering duplicates).
topic_msg_recv_counts: Family<TopicHash, Counter>,
/// Bytes received from gossip messages for each topic.
/// Bytes received from gossip messages for each topic (without filtering duplicates).
topic_msg_recv_bytes_unfiltered: Family<TopicHash, Counter>,
/// Bytes received from gossip messages for each topic (after filtering duplicates).
topic_msg_recv_bytes: Family<TopicHash, Counter>,

/* Metrics related to scoring */
Expand Down Expand Up @@ -281,9 +283,13 @@ impl Metrics {
"topic_msg_recv_counts",
"Number of gossip messages received on each topic (after duplicates have been filtered)"
);
let topic_msg_recv_bytes_unfiltered = register_family!(
"topic_msg_recv_bytes_unfiltered",
"Bytes received from gossip messages for each topic (without duplicates being filtered)"
);
let topic_msg_recv_bytes = register_family!(
"topic_msg_recv_bytes",
"Bytes received from gossip messages for each topic"
"Bytes received from gossip messages for each topic (after duplicates have been filtered)"
);

let hist_builder = HistBuilder {
Expand Down Expand Up @@ -382,6 +388,7 @@ impl Metrics {
topic_msg_published,
topic_msg_recv_counts_unfiltered,
topic_msg_recv_counts,
topic_msg_recv_bytes_unfiltered,
topic_msg_recv_bytes,
score_per_mesh,
scoring_penalties,
Expand Down Expand Up @@ -545,9 +552,12 @@ impl Metrics {
}

/// Register that a message was received (and was not a duplicate).
pub(crate) fn msg_recvd(&mut self, topic: &TopicHash) {
pub(crate) fn msg_recvd(&mut self, topic: &TopicHash, bytes: usize) {
if self.register_topic(topic).is_ok() {
self.topic_msg_recv_counts.get_or_create(topic).inc();
self.topic_msg_recv_bytes
.get_or_create(topic)
.inc_by(bytes as u64);
}
}

Expand All @@ -557,7 +567,7 @@ impl Metrics {
self.topic_msg_recv_counts_unfiltered
.get_or_create(topic)
.inc();
self.topic_msg_recv_bytes
self.topic_msg_recv_bytes_unfiltered
.get_or_create(topic)
.inc_by(bytes as u64);
}
Expand Down
4 changes: 4 additions & 0 deletions beacon_node/lighthouse_network/gossipsub/src/topic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ impl TopicHash {
pub fn as_str(&self) -> &str {
&self.hash
}

pub fn hash_byte_len(&self) -> usize {
self.hash.as_bytes().len()
}
Comment on lines +86 to +88
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we are only using this once and it's a one liner, no need for a function to abstract the one liner

}

/// A gossipsub topic.
Expand Down
16 changes: 16 additions & 0 deletions beacon_node/lighthouse_network/gossipsub/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use hashlink::LinkedHashMap;
use libp2p::identity::PeerId;
use libp2p::swarm::ConnectionId;
use prometheus_client::encoding::EncodeLabelValue;
use quick_protobuf::sizeofs::*;
use quick_protobuf::MessageWrite;
use std::collections::BTreeSet;
use std::fmt::Debug;
Expand Down Expand Up @@ -237,6 +238,21 @@ impl fmt::Debug for Message {
}
}

/// The byte size of a message
impl Message {
pub(crate) fn get_size(&self) -> usize {
self.source
.as_ref()
.map_or(0, |m| 1 + sizeof_len(m.to_bytes().len()))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PeerID has a fixed length by definition, we could just precompute it

+ sizeof_len(self.data.len())
+ self
.sequence_number
.as_ref()
.map_or(0, |m| 1 + sizeof_varint(*(m)))
+ sizeof_len(self.topic.hash_byte_len())
}
}

/// A subscription received by the gossipsub system.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Subscription {
Expand Down