Skip to content

refactor: consolidate platform-specific code#6208

Merged
overbalance merged 8 commits intoopen-telemetry:mainfrom
embrace-io:overbalance/consolidate-platform
Dec 19, 2025
Merged

refactor: consolidate platform-specific code#6208
overbalance merged 8 commits intoopen-telemetry:mainfrom
embrace-io:overbalance/consolidate-platform

Conversation

@overbalance
Copy link
Copy Markdown
Contributor

@overbalance overbalance commented Dec 10, 2025

Which problem is this PR solving?

Platform directories contain duplicate code with fallback chains for legacy environments that are no longer needed. Since the minimum supported versions are Node 18+ and modern browsers, globalThis and performance are universally available.

Short description of the changes

@opentelemetry/api

  • Deleted entire src/platform/ directory
  • Use globalThis directly instead of _globalThis
  • Removed browser field mappings from package.json

@opentelemetry/api-logs

  • Deleted entire src/platform/ directory
  • Use globalThis directly instead of _globalThis
  • Removed browser field mappings from package.json

@opentelemetry/core

  • globalThis: Moved to common/globalThis.ts as deprecated re-export of globalThis
  • performance: Removed wrapper files, otperformance is now a deprecated alias for performance
  • Deleted platform-specific globalThis.ts and performance.ts files

@opentelemetry/resources

  • Hoisted defaultServiceName to src/ with runtime check (process.argv0 in Node, fallback to unknown_service)
  • Deleted entire src/platform/ directory
  • Removed 4 browser field mappings from package.json
  • Added unit tests for defaultServiceName

@opentelemetry/instrumentation-fetch

  • Use globalThis directly instead of importing _globalThis from core

Documentation

  • Updated CONTRIBUTING.md to remove outdated platform-specific code examples
  • Added CHANGELOG entry

Type of change

  • Refactor (non-breaking change which improves code quality)

How Has This Been Tested?

  • npm test passes for all affected packages
  • Browser/webworker tests pass
  • Added tests for defaultServiceName (Node + browser environments)

Checklist:

  • Followed the style guidelines of this project
  • Unit tests have been added
  • Documentation has been updated

@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 10, 2025

Codecov Report

❌ Patch coverage is 90.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 95.39%. Comparing base (b2025ca) to head (61d0bbb).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...pentelemetry-resources/src/default-service-name.ts 83.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6208      +/-   ##
==========================================
- Coverage   95.40%   95.39%   -0.02%     
==========================================
  Files         317      314       -3     
  Lines        9514     9513       -1     
  Branches     2192     2194       +2     
==========================================
- Hits         9077     9075       -2     
- Misses        437      438       +1     
Files with missing lines Coverage Δ
api/src/internal/global-utils.ts 100.00% <100.00%> (ø)
...tal/packages/api-logs/src/internal/global-utils.ts 100.00% <100.00%> (ø)
...ckages/opentelemetry-core/src/common/globalThis.ts 100.00% <100.00%> (ø)
...ckages/opentelemetry-resources/src/ResourceImpl.ts 98.90% <100.00%> (ø)
...pentelemetry-resources/src/default-service-name.ts 85.71% <83.33%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@cjihrig
Copy link
Copy Markdown
Contributor

cjihrig commented Dec 10, 2025

~1.8x slower than Node Buffer (acceptable: 126ns vs 70ns per call)

Even though it's nice to have shared code, I'm not sure we should make this method slower. I (and others) am already seeing drastic slowdowns in HTTP servers when OTel is enabled, and generatedId() was showing up in flame graphs (albeit only ~3% of captured stacks).

@overbalance overbalance force-pushed the overbalance/consolidate-platform branch from 3e120ec to 16a4d1a Compare December 10, 2025 23:01
@overbalance
Copy link
Copy Markdown
Contributor Author

@cjihrig Cool. Reverted.

@overbalance overbalance force-pushed the overbalance/consolidate-platform branch from 16a4d1a to b4bebe0 Compare December 10, 2025 23:09
@overbalance overbalance force-pushed the overbalance/consolidate-platform branch from b4bebe0 to e72efa4 Compare December 10, 2025 23:13
@overbalance overbalance marked this pull request as ready for review December 10, 2025 23:13
@overbalance overbalance requested a review from a team as a code owner December 10, 2025 23:13
@overbalance overbalance added the browser Browser-specific additions or benefits label Dec 17, 2025
Copy link
Copy Markdown
Contributor

@david-luna david-luna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of consolidating code whenever possible without affecting perf. In fact I think sdk-info.ts from core package could be consolidated as well. LGTM but let's wait for other reviews :)

@overbalance overbalance requested a review from a team December 18, 2025 19:57
Copy link
Copy Markdown
Member

@pichlermarc pichlermarc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opentelemetry-core

  • globalThis: Moved to common - browser's fallback chain (globalThis → self → window → global → {}) works in all environments

We should be able to deprecate this and subsequently remove it in @opentelmetry/core@3.0.0, actually. It's only used in @opentelemetry/instrumentation-fetch

suggestion: let's copy the browser's fallback chain to where it's used in @opentelemetry/instrumentation-fetch and deprecate the one in @opentelemetry/core.

@overbalance
Copy link
Copy Markdown
Contributor Author

@pichlermarc I updated api and api-logs as well since they had the same pattern and the docs in core said to keep api in sync.

Comment thread experimental/packages/api-logs/src/internal/global-utils.ts
@overbalance overbalance added this pull request to the merge queue Dec 19, 2025
Merged via the queue into open-telemetry:main with commit 4b365bf Dec 19, 2025
27 checks passed
@overbalance overbalance deleted the overbalance/consolidate-platform branch December 19, 2025 19:00
@otelbot-js otelbot-js Bot mentioned this pull request Jan 7, 2026
typeof process.argv0 === 'string' &&
process.argv0.length > 0
? `unknown_service:${process.argv0}`
: 'unknown_service';
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@overbalance
This breaks on vercel edge environments

Copy link
Copy Markdown

@andreiborza andreiborza Jan 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @overbalance, as @chargome mentioned, this unfortunately breaks our vercel-edge environment due to process.argv0 being a node api.

We ended up having to bundle in @opentelemetry/resources and any code that uses it. It's not ideal.

Any way we can figure out a workaround here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chargome @andreiborza check out this other PR I've had out for review. Do you think it would solve your issue: #6257

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey, that might work! Thanks for tackling this 🙏

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andreiborza You said this "breaks"? What is the break? Inferring from @overbalance's PR there is warning from vercel-edge static analysis of some sort? Can you show an example?

For example, I don't recall here about warnings/breakages from this old change that was feature-testing for Node.js' process object: https://github.com/open-telemetry/opentelemetry-js/pull/4604/files#diff-7775cbc8f9eed30ff77488613bcbabec57446d86f655166fbdb5f7db4579a0b6R29

In that case it was checking global.process?.<something>.
If the code here in this PR used global.process instead of process, would that make the static analysis warnings go away?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's an example run of a nextjs app using our vercel-edge SDK: https://github.com/getsentry/sentry-javascript/actions/runs/20844816035/job/59886209607?pr=18734#step:16:157

We end up getting

 'A Node.js API is used (process.argv0 at line: 19) which is not supported in the Edge Runtime.\n' +

issues when building the app.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @andreiborza I was able to replicate and solve the issue: #6257

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

browser Browser-specific additions or benefits

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants