Skip to content

refactor(resources): use runtime check for default service name#6257

Merged
overbalance merged 2 commits intoopen-telemetry:mainfrom
embrace-io:overbalance/convert-to-runtime
Jan 16, 2026
Merged

refactor(resources): use runtime check for default service name#6257
overbalance merged 2 commits intoopen-telemetry:mainfrom
embrace-io:overbalance/convert-to-runtime

Conversation

@overbalance
Copy link
Copy Markdown
Contributor

@overbalance overbalance commented Dec 29, 2025

Which problem is this PR solving?

Static analysis tools in browser/Edge Runtime environments warn about accessing process.argv0 directly. Edge runtimes may also define a process object that is empty or incomplete.

Short description of the changes

  • Use globalThis.process.argv0 with try/catch to handle environments where process may not exist or may be incomplete
  • Lazy-evaluate and cache the service name on first call
  • Add _clearDefaultServiceNameCache() helper for testing
  • Add Next.js 15 Edge Runtime bundler test
  • Add Next.js 16 Edge Runtime bundler test

Type of change

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

How Has This Been Tested?

  • Unit tests for Node.js, browser, and edge runtime scenarios
  • Tests verify correct behavior when process is undefined, empty, or fully populated
  • Next.js 15 Edge Runtime middleware bundler test
  • Next.js 16 Edge Runtime API route bundler test

Checklist:

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

@overbalance overbalance requested a review from a team as a code owner December 29, 2025 18:34
@overbalance overbalance added the browser Browser-specific additions or benefits label Dec 29, 2025
@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 29, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.42%. Comparing base (0a9b614) to head (4acde2a).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6257      +/-   ##
==========================================
+ Coverage   95.41%   95.42%   +0.01%     
==========================================
  Files         317      317              
  Lines        9597     9599       +2     
  Branches     2220     2217       -3     
==========================================
+ Hits         9157     9160       +3     
+ Misses        440      439       -1     
Files with missing lines Coverage Δ
...pentelemetry-resources/src/default-service-name.ts 100.00% <100.00%> (+14.28%) ⬆️
🚀 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.

@overbalance overbalance force-pushed the overbalance/convert-to-runtime branch 3 times, most recently from ec46c10 to 9a1d2ff Compare December 29, 2025 19:23
Comment thread packages/opentelemetry-resources/test/default-service-name.test.ts Outdated
Comment thread packages/opentelemetry-resources/test/default-service-name.test.ts Outdated
@overbalance overbalance requested a review from cjihrig January 5, 2026 17:46
Comment thread packages/opentelemetry-resources/test/default-service-name.test.ts Outdated
@overbalance overbalance requested a review from cjihrig January 5, 2026 18:01
@cjihrig
Copy link
Copy Markdown
Contributor

cjihrig commented Jan 5, 2026

Unofficial LGTM, thanks!

@overbalance
Copy link
Copy Markdown
Contributor Author

@cjihrig I unofficially accept 😅

@overbalance overbalance disabled auto-merge January 9, 2026 16:24
typeof process.argv0 === 'string' &&
process.argv0.length > 0
? `unknown_service:${process.argv0}`
: 'unknown_service';
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.

I asked at #6208 (comment) whether potentially using global.process?.argv0 would suffice to avoid whatever static analysis warnings.

It would also be nice to have specific examples of these static analysis warnings to help for motivation in future maintenance.

@overbalance overbalance changed the title refactor(opentelemetry-resources): use runtime check for default service name refactor(resources): use runtime check for default service name Jan 9, 2026
@overbalance overbalance requested a review from trentm January 9, 2026 21:21
@overbalance
Copy link
Copy Markdown
Contributor Author

overbalance commented Jan 9, 2026

@trentm

  1. I added a NextJS bundler-test. Let me know if we should keep it.
  2. I updated the code to use try/catch. In envs where it throws, Claude suggested the cost is microseconds.

Comment thread packages/opentelemetry-resources/test/Resource.test.ts Outdated
Comment thread packages/opentelemetry-resources/test/Resource.test.ts Outdated
Comment thread bundler-tests/browser/nextjs-vercel/package.json Outdated
Comment thread bundler-tests/browser/nextjs-vercel/package.json Outdated
@trentm
Copy link
Copy Markdown
Contributor

trentm commented Jan 13, 2026

  • edge runtime scenarios

Does the added vercel bundler test actually test a failure here?

I reverted the implementation:

diff --git a/packages/opentelemetry-resources/src/default-service-name.ts b/packages/opentelemetry-resources/src/default-service-name.ts
index 88b236379..083e7ddb5 100644
--- a/packages/opentelemetry-resources/src/default-service-name.ts
+++ b/packages/opentelemetry-resources/src/default-service-name.ts
@@ -14,26 +14,18 @@
  * limitations under the License.
  */

-let serviceName: string | undefined;
+const DEFAULT_SERVICE_NAME =
+  typeof process === 'object' &&
+  typeof process.argv0 === 'string' &&
+  process.argv0.length > 0
+    ? `unknown_service:${process.argv0}`
+    : 'unknown_service';

-/**
- * Returns the default service name for OpenTelemetry resources.
- * In Node.js environments, returns "unknown_service:<process.argv0>".
- * In browser/edge environments, returns "unknown_service".
- */
 export function defaultServiceName(): string {
-  if (serviceName === undefined) {
-    try {
-      const argv0 = globalThis.process.argv0;
-      serviceName = argv0 ? `unknown_service:${argv0}` : 'unknown_service';
-    } catch {
-      serviceName = 'unknown_service';
-    }
-  }
-  return serviceName;
+  return DEFAULT_SERVICE_NAME;
 }

 /** @internal For testing purposes only */
 export function _clearDefaultServiceNameCache(): void {
-  serviceName = undefined;
+  // serviceName = undefined;
 }

re-ran npm run compile in the resource package dir, then ran the bundler tests. They pass:

% npm run test:bundle

> opentelemetry@0.1.0 test:bundle
> nx run-many -t test:bundle



   ✔  nx run nextjs-vercel-bundle-test:test:bundle
   ✔  nx run webpack-4-bundle-test:test:bundle
   ✔  nx run webpack-5-bundle-test:test:bundle

——————————————————————————————————————————————

 NX   Successfully ran target test:bundle for 3 projects (5s)

@overbalance overbalance force-pushed the overbalance/convert-to-runtime branch from 255c905 to 6b84134 Compare January 14, 2026 02:32
@overbalance
Copy link
Copy Markdown
Contributor Author

@trentm I added 2 edge runtime integration tests that fail on build warnings. I tested them by replacing globalThis.process with process in defaultServiceName():

Screenshot 2026-01-13 at 8 41 17 PM

Next 15:
Screenshot 2026-01-13 at 8 39 52 PM

Next 16:
Screenshot 2026-01-13 at 8 40 35 PM

@overbalance overbalance requested a review from trentm January 14, 2026 02:50
@overbalance overbalance force-pushed the overbalance/convert-to-runtime branch from 506840f to 722af49 Compare January 14, 2026 02:59
@overbalance
Copy link
Copy Markdown
Contributor Author

overbalance commented Jan 14, 2026

@overbalance overbalance force-pushed the overbalance/convert-to-runtime branch from 494c163 to 4acde2a Compare January 15, 2026 19:09
@overbalance overbalance requested a review from a team as a code owner January 15, 2026 19:09
@trentm
Copy link
Copy Markdown
Contributor

trentm commented Jan 16, 2026

The functional change looks good. IIUC, this is blocking some folks working on browsers, so we should get this in.
I have a couple Qs on the bundler-tests added, but I'm happy to get this in quickly and help get a release out, if that is needed. Please let me know.

next-16 bundler test

@overbalance Do I have it right that the next-16 bundler test is effectively a test of using Turbopack? ... I guess is about whatever the default config of Turbopack is in next-16. For all I know turbopack only has a life together with next@16. If so, this bundler test sounds good to me.

Output from the linked example test failure:

  ▲ Next.js 16.1.1 (Turbopack)
  
    Creating an optimized production build ...
  Turbopack build encountered 1 warnings:
  ./packages/opentelemetry-resources/build/esm/default-service-name.js:25:27
  Ecmascript file had an error
    23 |     if (serviceName === undefined) {
    24 |         try {
  > 25 |             const argv0 = process.argv0;
       |                           ^^^^^^^^^^^^^
    26 |             serviceName = argv0 ? `unknown_service:${argv0}` : 'unknown_service';
    27 |         }
    28 |         catch {

next-15 bundler test

So this is effectively a test of webpack.

     ▲ Next.js 15.5.9
  
     Creating an optimized production build ...
  MiddlewarePlugin: A Node.js API is used (process.argv0 at line: 23) which is not supported in the Edge Runtime.
  Learn more: https://nextjs.org/docs/api-reference/edge-runtime
  <w> [webpack.cache.PackFileCacheStrategy] Serializing big strings (139kiB) impacts deserialization performance (consider using Buffer instead and decode when needed)
  Failed to compile.
  
  Error: Webpack build has warnings

Do you think this could be done by tweaking the existing webpack-5 bundler test?
I tried to do this: to get standalone webpack usage to care/warn/error/something when bundling some JS code using process.argv0 and when targetting web. The following happily made me a bundle that'll error out in a browser:

npx webpack --fail-on-warnings --mode development --target web

So, I've no idea how next is invoking/configuring webpack. Sigh.

(Per my message above about getting this in, I'd be happy to get this in and discuss the next-15 bundler test in a follow-up PR.)

Copy link
Copy Markdown
Contributor

@trentm trentm left a comment

Choose a reason for hiding this comment

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

LGTM.

Happy to get this in, though I do have a Q about the bundler tests.
Let me know if we should just get this in right away at get a release out. I'm a little unsure of the urgency.

@overbalance
Copy link
Copy Markdown
Contributor Author

Do you think this could be done by tweaking the existing webpack-5 bundler test?

@trentm Next is doing a little bit of magic here beyond standard webpack options, like fully rejecting node api access. There may be a way to have smaller tests that leverage https://edge-runtime.vercel.app/packages/jest-environment but I haven't explored it yet.

@overbalance
Copy link
Copy Markdown
Contributor Author

Oh, and regarding the urgency I would ask @chargome and @andreiborza for input. It looks like Sentry had to prebundle to keep edge runtime support.

@andreiborza
Copy link
Copy Markdown

@overbalance correct, we ended up bundling the package in and duplicating apis from other SDKs. We are not blocked by this since we have this work-around but it's rather brittle and we'd love to remove the duplication and bundling again.

Thanks again for taking care of this 🙏

@overbalance overbalance added this pull request to the merge queue Jan 16, 2026
Merged via the queue into open-telemetry:main with commit b513292 Jan 16, 2026
27 checks passed
@overbalance overbalance deleted the overbalance/convert-to-runtime branch January 16, 2026 15:22
return DEFAULT_SERVICE_NAME;
if (serviceName === undefined) {
try {
const argv0 = globalThis.process.argv0;
Copy link
Copy Markdown

@andreiborza andreiborza Jan 19, 2026

Choose a reason for hiding this comment

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

@overbalance this is actually still preventing our builds to go through 😓

we still get the same warnings as before:

./node_modules/.pnpm/@opentelemetry+resources@2.4.0_@opentelemetry+api@1.9.0/node_modules/@opentelemetry/resources/build/esm/default-service-name.js A Node.js API is used (process.argv0 at line: 19) which is not supported in the Edge Runtime.

Which I guess makes sense given this is statically analysed.

Oops, hasn't been released yet. My bad 😅.

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.

No worries. Give it a shot now @andreiborza

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It worked: getsentry/sentry-javascript#18934

Thanks for taking care of this!

Copy link
Copy Markdown
Contributor Author

@overbalance overbalance Jan 23, 2026

Choose a reason for hiding this comment

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

I saw your comment on another PR about unclear Edge Runtime support. The Browser SIG has not discussed it but I'll bring it up on Slack to see if the JS SIG has. Feel free to join us there.

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.

4 participants