From 11ae37cfc6d9d15174dae7d1c628670642ef9133 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Wed, 3 Apr 2024 15:09:06 -0700 Subject: [PATCH 1/2] fix(resources): ensure BrowserDetector does not think Node.js v21 is a browser Fixes: #4561 --- CHANGELOG.md | 1 + .../src/detectors/BrowserDetectorSync.ts | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index becf83bc68..0c3d6bfe7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/ ### :bug: (Bug Fix) * fix(sdk-trace-web): fix invalid timings in span events [#4486](https://github.com/open-telemetry/opentelemetry-js/pull/4486) @Abinet18 +* fix(resources): ensure BrowserDetector does not think Node.js v21 is a browser [#4561](https://github.com/open-telemetry/opentelemetry-js/issues/4561) @trentm ### :books: (Refine Doc) diff --git a/packages/opentelemetry-resources/src/detectors/BrowserDetectorSync.ts b/packages/opentelemetry-resources/src/detectors/BrowserDetectorSync.ts index b58fea94c8..dec691ee08 100644 --- a/packages/opentelemetry-resources/src/detectors/BrowserDetectorSync.ts +++ b/packages/opentelemetry-resources/src/detectors/BrowserDetectorSync.ts @@ -24,7 +24,12 @@ import { diag } from '@opentelemetry/api'; */ class BrowserDetectorSync implements DetectorSync { detect(config?: ResourceDetectionConfig): IResource { - const isBrowser = typeof navigator !== 'undefined'; + const isBrowser = + typeof navigator !== 'undefined' && + process?.versions?.node === undefined && // Node.js v21 adds `navigator` + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore don't have Bun types + Bun?.version === undefined; // Bun (bun.sh) defines `navigator` if (!isBrowser) { return Resource.empty(); } From 7a82af29b245f02f2a1f486b3782a94257a6daf1 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Wed, 3 Apr 2024 15:15:34 -0700 Subject: [PATCH 2/2] fix browser test --- .../src/detectors/BrowserDetectorSync.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/opentelemetry-resources/src/detectors/BrowserDetectorSync.ts b/packages/opentelemetry-resources/src/detectors/BrowserDetectorSync.ts index dec691ee08..66a4801067 100644 --- a/packages/opentelemetry-resources/src/detectors/BrowserDetectorSync.ts +++ b/packages/opentelemetry-resources/src/detectors/BrowserDetectorSync.ts @@ -26,10 +26,10 @@ class BrowserDetectorSync implements DetectorSync { detect(config?: ResourceDetectionConfig): IResource { const isBrowser = typeof navigator !== 'undefined' && - process?.versions?.node === undefined && // Node.js v21 adds `navigator` + global.process?.versions?.node === undefined && // Node.js v21 adds `navigator` // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore don't have Bun types - Bun?.version === undefined; // Bun (bun.sh) defines `navigator` + global.Bun?.version === undefined; // Bun (bun.sh) defines `navigator` if (!isBrowser) { return Resource.empty(); }