Help with collecting bandwidth stats using BandwidthTransport in Rust-libp2p
โ Rust ๐ 2025-07-31 ๐ค surdeus ๐๏ธ 12Hi 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
Swarmusing 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:
-
How exactly to access the bandwidth stats from
MetricsorBandwidthTransportafter the swarm is running. -
How to properly integrate this with
SwarmBuilder::with_bandwidth_metrics()if I want to use it. -
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
BandwidthTransportwithSwarmBuilder::with_bandwidth_metrics(). -
Exposing or logging bandwidth data during the swarm event loop.
Thanks in advance for any help!
1 post - 1 participant
๐ท๏ธ Rust_feed