diff --git a/protocols/gossipsub/CHANGELOG.md b/protocols/gossipsub/CHANGELOG.md index 6cb7bd62031..c462a4dd448 100644 --- a/protocols/gossipsub/CHANGELOG.md +++ b/protocols/gossipsub/CHANGELOG.md @@ -1,4 +1,7 @@ ## 0.48.1 +- Allow whitelisting topics for metrics to ensure metrics are recorded correctly for these topics. + See [PR 5895](https://github.com/libp2p/rust-libp2p/pull/5895) + - Improve `max_messages_per_rpc` consistency by ensuring RPC control messages also adhere to the existing limits. See [PR 5826](https://github.com/libp2p/rust-libp2p/pull/5826) diff --git a/protocols/gossipsub/src/behaviour.rs b/protocols/gossipsub/src/behaviour.rs index ec2213e3083..7b7e6a80395 100644 --- a/protocols/gossipsub/src/behaviour.rs +++ b/protocols/gossipsub/src/behaviour.rs @@ -3053,6 +3053,13 @@ where } } } + + /// Register topics to ensure metrics are recorded correctly for these topics. + pub fn register_topics_for_metrics(&mut self, topics: Vec) { + if let Some(metrics) = &mut self.metrics { + metrics.register_allowed_topics(topics); + } + } } fn get_ip_addr(addr: &Multiaddr) -> Option { diff --git a/protocols/gossipsub/src/metrics.rs b/protocols/gossipsub/src/metrics.rs index 42dedc000b7..485bcd54eeb 100644 --- a/protocols/gossipsub/src/metrics.rs +++ b/protocols/gossipsub/src/metrics.rs @@ -648,6 +648,13 @@ impl Metrics { metric.set(metric.get() - 1); } } + + /// Registers a set of topics that we want to store calculate metrics for. + pub(crate) fn register_allowed_topics(&mut self, topics: Vec) { + for topic_hash in topics { + self.topic_info.insert(topic_hash, true); + } + } } /// Reasons why a peer was included in the mesh.