Struct InstrumentedFuture

Source
pub struct InstrumentedFuture<F: Future> { /* private fields */ }
Expand description

An instrumented Future.

InstrumentedFuture provides a transparent observability layer for futures. An instrumented future is not created directly. Rather, an instrumented future is created from an existing future using IntoInstrumentedFuture::into_instrumented_future.

Most importantly, the increment_until_resolved method allows callers to increment a GuardedGauge, and then decrement the gauge once the future has resolved.

§Examples

use prometheus_utils::IntoInstrumentedFuture;
use tokio::time::{sleep, Duration};

#[tokio::main]
async fn main() {
    let fut = sleep(Duration::from_millis(100));
    let instrumented = fut.into_instrumented_future();
    let _res = instrumented.await;
}

Implementations§

Source§

impl<F: Future> InstrumentedFuture<F>

Source

pub fn with_guard<GuardFn: FnOnce() -> Option<Box<dyn Any + Send>> + Send + 'static>( self, guard_fn: GuardFn, ) -> Self

Queue guard_fn to execute when the future is polled, retaining the returned value until the future completes.

§Examples
use prometheus_utils::IntoInstrumentedFuture;
use tokio::time::{sleep, Duration};

/// An RAII guard that will be attached to the future.
struct FutureGuard;

impl Drop for FutureGuard {
    fn drop(&mut self) {
        // This code will be run when the future is ready.
        println!("100ms have elapsed!");
    }
}

/// An asynchronous function.
async fn do_work() {
    sleep(Duration::from_millis(100)).await;
}

#[tokio::main]
async fn main() {
    do_work()
        .into_instrumented_future()
        .with_guard(|| {
            // This code will be run once the future is polled.
            println!("future was polled!");
            Some(Box::new(FutureGuard))
        })
        .await;
}
Source

pub fn with_count<P: Atomic + 'static>( self, counter: &'static GenericCounter<P>, ) -> Self

Increment a Prometheus counter immediately.

Source

pub fn with_count_labeled<C, L>(self, counter: &'static C, labels: L) -> Self
where C: Deref<Target = IntCounterWithLabels<L>> + Sync, L: Labels + Sync + Send + 'static,

Increment a labeled Prometheus counter when the future is polled.

Source

pub fn with_count_gauge<G, T, P>(self, gauge: &'static G) -> Self
where G: Deref<Target = T> + Sync, T: GuardedGauge<P> + 'static, P: Atomic + 'static,

Increment a Prometheus gauge until this future has resolved.

When called, this method will immediately increment the given gauge using the GuardedGauge::gaurded_inc trait method. This gauge will then be decremented once this future’s Future::poll implementation returns a Poll::Ready value.

See the GenericGaugeGuard documentation for more information about RAII guards for Prometheus metrics.

Trait Implementations§

Source§

impl<F: Future> Future for InstrumentedFuture<F>

Source§

type Output = <F as Future>::Output

An instrumented future returns the same type as its inner future.

Source§

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>

Polls the inner future.

If the inner future’s [Future::poll][fut-poll] implementation returns a [Poll::Ready][poll-ready] value, Prometheus gauges will be decremented accordingly.

Source§

impl<'pin, F: Future> Unpin for InstrumentedFuture<F>
where PinnedFieldsOf<__InstrumentedFuture<'pin, F>>: Unpin,

Auto Trait Implementations§

§

impl<F> Freeze for InstrumentedFuture<F>
where F: Freeze,

§

impl<F> !RefUnwindSafe for InstrumentedFuture<F>

§

impl<F> Send for InstrumentedFuture<F>
where F: Send,

§

impl<F> !Sync for InstrumentedFuture<F>

§

impl<F> !UnwindSafe for InstrumentedFuture<F>

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, 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<F> IntoFuture for F
where F: Future,

Source§

type Output = <F as Future>::Output

The output that the future will produce on completion.
Source§

type IntoFuture = F

Which kind of future are we turning this into?
Source§

fn into_future(self) -> <F as IntoFuture>::IntoFuture

Creates a future from a value. Read more
Source§

impl<F> IntoInstrumentedFuture for F
where F: Future,

Source§

type Future = F

The underlying to be instrumented.
Source§

fn into_instrumented_future(self) -> InstrumentedFuture<F>

Convert this future into an InstrumentedFuture.
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.