Help with collecting bandwidth stats using BandwidthTransport in Rust-libp2p

โš“ Rust    ๐Ÿ“… 2025-07-31    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 12      

surdeus

Warning

This post was published 125 days ago. The information described in this article may have changed.

Hi all,

Iโ€™m working on a Rust project using rust-libp2p and trying to instrument my network with bandwidth metrics. Iโ€™m using the BandwidthTransport wrapper around my transport and have set up a prometheus_client::Registry to collect metrics.

Here is what I have so far:

  • I create my base transport (TCP + Noise + Yamux).

  • I wrap it with BandwidthTransport::new(transport, &mut registry).

  • I then create a Swarm using this bandwidth-wrapped transport.

My goal is to periodically read bandwidth stats (sent/received bytes, rates, etc.) from the Metrics associated with the BandwidthTransport and output them (e.g., log or expose via Prometheus).

Iโ€™m struggling with:

  1. How exactly to access the bandwidth stats from Metrics or BandwidthTransport after the swarm is running.

  2. How to properly integrate this with SwarmBuilder::with_bandwidth_metrics() if I want to use it.

  3. Any examples or idiomatic ways to expose or print bandwidth usage in runtime.

I havenโ€™t found clear examples or docs on how to read and output these bandwidth metrics at runtime.

Could anyone provide guidance, example snippets, or best practices for:

  • Accessing current bandwidth stats from the swarm or metrics instance.

  • Integrating BandwidthTransport with SwarmBuilder::with_bandwidth_metrics().

  • Exposing or logging bandwidth data during the swarm event loop.

Thanks in advance for any help!

1 post - 1 participant

Read full topic

๐Ÿท๏ธ Rust_feed