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

    Function instrumentFetch

    • Wrap a fetch function with OpenTelemetry tracing and optional request/response logging.

      Creates a CLIENT span for each outgoing request, injects traceparent/tracestate into outgoing headers via propagation.inject(), and optionally emits a single structured log entry with the full request/response round-trip details.

      Cloudflare Workers — you do not need to call this manually. instrument and traceHandler automatically monkey-patch globalThis.fetch on the first request. Use getOriginalFetch if you need the un-instrumented version (e.g. for OTLP export calls).

      Node.js — you do not need to call this either. @opentelemetry/sdk-node (used internally by initSDK) registers auto-instrumentation for http/undici which already traces outgoing requests. Calling instrumentFetch on top of that would produce duplicate spans.

      This function is mainly useful when you need custom control over which fetch reference is wrapped, or when running in a non-standard runtime where neither the Cloudflare nor Node.js adapters apply.

      Parameters

      • originalFetch: (input: string | URL | Request, init?: RequestInit) => Promise<Response>

        The fetch function to wrap (typically globalThis.fetch).

      • Optionalconfig: InstrumentFetchConfig

        Optional configuration.

      Returns (input: string | URL | Request, init?: RequestInit) => Promise<Response>

      A wrapped fetch with the same signature.

      import { instrumentFetch } from "@tigorhutasuhut/telemetry-js";

      const tracedFetch = instrumentFetch(globalThis.fetch, { logger });
      const res = await tracedFetch("https://api.example.com/data");