The function to trace. Receives the active Span as its argument.
Optionalopts: WithTraceOptionsOptional tracing configuration.
The return value of fn (or a Promise thereof).
import { withTrace } from "@tigorhutasuhut/telemetry-js";
// Named function — span name is "fetchUser"
const user = await withTrace(async function fetchUser(span) {
span.setAttribute("user.id", id);
return db.users.find(id);
});
// Explicit name + attributes
const result = withTrace(
(span) => compute(span),
{ name: "heavy-computation", attributes: { "input.size": "42" } },
);
Execute
fninside a new OpenTelemetry span, returning whateverfnreturns.The span is automatically named from the function (or caller location) unless overridden via
opts.name. Errors are recorded on the span and re-thrown.Cloudflare Workers caveat:
performance.now()only advances after I/O in Workers (Spectre mitigation). Spans wrapping pure CPU work (nofetch, KV, R2, D1, etc.) will report a duration of 0 ms. UsewithTracefor operations that involve at least one I/O call.