Skip to main content

FutureMonitor

Struct FutureMonitor 

Source
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

Source

pub fn new() -> FutureMonitor

Constructs a new FutureMonitor for a single future.

Source

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).

Source

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.

Trait Implementations§

Source§

impl Debug for FutureMonitor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for FutureMonitor

Source§

fn default() -> FutureMonitor

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more