examples/metrics/README.md
The example showcases how to run a p2p network with libp2p and collect metrics using libp2p-metrics as well as span data via opentelemetry.
It sets up multiple nodes in the network and measures various metrics, such as libp2p_ping, to evaluate the network's performance.
To run the example, follow these steps:
Run the following command to start the first node:
cargo run
Open a second terminal and run the following command to start a second node:
cargo run -- <listen-addr-of-first-node>
Replace <listen-addr-of-first-node> with the listen address of the first node reported in the first terminal.
Look for the line that says NewListenAddr to find the address.
Open a third terminal and run the following command to retrieve the metrics from either the first or second node:
curl localhost:<metrics-port-of-first-or-second-node>/metrics
Replace <metrics-port-of-first-or-second-node> with the listen port of the metrics server of either the first or second node.
Look for the line that says tide::server Server listening on to find the port.
After executing the command, you should see a long list of metrics printed to the terminal.
Make sure to check the libp2p_ping metrics, which should have a value greater than zero (>0).
To see the span data collected as part of the Swarms activity, start up an opentelemetry collector:
docker compose up
Then, configure tracing to output spans:
export RUST_LOG=info,[ConnectionHandler::poll]=trace,[NetworkBehaviour::poll]=trace
Next, (re)-start the two example for it to connect to the OTEL collector. Finally, open the Jaeger UI in a browser and explore the spans: http://localhost:16686.
For a precise documentation, please see the following documentation in tracing: https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#directives.
rust-libp2p consistently applies spans to the following functions:
ConnectionHandler::poll implementationsNetworkBehaviour::poll implementationsThe above spans are all called exactly that: ConnectionHandler::poll and NetworkBehaviour::poll.
You can activate all of them by setting:
RUST_LOG=[ConnectionHandler::poll]=trace
If you just wanted to see the spans of the libp2p_ping crate, you can filter like this:
RUST_LOG=libp2p_ping[ConnectionHandler::poll]=trace
This example demonstrates how to utilize the libp2p-metrics crate to collect and analyze metrics in a libp2p network.
By running multiple nodes and examining the metrics, users can gain insights into the network's performance and behavior.