From 39951f1e442e97b19706b5d258489296a9b4eee7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Mon, 3 Nov 2025 11:41:35 +0000 Subject: [PATCH 1/2] feat(gossipsub): record the bytes received from last gossip message for each topic --- protocols/gossipsub/CHANGELOG.md | 5 ++- protocols/gossipsub/src/behaviour.rs | 2 +- protocols/gossipsub/src/metrics.rs | 60 +++++++++++++++++++++++++--- 3 files changed, 60 insertions(+), 7 deletions(-) diff --git a/protocols/gossipsub/CHANGELOG.md b/protocols/gossipsub/CHANGELOG.md index b143b72c15c..821580d0ac8 100644 --- a/protocols/gossipsub/CHANGELOG.md +++ b/protocols/gossipsub/CHANGELOG.md @@ -1,4 +1,7 @@ ## 0.50.0 +- Add extra metrics for bytes received and sent, filtered and unfiltered for each topic. + See [PR XXXX](https://github.com/libp2p/rust-libp2p/pull/XXXX) + - Prevent mesh exceeding mesh_n_high. See [PR 6184](https://github.com/libp2p/rust-libp2p/pull/6184) @@ -19,7 +22,7 @@ - Fix incorrect default values in ConfigBuilder See [PR 6113](https://github.com/libp2p/rust-libp2p/pull/6113) - + - Remove duplicated config `set_topic_max_transmit_size` method, prefer `max_transmit_size_for_topic`. See [PR 6173](https://github.com/libp2p/rust-libp2p/pull/6173). diff --git a/protocols/gossipsub/src/behaviour.rs b/protocols/gossipsub/src/behaviour.rs index 3d516098a7c..50a96acb609 100644 --- a/protocols/gossipsub/src/behaviour.rs +++ b/protocols/gossipsub/src/behaviour.rs @@ -1805,7 +1805,7 @@ where // Record the received message with the metrics #[cfg(feature = "metrics")] if let Some(metrics) = self.metrics.as_mut() { - metrics.msg_recvd(&message.topic); + metrics.msg_recvd(&message.topic, raw_message.raw_protobuf_len()); } // Tells score that message arrived (but is maybe not fully validated yet). diff --git a/protocols/gossipsub/src/metrics.rs b/protocols/gossipsub/src/metrics.rs index 1394d9a92a7..ed6e3d4a124 100644 --- a/protocols/gossipsub/src/metrics.rs +++ b/protocols/gossipsub/src/metrics.rs @@ -148,6 +148,8 @@ pub(crate) struct Metrics { topic_msg_sent_counts: Family, /// Bytes from gossip messages sent to each topic. topic_msg_sent_bytes: Family, + /// Bytes from the last gossip messages sent to each topic. + topic_msg_last_sent_bytes: Family, /// Number of gossipsub messages published to each topic. topic_msg_published: Family, @@ -155,8 +157,14 @@ pub(crate) struct Metrics { topic_msg_recv_counts_unfiltered: Family, /// Number of gossipsub messages received on each topic (after filtering duplicates). topic_msg_recv_counts: Family, - /// Bytes received from gossip messages for each topic. + /// Bytes received from gossip messages for each topic (after filtering duplicates). topic_msg_recv_bytes: Family, + /// Bytes received from last gossip message for each topic (after filtering duplicates). + topic_msg_last_recv_bytes: Family, + /// Bytes received from gossip messages for each topic (without filtering duplicates). + topic_msg_recv_bytes_unfiltered: Family, + /// Bytes received from last gossip message for each topic (without filtering duplicates). + topic_msg_last_recv_bytes_unfiltered: Family, // Metrics related to scoring /// Histogram of the scores for each mesh topic. @@ -248,25 +256,35 @@ impl Metrics { "mesh_peer_counts", "Number of peers in each topic in our mesh" ); + let mesh_peer_inclusion_events = register_family!( "mesh_peer_inclusion_events", "Number of times a peer gets added to our mesh for different reasons" ); + let mesh_peer_churn_events = register_family!( "mesh_peer_churn_events", "Number of times a peer gets removed from our mesh for different reasons" ); + let topic_msg_sent_counts = register_family!( "topic_msg_sent_counts", "Number of gossip messages sent to each topic" ); + let topic_msg_published = register_family!( "topic_msg_published", "Number of gossip messages published to each topic" ); + let topic_msg_sent_bytes = register_family!( "topic_msg_sent_bytes", - "Bytes from gossip messages sent to each topic" + "bytes from gossip messages sent to each topic (after duplicates being filtered)" + ); + + let topic_msg_last_sent_bytes = register_family!( + "topic_msg_sent_bytes", + "bytes from the last gossip message sent to each topic (after duplicates being filtered)" ); let topic_msg_recv_counts_unfiltered = register_family!( @@ -278,9 +296,25 @@ impl Metrics { "topic_msg_recv_counts", "Number of gossip messages received on each topic (after duplicates have been 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 being filtered)" + ); + + let topic_msg_last_recv_bytes = register_family!( + "topic_msg_last_recv_bytes_unfiltered", + "Bytes received from last gossip message for each topic (after duplicates being filtered)" + ); + + let topic_msg_recv_bytes_unfiltered = register_family!( + "topic_msg_recv_bytes", + "Bytes received from gossip messages for each topic (without duplicates being filtered)" + ); + + let topic_msg_last_recv_bytes_unfiltered = register_family!( + "topic_msg_last_recv_bytes_unfiltered", + "Bytes received from last gossip message for each topic (without duplicates being filtered)" ); let hist_builder = HistBuilder { @@ -390,10 +424,14 @@ impl Metrics { mesh_peer_churn_events, topic_msg_sent_counts, topic_msg_sent_bytes, + topic_msg_last_sent_bytes, topic_msg_published, topic_msg_recv_counts_unfiltered, topic_msg_recv_counts, topic_msg_recv_bytes, + topic_msg_last_recv_bytes, + topic_msg_recv_bytes_unfiltered, + topic_msg_last_recv_bytes_unfiltered, score_per_mesh, scoring_penalties, peers_per_protocol, @@ -532,13 +570,22 @@ impl Metrics { self.topic_msg_sent_bytes .get_or_create(topic) .inc_by(bytes as u64); + self.topic_msg_last_sent_bytes + .get_or_create(topic) + .set(bytes as i64); } } /// 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); + self.topic_msg_last_recv_bytes + .get_or_create(topic) + .set(bytes as i64); } } @@ -548,9 +595,12 @@ 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); + self.topic_msg_last_recv_bytes_unfiltered + .get_or_create(topic) + .set(bytes as i64); } } From 40439c24639fa64d25fb718d40b1e4054784ed73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Tue, 4 Nov 2025 11:34:06 +0000 Subject: [PATCH 2/2] address review --- protocols/gossipsub/CHANGELOG.md | 2 +- protocols/gossipsub/src/metrics.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/protocols/gossipsub/CHANGELOG.md b/protocols/gossipsub/CHANGELOG.md index 821580d0ac8..1cb0400e72c 100644 --- a/protocols/gossipsub/CHANGELOG.md +++ b/protocols/gossipsub/CHANGELOG.md @@ -1,6 +1,6 @@ ## 0.50.0 - Add extra metrics for bytes received and sent, filtered and unfiltered for each topic. - See [PR XXXX](https://github.com/libp2p/rust-libp2p/pull/XXXX) + See [PR 6192](https://github.com/libp2p/rust-libp2p/pull/6192) - Prevent mesh exceeding mesh_n_high. See [PR 6184](https://github.com/libp2p/rust-libp2p/pull/6184) diff --git a/protocols/gossipsub/src/metrics.rs b/protocols/gossipsub/src/metrics.rs index ed6e3d4a124..8b915fc882c 100644 --- a/protocols/gossipsub/src/metrics.rs +++ b/protocols/gossipsub/src/metrics.rs @@ -303,12 +303,12 @@ impl Metrics { ); let topic_msg_last_recv_bytes = register_family!( - "topic_msg_last_recv_bytes_unfiltered", + "topic_msg_last_recv_bytes", "Bytes received from last gossip message for each topic (after duplicates being filtered)" ); let topic_msg_recv_bytes_unfiltered = register_family!( - "topic_msg_recv_bytes", + "topic_msg_recv_bytes_unfiltered", "Bytes received from gossip messages for each topic (without duplicates being filtered)" );