Skip to content

Commit 7103006

Browse files
authored
free spawn function (#343)
* feat: add spawn() free function using TelemetryHandle::current * feat: add spawn() free function using TelemetryHandle::current
1 parent 5a355cf commit 7103006

3 files changed

Lines changed: 23 additions & 2 deletions

File tree

dial9-tokio-telemetry/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ pub use current_config::{
3939
Dial9Config, Dial9ConfigBuilder, Dial9ConfigBuilderError, ValidationError,
4040
};
4141
pub use dial9_macro::main;
42-
pub use telemetry::{TelemetryRuntimeError, TracedRuntime};
42+
pub use telemetry::{TelemetryRuntimeError, TracedRuntime, spawn};

dial9-tokio-telemetry/src/telemetry/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub use format::{
2525
pub use recorder::{
2626
HasTracePath, NoTracePath, RuntimeTelemetryHandle, TelemetryCore, TelemetryCoreBuilder,
2727
TelemetryGuard, TelemetryHandle, TelemetryRuntimeError, TraceRuntimeCoreBuilder, TracedRuntime,
28-
TracedRuntimeBuilder, current_worker_id,
28+
TracedRuntimeBuilder, current_worker_id, spawn,
2929
};
3030
pub use task_metadata::{TaskId, UNKNOWN_TASK_ID};
3131
pub use writer::{NullWriter, RotatingWriter, TraceWriter};

dial9-tokio-telemetry/src/telemetry/recorder/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,27 @@ impl TelemetryHandle {
513513
}
514514
}
515515

516+
/// Spawn a traced task on the current tokio runtime.
517+
///
518+
/// Like [`tokio::spawn`], but wraps the future with wake-event tracking
519+
/// when called from a thread owned by a dial9 runtime. On other threads,
520+
/// falls back to plain [`tokio::spawn`].
521+
///
522+
/// Equivalent to [`TelemetryHandle::current().spawn(future)`](TelemetryHandle::spawn).
523+
///
524+
/// # Panics
525+
///
526+
/// Panics if called from outside a tokio runtime context (same
527+
/// as [`tokio::spawn`]).
528+
#[track_caller]
529+
pub fn spawn<F>(future: F) -> tokio::task::JoinHandle<F::Output>
530+
where
531+
F: std::future::Future + Send + 'static,
532+
F::Output: Send + 'static,
533+
{
534+
TelemetryHandle::current().spawn(future)
535+
}
536+
516537
/// RAII guard that sets `INSTRUMENTED_SPAWN` to `true` on creation and
517538
/// resets it to `false` on drop, even if `tokio::spawn` panics.
518539
struct InstrumentedSpawnGuard;

0 commit comments

Comments
 (0)