spawn_with_context

Function spawn_with_context 

Source
pub fn spawn_with_context<F>(future: F) -> JoinHandle<<F as Future>::Output>
where F: Future + Send + 'static, <F as Future>::Output: Send + 'static,
Expand description

Spawns a future on the tokio runtime while preserving the current span context.

This is a wrapper around tokio::spawn that captures the current span ID before spawning and establishes it as the parent context in the spawned task. This ensures that instrumented async functions called within the spawned task will correctly report the spawning context as their parent.

The parent span ID is pushed onto the thread-local async call stack before each poll and popped after, so it works correctly across yield points and executor thread migration.

§Example

use micromegas_tracing::prelude::*;

#[span_fn]
async fn parent_work() {
    // Spans created in child_work will show parent_work as their parent
    spawn_with_context(child_work()).await.unwrap();
}

#[span_fn]
async fn child_work() {
    // This span's parent will be parent_work, not root
}