Creates a new trace, evaluates the expression, ends the trace, and
optionally exports it. The trace is available via current_trace()
during evaluation.
Thread Safety
The context stack is process-global, following R's standard single-threaded
assumption. Parallel workers spawned via future, callr, or
parallel receive isolated copies of the stack, so spans created in
those workers will not appear in the parent trace. This is consistent
with how options(), par(), and Sys.setenv() behave in
base R.
Examples
# Trace a block of code
result <- with_trace("my-operation", {
Sys.sleep(0.01)
1 + 1
})
result
#> [1] 2
# With an exporter
result <- with_trace("traced-op", {
10 * 2
}, exporter = console_exporter(verbose = FALSE))
#> --- Trace: traced-op ---
#> Status: completed
#> Duration: 0.00s
#> Spans: 0