juncture_core/tracing_wasm.rs
1//! WASM-compatible tracing helpers.
2//!
3//! On WASM, `tracing::info_span!()` and `Instrumented::poll()` call
4//! `std::time::Instant::now()` which panics. This module provides
5//! `cfg`-gated replacements that completely avoid `Instant::now()`.
6
7/// A wrapper around `tracing::Span` that makes `.instrument()` a no-op.
8///
9/// On WASM, `info_span!()` returns `WasmSpan(Span::none())`. When
10/// `.instrument(wasm_span)` is called, it returns the future unchanged
11/// instead of wrapping it in `Instrumented<F>` (which calls `Instant::now()`).
12#[cfg(target_family = "wasm")]
13pub struct WasmSpan(pub tracing::Span);
14
15/// Create a span that is a no-op on WASM.
16///
17/// On WASM: returns `WasmSpan(Span::none())` - `.instrument()` becomes a no-op.
18/// On native: delegates to `tracing::info_span!()`.
19#[cfg(target_family = "wasm")]
20#[macro_export]
21macro_rules! info_span {
22 ($($args:tt)*) => {
23 $crate::tracing_wasm::WasmSpan(tracing::Span::none())
24 };
25}
26
27/// Create a span (native path).
28///
29/// On native: delegates to `tracing::info_span!()`.
30#[cfg(not(target_family = "wasm"))]
31#[macro_export]
32macro_rules! info_span {
33 ($($args:tt)*) => {
34 tracing::info_span!($($args)*)
35 };
36}
37
38/// Extension trait that makes `.instrument(WasmSpan)` a no-op on WASM.
39///
40/// This avoids creating `Instrumented<F>` which calls `Instant::now()`.
41#[cfg(target_family = "wasm")]
42pub trait WasmInstrument: std::future::Future + Sized {
43 fn instrument(self, _span: WasmSpan) -> Self {
44 self
45 }
46}
47
48#[cfg(target_family = "wasm")]
49impl<F: std::future::Future> WasmInstrument for F {}