You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(setup): add OTEL_INSTRUMENT_FETCH env var to toggle fetch tracing
Introduces `OTEL_INSTRUMENT_FETCH` (`true`/`1` | `false`/`0`) that
overrides both the `instrumentFetch` option and the smart default,
matching the rest of the package's env-wins config story.
- `parseBooleanEnv` helper mirrors `envLevel()` — defers on unrecognized
values so the code option still applies
- Integration test covers the disabled case via a child process
(`disabled-fetch.child.mjs`) because `setupOtel` is process-global and
idempotent; CI workflow now runs `bun run build` first since the child
imports the built bundle
- Unit tests use a `fetchInstrumentationActive()` helper that checks
both
the global-wrap marker and the undici diagnostics_channel subscriber,
catching the native Node path that never reassigns `globalThis.fetch`
Copy file name to clipboardExpand all lines: docs/configuration.mdx
+9Lines changed: 9 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -183,6 +183,14 @@ setupOtel({
183
183
});
184
184
```
185
185
186
+
Or disable it from the environment — no code change or redeploy of app logic required:
187
+
188
+
```bash
189
+
OTEL_INSTRUMENT_FETCH=false
190
+
```
191
+
192
+
`OTEL_INSTRUMENT_FETCH` accepts `true` / `1` (force on) and `false` / `0` (disable), and takes precedence over both the `instrumentFetch` option and the default. The object form (`mode`, `ignore`) still configures _how_ fetch is traced whenever instrumentation is on.
193
+
186
194
You can force it on even without an exporter endpoint:
187
195
188
196
```ts
@@ -228,6 +236,7 @@ The package always excludes its own OTLP trace and log exporter endpoints from f
Copy file name to clipboardExpand all lines: docs/guides/fetch-instrumentation.mdx
+18Lines changed: 18 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,6 +19,22 @@ When a traces endpoint is configured, setup instruments fetch by default. The st
19
19
20
20
The standalone `instrumentFetch()` export always performs the `globalThis.fetch` wrap, and `createInstrumentedFetch()` wraps a single fetch instance (for SDKs) without touching the global.
21
21
22
+
## Enabling and disabling
23
+
24
+
Pass `instrumentFetch: false` to turn it off, or `true` to force it on even without an endpoint:
The `OTEL_INSTRUMENT_FETCH` environment variable overrides both the option and the default — `true` / `1` forces it on, `false` / `0` disables it. Because env wins over code (matching the rest of the package's configuration), you can silence noisy or expensive fetch spans in production without changing application code:
31
+
32
+
```bash
33
+
OTEL_INSTRUMENT_FETCH=false
34
+
```
35
+
36
+
The env var only flips the on/off decision; when instrumentation stays on, the object form still applies — `OTEL_INSTRUMENT_FETCH=true` alongside `instrumentFetch: { mode: "global", ignore }` keeps your `mode` and `ignore`.
37
+
22
38
## What each fetch span contains
23
39
24
40
For an outbound request, fetch instrumentation creates a span with:
@@ -125,6 +141,8 @@ If fetch is already wrapped by this package, a second call does not stack anothe
125
141
126
142
The implementation uses a global symbol marker to remember the original fetch. That guard works even if two copies of the package are loaded, such as Bun consuming TypeScript source while another path consumes the built ESM bundle.
127
143
144
+
`setupOtel()` is idempotent too: the first call wins, so a later `setupOtel({ instrumentFetch: false })` cannot turn off instrumentation that an earlier call already enabled. Decide on the first call, or use `OTEL_INSTRUMENT_FETCH`, which is read whenever setup actually runs.
0 commit comments