diff --git a/CHANGELOG.md b/CHANGELOG.md index adca97b027b..5c6585e4650 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2 * refactor(sdk-metrics): use test() instead of match() in isValidName() [#6205](https://github.com/open-telemetry/opentelemetry-js/pull/6205) @cjihrig * refactor(core): remove TimeOriginLegacy Safari <15 fallback [#6235](https://github.com/open-telemetry/opentelemetry-js/pull/6235) @overbalance * chore: remove backcompat workspace [#6238](https://github.com/open-telemetry/opentelemetry-js/pull/6238) @overbalance +* refactor(core,resources): consolidate platform-specific code [#6208](https://github.com/open-telemetry/opentelemetry-js/pull/6208) @overbalance ## 2.2.0 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d178a395db4..83377996000 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -297,28 +297,4 @@ After adding the package, run `npm install` from the root of the project. This w Universal packages are packages that can be used in both web browsers and Node.js environment. These packages may be implemented on top of different -platform APIs to achieve the same goal. Like accessing the _global_ reference, -we have different preferred ways to do it: - -- In Node.js, we access the _global_ reference with `globalThis` or `global`: - -```js -/// packages/opentelemetry-core/src/platform/node/globalThis.ts -export const _globalThis = typeof globalThis === 'object' ? globalThis : global; -``` - -- In web browser, we access the _global_ reference with the following definition: - -```js -/// packages/opentelemetry-core/src/platform/browser/globalThis.ts -export const _globalThis: typeof globalThis = - typeof globalThis === 'object' ? globalThis : - typeof self === 'object' ? self : - typeof window === 'object' ? window : - typeof global === 'object' ? global : - {} as typeof globalThis; -``` - -Even though the implementation may differ, the exported names must be aligned. -It can be confusing if exported names present in one environment but not in the -others. +platform APIs to achieve the same goal. diff --git a/api/CHANGELOG.md b/api/CHANGELOG.md index b9c38e2293d..582ec74a7e6 100644 --- a/api/CHANGELOG.md +++ b/api/CHANGELOG.md @@ -23,6 +23,7 @@ fix(api): prioritize `esnext` export condition as it is more specific [#5458](ht * refactor(api): remove "export *" in favor of explicit named exports [#4880](https://github.com/open-telemetry/opentelemetry-js/pull/4880) @robbkidd * chore: enable tsconfig isolatedModules [#5697](https://github.com/open-telemetry/opentelemetry-js/pull/5697) @legendecas * chore: disallow constructor parameter property syntax [#6187](https://github.com/open-telemetry/opentelemetry-js/pull/6187) @legendecas +* refactor(api): remove platform-specific globalThis, use globalThis directly [#6208](https://github.com/open-telemetry/opentelemetry-js/pull/6208) @overbalance ## 1.9.0 diff --git a/api/package.json b/api/package.json index 215e45c9c92..fd8d00ab27b 100644 --- a/api/package.json +++ b/api/package.json @@ -6,12 +6,6 @@ "module": "build/esm/index.js", "esnext": "build/esnext/index.js", "types": "build/src/index.d.ts", - "browser": { - "./src/platform/index.ts": "./src/platform/browser/index.ts", - "./build/esm/platform/index.js": "./build/esm/platform/browser/index.js", - "./build/esnext/platform/index.js": "./build/esnext/platform/browser/index.js", - "./build/src/platform/index.js": "./build/src/platform/browser/index.js" - }, "exports": { ".": { "esnext": "./build/esnext/index.js", diff --git a/api/src/internal/global-utils.ts b/api/src/internal/global-utils.ts index b8c5fb16dbf..9eeb7d316be 100644 --- a/api/src/internal/global-utils.ts +++ b/api/src/internal/global-utils.ts @@ -17,7 +17,6 @@ import { MeterProvider } from '../metrics/MeterProvider'; import { ContextManager } from '../context/types'; import { DiagLogger } from '../diag/types'; -import { _globalThis } from '../platform'; import { TextMapPropagator } from '../propagation/TextMapPropagator'; import type { TracerProvider } from '../trace/tracer_provider'; import { VERSION } from '../version'; @@ -28,7 +27,7 @@ const GLOBAL_OPENTELEMETRY_API_KEY = Symbol.for( `opentelemetry.js.api.${major}` ); -const _global = _globalThis as OTelGlobal; +const _global = globalThis as OTelGlobal; export function registerGlobal( type: Type, diff --git a/api/src/platform/browser/globalThis.ts b/api/src/platform/browser/globalThis.ts deleted file mode 100644 index 105d77ce9d4..00000000000 --- a/api/src/platform/browser/globalThis.ts +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// Updates to this file should also be replicated to @opentelemetry/core too. - -/** - * - globalThis (New standard) - * - self (Will return the current window instance for supported browsers) - * - window (fallback for older browser implementations) - * - global (NodeJS implementation) - * - (When all else fails) - */ - -/** only globals that common to node and browsers are allowed */ -// eslint-disable-next-line n/no-unsupported-features/es-builtins, no-undef -export const _globalThis: typeof globalThis = - typeof globalThis === 'object' - ? globalThis - : typeof self === 'object' - ? self - : typeof window === 'object' - ? window - : typeof global === 'object' - ? (global as unknown as typeof globalThis) - : ({} as typeof globalThis); diff --git a/api/src/platform/browser/index.ts b/api/src/platform/browser/index.ts deleted file mode 100644 index e4d18640b48..00000000000 --- a/api/src/platform/browser/index.ts +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -export { _globalThis } from './globalThis'; diff --git a/api/src/platform/index.ts b/api/src/platform/index.ts deleted file mode 100644 index a6deb70b173..00000000000 --- a/api/src/platform/index.ts +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -export { _globalThis } from './node'; diff --git a/api/src/platform/node/globalThis.ts b/api/src/platform/node/globalThis.ts deleted file mode 100644 index 14c5b445877..00000000000 --- a/api/src/platform/node/globalThis.ts +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** only globals that common to node and browsers are allowed */ -// eslint-disable-next-line n/no-unsupported-features/es-builtins -export const _globalThis = typeof globalThis === 'object' ? globalThis : global; diff --git a/api/test/common/internal/global.test.ts b/api/test/common/internal/global.test.ts index 05fc7ccc875..b480e287c9d 100644 --- a/api/test/common/internal/global.test.ts +++ b/api/test/common/internal/global.test.ts @@ -17,7 +17,6 @@ import * as assert from 'assert'; import * as sinon from 'sinon'; import { getGlobal } from '../../../src/internal/global-utils'; -import { _globalThis } from '../../../src/platform'; import { NoopContextManager } from '../../../src/context/NoopContextManager'; import { DiagLogLevel } from '../../../src/diag/types'; @@ -56,7 +55,7 @@ describe('Global Utils', function () { api1.trace.disable(); api1.diag.disable(); // @ts-expect-error we are modifying internals for testing purposes here - delete _globalThis[Symbol.for(GLOBAL_API_SYMBOL_KEY)]; + delete globalThis[Symbol.for(GLOBAL_API_SYMBOL_KEY)]; }); it('should change the global context manager', function () { @@ -92,7 +91,7 @@ describe('Global Utils', function () { const globalInstance = getGlobal('diag'); assert.ok(globalInstance); // @ts-expect-error we are modifying internals for testing purposes here - _globalThis[Symbol.for(GLOBAL_API_SYMBOL_KEY)].version = '0.0.1'; + globalThis[Symbol.for(GLOBAL_API_SYMBOL_KEY)].version = '0.0.1'; assert.equal(false, api1.diag.setLogger(logger2)); // won't happen diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 8696710e2b7..a21c54cea1a 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -50,6 +50,7 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2 * test(otlp-transformer): add benchmark for ProtobufTraceSerializer [#6226](https://github.com/open-telemetry/opentelemetry-js/pull/6226) @overbalance * chore(backcompat): fix backcompat tests and tsconfig cleanup [#6232](https://github.com/open-telemetry/opentelemetry-js/pull/6232) @overbalance * refactor(instrumentation-http): refactor getIncomingRequestAttributes() to reduce work [#6207](https://github.com/open-telemetry/opentelemetry-js/pull/6207) @cjihrig +* refactor(api-logs,instrumentation-fetch): remove platform-specific globalThis, use globalThis directly [#6208](https://github.com/open-telemetry/opentelemetry-js/pull/6208) @overbalance ## 0.208.0 diff --git a/experimental/packages/api-logs/package.json b/experimental/packages/api-logs/package.json index 6e9921c3f75..553cacfb7f9 100644 --- a/experimental/packages/api-logs/package.json +++ b/experimental/packages/api-logs/package.json @@ -6,12 +6,6 @@ "module": "build/esm/index.js", "esnext": "build/esnext/index.js", "types": "build/src/index.d.ts", - "browser": { - "./src/platform/index.ts": "./src/platform/browser/index.ts", - "./build/esm/platform/index.js": "./build/esm/platform/browser/index.js", - "./build/esnext/platform/index.js": "./build/esnext/platform/browser/index.js", - "./build/src/platform/index.js": "./build/src/platform/browser/index.js" - }, "repository": "open-telemetry/opentelemetry-js", "scripts": { "prepublishOnly": "npm run compile", diff --git a/experimental/packages/api-logs/src/internal/global-utils.ts b/experimental/packages/api-logs/src/internal/global-utils.ts index 6325db8144a..7dca2b5fcd2 100644 --- a/experimental/packages/api-logs/src/internal/global-utils.ts +++ b/experimental/packages/api-logs/src/internal/global-utils.ts @@ -15,7 +15,6 @@ */ import { LoggerProvider } from '../types/LoggerProvider'; -import { _globalThis } from '../platform'; export const GLOBAL_LOGS_API_KEY = Symbol.for('io.opentelemetry.js.api.logs'); @@ -24,7 +23,7 @@ type OtelGlobal = Partial<{ [GLOBAL_LOGS_API_KEY]: Get; }>; -export const _global = _globalThis as OtelGlobal; +export const _global = globalThis as OtelGlobal; /** * Make a function which accepts a version integer and returns the instance of an API if the version diff --git a/experimental/packages/api-logs/src/platform/browser/globalThis.ts b/experimental/packages/api-logs/src/platform/browser/globalThis.ts deleted file mode 100644 index c22b79a4ccc..00000000000 --- a/experimental/packages/api-logs/src/platform/browser/globalThis.ts +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// Updates to this file should also be replicated to @opentelemetry/api and -// @opentelemetry/core too. - -/** - * - globalThis (New standard) - * - self (Will return the current window instance for supported browsers) - * - window (fallback for older browser implementations) - * - global (NodeJS implementation) - * - (When all else fails) - */ - -/** only globals that common to node and browsers are allowed */ -// eslint-disable-next-line n/no-unsupported-features/es-builtins, no-undef -export const _globalThis: typeof globalThis = - typeof globalThis === 'object' - ? globalThis - : typeof self === 'object' - ? self - : typeof window === 'object' - ? window - : typeof global === 'object' - ? (global as unknown as typeof globalThis) - : ({} as typeof globalThis); diff --git a/experimental/packages/api-logs/src/platform/browser/index.ts b/experimental/packages/api-logs/src/platform/browser/index.ts deleted file mode 100644 index e4d18640b48..00000000000 --- a/experimental/packages/api-logs/src/platform/browser/index.ts +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -export { _globalThis } from './globalThis'; diff --git a/experimental/packages/api-logs/src/platform/index.ts b/experimental/packages/api-logs/src/platform/index.ts deleted file mode 100644 index a6deb70b173..00000000000 --- a/experimental/packages/api-logs/src/platform/index.ts +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -export { _globalThis } from './node'; diff --git a/experimental/packages/api-logs/src/platform/node/globalThis.ts b/experimental/packages/api-logs/src/platform/node/globalThis.ts deleted file mode 100644 index 14c5b445877..00000000000 --- a/experimental/packages/api-logs/src/platform/node/globalThis.ts +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** only globals that common to node and browsers are allowed */ -// eslint-disable-next-line n/no-unsupported-features/es-builtins -export const _globalThis = typeof globalThis === 'object' ? globalThis : global; diff --git a/experimental/packages/api-logs/src/platform/node/index.ts b/experimental/packages/api-logs/src/platform/node/index.ts deleted file mode 100644 index e4d18640b48..00000000000 --- a/experimental/packages/api-logs/src/platform/node/index.ts +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -export { _globalThis } from './globalThis'; diff --git a/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts b/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts index a09e17304fd..cf8b97b6986 100644 --- a/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts +++ b/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts @@ -59,7 +59,6 @@ import { serverPortFromUrl, } from './utils'; import { VERSION } from './version'; -import { _globalThis } from '@opentelemetry/core'; // how long to wait for observer to collect information about resources // this is needed as event "load" is called before observer @@ -658,10 +657,10 @@ export class FetchInstrumentation extends InstrumentationBase(); } } diff --git a/api/src/platform/node/index.ts b/packages/opentelemetry-core/src/common/globalThis.ts similarity index 86% rename from api/src/platform/node/index.ts rename to packages/opentelemetry-core/src/common/globalThis.ts index e4d18640b48..c0102d8df73 100644 --- a/api/src/platform/node/index.ts +++ b/packages/opentelemetry-core/src/common/globalThis.ts @@ -14,4 +14,7 @@ * limitations under the License. */ -export { _globalThis } from './globalThis'; +/** + * @deprecated Use globalThis directly instead. + */ +export const _globalThis = globalThis; diff --git a/packages/opentelemetry-core/src/platform/browser/globalThis.ts b/packages/opentelemetry-core/src/platform/browser/globalThis.ts deleted file mode 100644 index a939e1f7657..00000000000 --- a/packages/opentelemetry-core/src/platform/browser/globalThis.ts +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// Updates to this file should also be replicated to @opentelemetry/api too. - -/** - * - globalThis (New standard) - * - self (Will return the current window instance for supported browsers) - * - window (fallback for older browser implementations) - * - global (NodeJS implementation) - * - (When all else fails) - */ - -/** only globals that common to node and browsers are allowed */ -// eslint-disable-next-line n/no-unsupported-features/es-builtins, no-undef -export const _globalThis: typeof globalThis = - typeof globalThis === 'object' - ? globalThis - : typeof self === 'object' - ? self - : typeof window === 'object' - ? window - : typeof global === 'object' - ? global - : ({} as typeof globalThis); diff --git a/packages/opentelemetry-core/src/platform/browser/index.ts b/packages/opentelemetry-core/src/platform/browser/index.ts index 27b2ba4a0de..9a531d82a43 100644 --- a/packages/opentelemetry-core/src/platform/browser/index.ts +++ b/packages/opentelemetry-core/src/platform/browser/index.ts @@ -20,6 +20,10 @@ export { getNumberFromEnv, getStringListFromEnv, } from './environment'; -export { _globalThis } from './globalThis'; -export { otperformance } from './performance'; +export { _globalThis } from '../../common/globalThis'; export { SDK_INFO } from './sdk-info'; + +/** + * @deprecated Use performance directly. + */ +export const otperformance = performance; diff --git a/packages/opentelemetry-core/src/platform/browser/performance.ts b/packages/opentelemetry-core/src/platform/browser/performance.ts deleted file mode 100644 index e397328e731..00000000000 --- a/packages/opentelemetry-core/src/platform/browser/performance.ts +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -export const otperformance: { now(): number; readonly timeOrigin: number } = - performance; diff --git a/packages/opentelemetry-core/src/platform/node/globalThis.ts b/packages/opentelemetry-core/src/platform/node/globalThis.ts deleted file mode 100644 index 14c5b445877..00000000000 --- a/packages/opentelemetry-core/src/platform/node/globalThis.ts +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** only globals that common to node and browsers are allowed */ -// eslint-disable-next-line n/no-unsupported-features/es-builtins -export const _globalThis = typeof globalThis === 'object' ? globalThis : global; diff --git a/packages/opentelemetry-core/src/platform/node/index.ts b/packages/opentelemetry-core/src/platform/node/index.ts index 27b2ba4a0de..9a531d82a43 100644 --- a/packages/opentelemetry-core/src/platform/node/index.ts +++ b/packages/opentelemetry-core/src/platform/node/index.ts @@ -20,6 +20,10 @@ export { getNumberFromEnv, getStringListFromEnv, } from './environment'; -export { _globalThis } from './globalThis'; -export { otperformance } from './performance'; +export { _globalThis } from '../../common/globalThis'; export { SDK_INFO } from './sdk-info'; + +/** + * @deprecated Use performance directly. + */ +export const otperformance = performance; diff --git a/packages/opentelemetry-core/src/platform/node/performance.ts b/packages/opentelemetry-core/src/platform/node/performance.ts deleted file mode 100644 index 34b364cbee1..00000000000 --- a/packages/opentelemetry-core/src/platform/node/performance.ts +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { performance } from 'perf_hooks'; - -export const otperformance: { now(): number; readonly timeOrigin: number } = - performance; diff --git a/packages/opentelemetry-resources/package.json b/packages/opentelemetry-resources/package.json index 070d7b4d414..3608ee6f836 100644 --- a/packages/opentelemetry-resources/package.json +++ b/packages/opentelemetry-resources/package.json @@ -6,10 +6,6 @@ "module": "build/esm/index.js", "esnext": "build/esnext/index.js", "browser": { - "./src/platform/index.ts": "./src/platform/browser/index.ts", - "./build/esm/platform/index.js": "./build/esm/platform/browser/index.js", - "./build/esnext/platform/index.js": "./build/esnext/platform/browser/index.js", - "./build/src/platform/index.js": "./build/src/platform/browser/index.js", "./src/detectors/platform/index.ts": "./src/detectors/platform/browser/index.ts", "./build/esm/detectors/platform/index.js": "./build/esm/detectors/platform/browser/index.js", "./build/esnext/detectors/platform/index.js": "./build/esnext/detectors/platform/browser/index.js", diff --git a/packages/opentelemetry-resources/src/ResourceImpl.ts b/packages/opentelemetry-resources/src/ResourceImpl.ts index 3ab01c683b5..e40735e7ed6 100644 --- a/packages/opentelemetry-resources/src/ResourceImpl.ts +++ b/packages/opentelemetry-resources/src/ResourceImpl.ts @@ -23,7 +23,7 @@ import { ATTR_TELEMETRY_SDK_VERSION, } from '@opentelemetry/semantic-conventions'; import { Resource } from './Resource'; -import { defaultServiceName } from './platform'; +import { defaultServiceName } from './default-service-name'; import { DetectedResource, DetectedResourceAttributes, diff --git a/packages/opentelemetry-resources/src/platform/browser/default-service-name.ts b/packages/opentelemetry-resources/src/default-service-name.ts similarity index 65% rename from packages/opentelemetry-resources/src/platform/browser/default-service-name.ts rename to packages/opentelemetry-resources/src/default-service-name.ts index 0811da9cf6b..887dddc73a2 100644 --- a/packages/opentelemetry-resources/src/platform/browser/default-service-name.ts +++ b/packages/opentelemetry-resources/src/default-service-name.ts @@ -14,6 +14,15 @@ * limitations under the License. */ +// Check if we are in a Node.js environment and if so, use the process.argv0 property +// to determine the default service name +const DEFAULT_SERVICE_NAME = + typeof process === 'object' && + typeof process.argv0 === 'string' && + process.argv0.length > 0 + ? `unknown_service:${process.argv0}` + : 'unknown_service'; + export function defaultServiceName(): string { - return 'unknown_service'; + return DEFAULT_SERVICE_NAME; } diff --git a/packages/opentelemetry-resources/src/index.ts b/packages/opentelemetry-resources/src/index.ts index 6e957d2d88c..9dba58f5e85 100644 --- a/packages/opentelemetry-resources/src/index.ts +++ b/packages/opentelemetry-resources/src/index.ts @@ -29,7 +29,7 @@ export { defaultResource, emptyResource, } from './ResourceImpl'; -export { defaultServiceName } from './platform'; +export { defaultServiceName } from './default-service-name'; export type { ResourceDetector, DetectedResource, diff --git a/packages/opentelemetry-resources/src/platform/browser/index.ts b/packages/opentelemetry-resources/src/platform/browser/index.ts deleted file mode 100644 index 7d7c3616481..00000000000 --- a/packages/opentelemetry-resources/src/platform/browser/index.ts +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -export { defaultServiceName } from './default-service-name'; diff --git a/packages/opentelemetry-resources/src/platform/index.ts b/packages/opentelemetry-resources/src/platform/index.ts deleted file mode 100644 index ac520108f4c..00000000000 --- a/packages/opentelemetry-resources/src/platform/index.ts +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -export { defaultServiceName } from './node'; diff --git a/packages/opentelemetry-resources/src/platform/node/default-service-name.ts b/packages/opentelemetry-resources/src/platform/node/default-service-name.ts deleted file mode 100644 index 8b3b1a3b54f..00000000000 --- a/packages/opentelemetry-resources/src/platform/node/default-service-name.ts +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -export function defaultServiceName(): string { - return `unknown_service:${process.argv0}`; -} diff --git a/packages/opentelemetry-resources/src/platform/node/index.ts b/packages/opentelemetry-resources/src/platform/node/index.ts deleted file mode 100644 index 7d7c3616481..00000000000 --- a/packages/opentelemetry-resources/src/platform/node/index.ts +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -export { defaultServiceName } from './default-service-name'; diff --git a/packages/opentelemetry-resources/test/default-service-name.test.ts b/packages/opentelemetry-resources/test/default-service-name.test.ts new file mode 100644 index 00000000000..7456d2a0162 --- /dev/null +++ b/packages/opentelemetry-resources/test/default-service-name.test.ts @@ -0,0 +1,46 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import * as assert from 'assert'; +import { defaultServiceName } from '../src/default-service-name'; + +const isNode = typeof process === 'object' && typeof process.argv0 === 'string'; + +describe('defaultServiceName', () => { + it('returns unknown_service prefix', () => { + const serviceName = defaultServiceName(); + assert.ok(serviceName.startsWith('unknown_service')); + }); + + if (isNode) { + it('includes process.argv0 in Node.js', () => { + const serviceName = defaultServiceName(); + assert.ok(serviceName.startsWith('unknown_service:')); + assert.ok(serviceName.length > 'unknown_service:'.length); + }); + } else { + it('returns plain unknown_service in browser', () => { + const serviceName = defaultServiceName(); + assert.strictEqual(serviceName, 'unknown_service'); + }); + } + + it('returns consistent value on multiple calls', () => { + const serviceName1 = defaultServiceName(); + const serviceName2 = defaultServiceName(); + assert.strictEqual(serviceName1, serviceName2); + }); +});