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.
Wrap a
fetchfunction with OpenTelemetry tracing and optional request/response logging.Creates a
CLIENTspan for each outgoing request, injectstraceparent/tracestateinto outgoing headers viapropagation.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.fetchon 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 byinitSDK) registers auto-instrumentation forhttp/undiciwhich already traces outgoing requests. CallinginstrumentFetchon top of that would produce duplicate spans.This function is mainly useful when you need custom control over which
fetchreference is wrapped, or when running in a non-standard runtime where neither the Cloudflare nor Node.js adapters apply.