pub struct FutureMonitor { /* private fields */ }Expand description
Monitors the metrics of a single future running within a larger, already-instrumented task.
TaskMonitor aggregates metrics across all the futures it instruments;
FutureMonitor instead captures the metrics of one future so they can be
attached to that future’s own record. Idle, poll, and first-poll metrics are
measured locally from the future. Scheduling delay — which only the
root future the runtime schedules can observe — is read from the surrounding
task’s TaskScheduling log over the future’s lifetime, so for scheduling
metrics to be populated the surrounding task must itself be instrumented with
TaskMonitor::instrument.
FutureMonitor instruments exactly one future: instrument consumes the
monitor, so it cannot be reused, and the returned future resolves to the
wrapped output together with the captured FutureMetrics.
§Examples
use tokio_metrics::{FutureMonitor, TaskMonitor};
#[tokio::main]
async fn main() {
// the larger task is instrumented once; `publish_scheduling_delay`
// enables per-future scheduling-delay capture
let mut builder = TaskMonitor::builder();
builder.publish_scheduling_delay();
let task_monitor = builder.build();
task_monitor.instrument(async {
// each unit of work within the task is measured on its own
let (_output, metrics) = FutureMonitor::new()
.instrument(async {
tokio::task::yield_now().await;
})
.await;
assert!(metrics.poll_count >= 1);
}).await;
}Implementations§
Source§impl FutureMonitor
impl FutureMonitor
Sourcepub fn new() -> FutureMonitor
pub fn new() -> FutureMonitor
Constructs a new FutureMonitor for a single future.
Sourcepub fn with_slow_poll_threshold(slow_poll_threshold: Duration) -> FutureMonitor
pub fn with_slow_poll_threshold(slow_poll_threshold: Duration) -> FutureMonitor
Constructs a new FutureMonitor whose local poll metrics use a custom
slow-poll threshold (see TaskMonitor::with_slow_poll_threshold).
Sourcepub fn instrument<F>(self, task: F) -> MonitoredFuture<F> ⓘ
pub fn instrument<F>(self, task: F) -> MonitoredFuture<F> ⓘ
Instruments the future, consuming the monitor.
Taking self by value means a FutureMonitor can only instrument one
future. Await the returned MonitoredFuture to get the future’s
output alongside its FutureMetrics.