| Crates.io | tokio-metrics |
| lib.rs | tokio-metrics |
| version | 0.4.5 |
| created_at | 2022-02-17 22:23:55.336047+00 |
| updated_at | 2025-09-06 00:43:11.28488+00 |
| description | Runtime and task level metrics for Tokio applications. |
| homepage | https://tokio.rs |
| repository | https://github.com/tokio-rs/tokio-metrics |
| max_upload_size | |
| id | 534401 |
| size | 310,329 |
Provides utilities for collecting metrics from a Tokio application, including runtime and per-task metrics.
[dependencies]
tokio-metrics = { version = "0.4.5", default-features = false }
Use TaskMonitor to instrument tasks before spawning them, and to observe
metrics for those tasks. All tasks instrumented with a given TaskMonitor
aggregate their metrics together. To split out metrics for different tasks, use
separate TaskMetrics instances.
// construct a TaskMonitor
let monitor = tokio_metrics::TaskMonitor::new();
// print task metrics every 500ms
{
let frequency = std::time::Duration::from_millis(500);
let monitor = monitor.clone();
tokio::spawn(async move {
for metrics in monitor.intervals() {
println!("{:?}", metrics);
tokio::time::sleep(frequency).await;
}
});
}
// instrument some tasks and spawn them
loop {
tokio::spawn(monitor.instrument(do_work()));
}
instrumented_countdropped_countfirst_poll_counttotal_first_poll_delaytotal_idled_counttotal_idle_durationtotal_scheduled_counttotal_scheduled_durationtotal_poll_counttotal_poll_durationtotal_fast_poll_counttotal_fast_poll_durationtotal_slow_poll_counttotal_slow_poll_duration
The total duration of slow polls.total_short_delay_count
The total count of short scheduling delays.total_short_delay_duration
The total duration of short scheduling delays.total_long_delay_count
The total count of long scheduling delays.total_long_delay_duration
The total duration of long scheduling delays.mean_first_poll_delaymean_idle_durationmean_scheduled_durationmean_poll_durationslow_poll_ratiomean_fast_poll_durationmean_slow_poll_durationlong_delay_ratiomean_short_delay_duration
The mean duration of short schedules.mean_long_delay_duration
The mean duration of long schedules.Not all runtime metrics are stable. Using unstable metrics requires tokio_unstable, and the rt crate
feature. To enable tokio_unstable, the --cfg tokio_unstable must be passed
to rustc when compiling. You can do this by setting the RUSTFLAGS
environment variable before compiling your application; e.g.:
RUSTFLAGS="--cfg tokio_unstable" cargo build
Or, by creating the file .cargo/config.toml in the root directory of your crate.
If you're using a workspace, put this file in the root directory of your workspace instead.
[build]
rustflags = ["--cfg", "tokio_unstable"]
rustdocflags = ["--cfg", "tokio_unstable"]
Putting .cargo/config.toml files below the workspace or crate root directory may lead to tools like
Rust-Analyzer or VSCode not using your .cargo/config.toml since they invoke cargo from
the workspace or crate root and cargo only looks for the .cargo directory in the current & parent directories.
Cargo ignores configurations in child directories.
More information about where cargo looks for configuration files can be found
here.
Missing this configuration file during compilation will cause tokio-metrics to not work, and alternating between building with and without this configuration file included will cause full rebuilds of your project.
workers_counttotal_park_countmax_park_countmin_park_counttotal_busy_durationmax_busy_durationmin_busy_durationglobal_queue_depth]The rt feature of tokio-metrics is on by default; simply check that you do
not set default-features = false when declaring it as a dependency; e.g.:
[dependencies]
tokio-metrics = "0.4.5"
From within a Tokio runtime, use RuntimeMonitor to monitor key metrics of
that runtime.
let handle = tokio::runtime::Handle::current();
let runtime_monitor = tokio_metrics::RuntimeMonitor::new(&handle);
// print runtime metrics every 500ms
let frequency = std::time::Duration::from_millis(500);
tokio::spawn(async move {
for metrics in runtime_monitor.intervals() {
println!("Metrics = {:?}", metrics);
tokio::time::sleep(frequency).await;
}
});
// run some tasks
tokio::spawn(do_work());
tokio::spawn(do_work());
tokio::spawn(do_work());
If you also enable the metrics-rs-integration feature, you can use metrics.rs exporters to export metrics outside of your process. metrics.rs supports a variety of exporters, including Prometheus.
The exported metrics by default will be exported with their name, preceded by tokio_. For example, tokio_workers_count for the workers_count metric. This can be customized by using the with_metrics_tranformer function.
If you want to use Prometheus, you could have this Cargo.toml:
[dependencies]
tokio-metrics = { version = "0.4.5", features = ["metrics-rs-integration"] }
metrics = "0.24"
# You don't actually need to use the Prometheus exporter with uds-listener enabled,
# it's just here as an example.
metrics-exporter-prometheus = { version = "0.16", features = ["uds-listener"] }
Then, you can launch a metrics exporter:
// This makes metrics visible via a local Unix socket with name prometheus.sock
// You probably want to do it differently.
//
// If you use this exporter, you can access the metrics for debugging
// by running `curl --unix-socket prometheus.sock localhost`.
metrics_exporter_prometheus::PrometheusBuilder::new()
.with_http_uds_listener("prometheus.sock")
.install()
.unwrap();
// This line launches the reporter that monitors the Tokio runtime and exports the metrics.
tokio::task::spawn(
tokio_metrics::RuntimeMetricsReporterBuilder::default().describe_and_run(),
);
// run some tasks
tokio::spawn(do_work());
tokio::spawn(do_work());
tokio::spawn(do_work());
Of course, it will work with any other metrics.rs exporter.
workers_counttotal_park_countmax_park_countmin_park_counttotal_noop_countmax_noop_countmin_noop_counttotal_steal_countmax_steal_countmin_steal_counttotal_steal_operationsmax_steal_operationsmin_steal_operationsnum_remote_schedulestotal_local_schedule_countmax_local_schedule_countmin_local_schedule_counttotal_overflow_countmax_overflow_countmin_overflow_counttotal_polls_countmax_polls_countmin_polls_counttotal_busy_durationmax_busy_durationmin_busy_durationinjection_queue_depthtotal_local_queue_depthmax_local_queue_depthmin_local_queue_depthblocking_queue_depth
The number of tasks currently waiting to be executed in the blocking threadpool.live_tasks_count
The current number of alive tasks in the runtime.blocking_threads_count
The number of additional threads spawned by the runtime.idle_blocking_threads_count
The number of idle threads, which have spawned by the runtime for spawn_blocking calls.elapsedbudget_forced_yield_count
The number of times that a task was forced to yield because it exhausted its budget.io_driver_ready_count
The number of ready events received from the I/O driver.Currently, Tokio Console is primarily intended for local debugging. Tokio
metrics is intended to enable reporting of metrics in production to your
preferred tools. Longer term, it is likely that tokio-metrics will merge with
Tokio Console.
This project is licensed under the MIT license.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in tokio-metrics by you, shall be licensed as MIT, without any additional terms or conditions.