@tigorhutasuhut/telemetry-js - v0.7.3
    Preparing search index...

    Function withTrace

    • Execute fn inside a new OpenTelemetry span, returning whatever fn returns.

      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 (no fetch, KV, R2, D1, etc.) will report a duration of 0 ms. Use withTrace for operations that involve at least one I/O call.

      Type Parameters

      • T

      Parameters

      • fn: (span: Span) => T

        The function to trace. Receives the active Span as its argument.

      • Optionalopts: WithTraceOptions

        Optional tracing configuration.

      Returns T

      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" } },
      );