From 31f66112574319b823ef165515fb2fe80c0a89ef Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Fri, 19 Apr 2024 13:08:34 +0200 Subject: [PATCH 1/2] fix(core): align inconsistent behavior of getEnv() and getEnvWithoutDefaults() when a process polyfill is used --- CHANGELOG.md | 2 ++ .../src/platform/browser/environment.ts | 4 ++++ .../opentelemetry-core/src/platform/browser/index.ts | 2 +- .../src/platform/node/environment.ts | 4 ++++ .../opentelemetry-core/src/platform/node/index.ts | 2 +- packages/opentelemetry-core/src/utils/environment.ts | 11 ----------- 6 files changed, 12 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 46de8a7e524..2ed2b6fe94b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,8 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/ * 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 +* fix(core): align inconsistent behavior of `getEnv()` and `getEnvWithoutDefaults()` when a `process` polyfill is used [#4648](https://github.com/open-telemetry/opentelemetry-js/pull/4648) @pichlermarc + * `getEnvWithoutDefaults()` would use `process.env` if it was defined when running in a browser, while `getEnv()` would always use `_globalThis`. Now both use `_globalThis` when running in a browser. ### :books: (Refine Doc) diff --git a/packages/opentelemetry-core/src/platform/browser/environment.ts b/packages/opentelemetry-core/src/platform/browser/environment.ts index 0c1409671b8..2392cc15ab8 100644 --- a/packages/opentelemetry-core/src/platform/browser/environment.ts +++ b/packages/opentelemetry-core/src/platform/browser/environment.ts @@ -31,3 +31,7 @@ export function getEnv(): Required { ); return Object.assign({}, DEFAULT_ENVIRONMENT, globalEnv); } + +export function getEnvWithoutDefaults(): ENVIRONMENT { + return parseEnvironment(_globalThis as typeof globalThis & RAW_ENVIRONMENT); +} diff --git a/packages/opentelemetry-core/src/platform/browser/index.ts b/packages/opentelemetry-core/src/platform/browser/index.ts index e2860192ef2..ca373f3d4d6 100644 --- a/packages/opentelemetry-core/src/platform/browser/index.ts +++ b/packages/opentelemetry-core/src/platform/browser/index.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -export * from './environment'; +export { getEnvWithoutDefaults, getEnv } from './environment'; export * from './globalThis'; export * from './hex-to-base64'; export * from './RandomIdGenerator'; diff --git a/packages/opentelemetry-core/src/platform/node/environment.ts b/packages/opentelemetry-core/src/platform/node/environment.ts index 48658693fe8..d0c037bf2d4 100644 --- a/packages/opentelemetry-core/src/platform/node/environment.ts +++ b/packages/opentelemetry-core/src/platform/node/environment.ts @@ -28,3 +28,7 @@ export function getEnv(): Required { const processEnv = parseEnvironment(process.env as RAW_ENVIRONMENT); return Object.assign({}, DEFAULT_ENVIRONMENT, processEnv); } + +export function getEnvWithoutDefaults(): ENVIRONMENT { + return parseEnvironment(process.env as RAW_ENVIRONMENT); +} diff --git a/packages/opentelemetry-core/src/platform/node/index.ts b/packages/opentelemetry-core/src/platform/node/index.ts index e2860192ef2..ca373f3d4d6 100644 --- a/packages/opentelemetry-core/src/platform/node/index.ts +++ b/packages/opentelemetry-core/src/platform/node/index.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -export * from './environment'; +export { getEnvWithoutDefaults, getEnv } from './environment'; export * from './globalThis'; export * from './hex-to-base64'; export * from './RandomIdGenerator'; diff --git a/packages/opentelemetry-core/src/utils/environment.ts b/packages/opentelemetry-core/src/utils/environment.ts index fda6e103b7d..3540d0813d3 100644 --- a/packages/opentelemetry-core/src/utils/environment.ts +++ b/packages/opentelemetry-core/src/utils/environment.ts @@ -16,7 +16,6 @@ import { DiagLogLevel } from '@opentelemetry/api'; import { TracesSamplerValues } from './sampling'; -import { _globalThis } from '../platform/browser/globalThis'; const DEFAULT_LIST_SEPARATOR = ','; @@ -369,13 +368,3 @@ export function parseEnvironment(values: RAW_ENVIRONMENT): ENVIRONMENT { return environment; } - -/** - * Get environment in node or browser without - * populating default values. - */ -export function getEnvWithoutDefaults(): ENVIRONMENT { - return typeof process !== 'undefined' && process && process.env - ? parseEnvironment(process.env as RAW_ENVIRONMENT) - : parseEnvironment(_globalThis as typeof globalThis & RAW_ENVIRONMENT); -} From 74c8f1a5410d94375b2ceedbb4a1014495257ab7 Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Fri, 26 Apr 2024 14:29:29 +0200 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 69b921df910..16933175595 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/ ### :bug: (Bug Fix) - fix(core): align inconsistent behavior of `getEnv()` and `getEnvWithoutDefaults()` when a `process` polyfill is used [#4648](https://github.com/open-telemetry/opentelemetry-js/pull/4648) @pichlermarc +* fix(core): align inconsistent behavior of `getEnv()` and `getEnvWithoutDefaults()` when a `process` polyfill is used [#4648](https://github.com/open-telemetry/opentelemetry-js/pull/4648) @pichlermarc * `getEnvWithoutDefaults()` would use `process.env` if it was defined when running in a browser, while `getEnv()` would always use `_globalThis`. Now both use `_globalThis` when running in a browser. * fix(resources): prevent circular import (resource -> detector -> resource -> ...) [#4653](https://github.com/open-telemetry/opentelemetry-js/pull/4653) @pichlermarc * fixes a circular import warning which would appear in rollup when bundling `@opentelemetry/resources`