Skip to content

Commit aff48a1

Browse files
trentmpichlermarc
andauthored
fix(instr-fetch): do not enable in Node.js; clarify in docs this instr is for web fetch only (#4498)
* fix(instr-fetch): do not enable in Node.js; clarify in docs this instr is for web fetch only * add a changelog entry * add a diagnostic warning if attempting to use instr-fetch in Node.js * fixup! add a diagnostic warning if attempting to use instr-fetch in Node.js --------- Co-authored-by: Marc Pichler <[email protected]>
1 parent 89caef9 commit aff48a1

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

experimental/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ All notable changes to experimental packages in this project will be documented
2626
* fix(instrumentation): normalize paths for internal files in scoped packages [#4467](https://github.com/open-telemetry/opentelemetry-js/pull/4467) @pichlermarc
2727
* Fixes a bug where, on Windows, internal files on scoped packages would not be instrumented.
2828
* fix(otlp-transformer): only use BigInt inside hrTimeToNanos() [#4484](https://github.com/open-telemetry/opentelemetry-js/pull/4484) @pichlermarc
29+
* fix(instrumentation-fetch): do not enable in Node.js; clarify in docs this instr is for web fetch only [#4498](https://github.com/open-telemetry/opentelemetry-js/pull/4498) @trentm
2930

3031
### :books: (Refine Doc)
3132

experimental/packages/opentelemetry-instrumentation-fetch/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
**Note: This is an experimental package under active development. New releases may include breaking changes.**
77

8-
This module provides auto instrumentation for web using fetch.
8+
This module provides auto instrumentation for web using [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch).
9+
(Note: This instrumentation does **not** instrument [Node.js' fetch](https://nodejs.org/api/globals.html#fetch). See [this issue](https://github.com/open-telemetry/opentelemetry-js/issues/4333).)
910

1011
## Installation
1112

experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts

+13
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import { _globalThis } from '@opentelemetry/core';
3535
// safe enough
3636
const OBSERVER_WAIT_TIME_MS = 300;
3737

38+
const isNode = typeof process === 'object' && process.release?.name === 'node';
39+
3840
export interface FetchCustomAttributeFunction {
3941
(
4042
span: api.Span,
@@ -465,6 +467,14 @@ export class FetchInstrumentation extends InstrumentationBase<
465467
* implements enable function
466468
*/
467469
override enable(): void {
470+
if (isNode) {
471+
// Node.js v18+ *does* have a global `fetch()`, but this package does not
472+
// support instrumenting it.
473+
this._diag.warn(
474+
"this instrumentation is intended for web usage only, it does not instrument Node.js's fetch()"
475+
);
476+
return;
477+
}
468478
if (isWrapped(fetch)) {
469479
this._unwrap(_globalThis, 'fetch');
470480
this._diag.debug('removing previous patch for constructor');
@@ -476,6 +486,9 @@ export class FetchInstrumentation extends InstrumentationBase<
476486
* implements unpatch function
477487
*/
478488
override disable(): void {
489+
if (isNode) {
490+
return;
491+
}
479492
this._unwrap(_globalThis, 'fetch');
480493
this._usedResources = new WeakSet<PerformanceResourceTiming>();
481494
}

0 commit comments

Comments
 (0)