Provides utilities for collecting metrics from a Tokio application, including runtime and per-task metrics.
toml
[dependencies]
tokio-metrics = { version = "0.2.2", 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.
```rust // 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_count
] dropped_count
] first_poll_count
] total_first_poll_delay
] total_idled_count
] total_idle_duration
] total_scheduled_count
] total_scheduled_duration
] total_poll_count
] total_poll_duration
] total_fast_poll_count
] total_fast_poll_duration
] total_slow_poll_count
] total_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_delay
] mean_idle_duration
] mean_scheduled_duration
] mean_poll_duration
] slow_poll_ratio
] mean_fast_poll_duration
] mean_slow_poll_duration
] long_delay_ratio
]mean_short_delay_duration
]
The mean duration of short schedules.mean_long_delay_duration
]
The mean duration of long schedules.This unstable functionality 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.:
sh
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.
toml
[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.
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.:
toml
[dependencies]
tokio-metrics = "0.2.2"
From within a Tokio runtime, use RuntimeMonitor
to monitor key metrics of
that runtime.
```rust
let handle = tokio::runtime::Handle::current();
let runtimemonitor = tokiometrics::RuntimeMonitor::new(&handle);
// print runtime metrics every 500ms let frequency = std::time::Duration::frommillis(500); tokio::spawn(async move { for metrics in runtimemonitor.intervals() { println!("Metrics = {:?}", metrics); tokio::time::sleep(frequency).await; } });
// run some tasks tokio::spawn(dowork()); tokio::spawn(dowork()); tokio::spawn(do_work()); ```
workers_count
] total_park_count
] max_park_count
] min_park_count
] total_noop_count
] max_noop_count
] min_noop_count
] total_steal_count
] max_steal_count
] min_steal_count
] total_steal_operations
] max_steal_operations
] min_steal_operations
] num_remote_schedules
] total_local_schedule_count
] max_local_schedule_count
] min_local_schedule_count
] total_overflow_count
] max_overflow_count
] min_overflow_count
] total_polls_count
] max_polls_count
] min_polls_count
] total_busy_duration
] max_busy_duration
] min_busy_duration
] injection_queue_depth
] total_local_queue_depth
] max_local_queue_depth
] min_local_queue_depth
] elapsed
] budget_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.mean_polls_per_park
]busy_ratio
]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.