future-metrics

Crates.iofuture-metrics
lib.rsfuture-metrics
version0.1.0
sourcesrc
created_at2025-01-22 09:03:58.450069+00
updated_at2025-01-22 09:03:58.450069+00
descriptionInstrument futures with execution metrics
homepagehttps://github.com/heilhead/future-metrics.git
repositoryhttps://github.com/heilhead/future-metrics.git
max_upload_size
id1526441
size65,950
Ivan Reshetnikov (heilhead)

documentation

README

future-metrics

Instrument futures with execution metrics.

The primary use case is keeping track of the async tasks created and detached, as well as monitoring their execution to identify potential bottlenecks or async executor blocking.

Usage

Adding dependency:

[dependencies]
future-metrics = "0.1"

The very basic example of collecting execution stats from an async task:

use future_metrics::{ExecutionStats, FutureExt as _, Recorder};

struct MyRecorder;

impl Recorder for MyRecorder {
    fn task_created(&self) {
        // Future was created.
    }

    fn task_destroyed(&self, stats: ExecutionStats) {
        // Future was destroyed.
        println!("{stats:?}");
    }
}

#[tokio::main]
async fn main() {
    tokio::time::sleep(std::time::Duration::from_millis(300))
        .with_metrics(MyRecorder)
        .await;
}

The above would output something like this:

ExecutionStats { created: Instant { tv_sec: 9919, tv_nsec: 707589973 }, started: Some(Instant { tv_sec: 9919, tv_nsec: 707590083 }), finished: Some(Instant { tv_sec: 9920, tv_nsec: 9048028 }), poll_duration: 13.52µs, poll_duration_max: 9.48µs, poll_entries: 2 }

Which can be integrated with any metrics backend.

More examples can be found in examples/ directory.

License

Apache 2.0

Commit count: 0

cargo fmt