Expand description
timeout-tracing allows executing an async function with a timeout (much like tokio::time::timeout).
When the timeout elapses it returns the exact location where the async code was awaiting at that specific moment.
§Basic usage
The basic usage looks as follows:
use std::time::Duration;
use timeout_tracing::{CaptureSpanTrace, timeout};
use tokio::time::sleep;
use tracing::instrument;
use tracing_error::ErrorLayer;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
#[tokio::main]
async fn main() {
// (1)
tracing_subscriber::registry()
.with(ErrorLayer::default())
.init();
// (2)
match timeout(Duration::from_secs(1), CaptureSpanTrace, computation(25)).await {
Ok(()) => println!("Completed"),
Err(elapsed) => println!("{elapsed}"), // (4)
}
}
// (5)
#[instrument]
async fn computation(n: i32) {
for i in 0..n {
step(i).await;
}
}
#[instrument]
async fn step(i: i32) {
sleep(Duration::from_millis(100)).await;
}This prints out:
timeout elapsed at:
trace 0:
0: basic::step
with i=9
at examples/basic.rs:31
1: basic::computation
with n=25
at examples/basic.rs:24tracing-errormust be initialized, as it is used (by default) to gather span traces.timeout_tracing::timeoutexecutes the future with a timeoutCaptureSpanTraceis the object that captures the stack. The default implementation captures span trace (viatracing-error). It is also possible to capture a stack trace as well usingCaptureSpanAndStackTrace(via Rust standard library; theRUST_BACKTRACE=1environment variable must be set for stack trace capture to work)- If the future does not complete within the given time limit, an error is returned. It contains a set of traces for each active leaf await point within the future.
- The executed functions should be instrumented with
tokio-tracingspans (for example, by using the#[tokio-tracing::instrument]macro) for span trace to work.
Structs§
- Capture
Span AndStack Trace - Implementation of
CaptureTracethat captures both a span trace usingtracing_error::SpanTraceand a stack trace.tracingmust be initialized withtracing_error::ErrorLayerfor the span trace to be captured successfully andRUST_BACKTRACEenvironment variable must be set for the stack trace to be captured. - Capture
Span Trace - Implementation of
CaptureTracethat captures span trace usingtracing_error::SpanTrace.tracingmust be initialized withtracing_error::ErrorLayerfor the trace to be captured successfully. - Stack
AndSpan Trace - Timeout
Elapsed - Timeout
Future
Traits§
- Capture
Trace - A trait to support custom implementations of traces
Functions§
- timeout
- Drive the future
futto completion, while limiting its run time toduration. Iffutfails to finish withinfuration, returns span traces for all active await points withinfut. Usecaptureto specify which kind of trace should be captured.