Help with collecting bandwidth stats using BandwidthTransport in Rust-libp2p

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!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.