Skip to content

Commit

Permalink
fix(instr-fetch): do not enable in Node.js; clarify in docs this inst…
Browse files Browse the repository at this point in the history
…r 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 <marc.pichler@dynatrace.com>
  • Loading branch information
trentm and pichlermarc committed Feb 23, 2024
1 parent 89caef9 commit aff48a1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ All notable changes to experimental packages in this project will be documented
* fix(instrumentation): normalize paths for internal files in scoped packages [#4467](https://github.com/open-telemetry/opentelemetry-js/pull/4467) @pichlermarc
* Fixes a bug where, on Windows, internal files on scoped packages would not be instrumented.
* fix(otlp-transformer): only use BigInt inside hrTimeToNanos() [#4484](https://github.com/open-telemetry/opentelemetry-js/pull/4484) @pichlermarc
* 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

### :books: (Refine Doc)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

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

This module provides auto instrumentation for web using fetch.
This module provides auto instrumentation for web using [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch).
(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).)

## Installation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import { _globalThis } from '@opentelemetry/core';
// safe enough
const OBSERVER_WAIT_TIME_MS = 300;

const isNode = typeof process === 'object' && process.release?.name === 'node';

export interface FetchCustomAttributeFunction {
(
span: api.Span,
Expand Down Expand Up @@ -465,6 +467,14 @@ export class FetchInstrumentation extends InstrumentationBase<
* implements enable function
*/
override enable(): void {
if (isNode) {
// Node.js v18+ *does* have a global `fetch()`, but this package does not
// support instrumenting it.
this._diag.warn(
"this instrumentation is intended for web usage only, it does not instrument Node.js's fetch()"
);
return;
}
if (isWrapped(fetch)) {
this._unwrap(_globalThis, 'fetch');
this._diag.debug('removing previous patch for constructor');
Expand All @@ -476,6 +486,9 @@ export class FetchInstrumentation extends InstrumentationBase<
* implements unpatch function
*/
override disable(): void {
if (isNode) {
return;
}
this._unwrap(_globalThis, 'fetch');
this._usedResources = new WeakSet<PerformanceResourceTiming>();
}
Expand Down

0 comments on commit aff48a1

Please sign in to comment.