From 141b457b8513069b64a5f246165167832fc0d83a Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Fri, 26 Apr 2024 14:46:29 +0200 Subject: [PATCH] fix(core): align inconsistent behavior of getEnv() and getEnvWithoutDefaults() when a process polyfill is used (#4649) * fix(core): align inconsistent behavior of getEnv() and getEnvWithoutDefaults() when a process polyfill is used * Update CHANGELOG.md --- CHANGELOG.md | 7 +++++-- .../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, 15 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6072f038916..16933175595 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,11 @@ 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 + * `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` + ### :books: (Refine Doc) ### :house: (Internal) @@ -29,8 +34,6 @@ 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(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` ## 1.23.0 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); -}