Skip to content

fix(opentelemetry): Respect OTEL_SERVICE_NAME, OTEL_RESOURCE_ATTRIBUTES#20509

Open
isaacs wants to merge 1 commit intodevelopfrom
isaacschlueter/js-2280-respect-otel_service_name-and-otel_resource_attributes
Open

fix(opentelemetry): Respect OTEL_SERVICE_NAME, OTEL_RESOURCE_ATTRIBUTES#20509
isaacs wants to merge 1 commit intodevelopfrom
isaacschlueter/js-2280-respect-otel_service_name-and-otel_resource_attributes

Conversation

@isaacs
Copy link
Copy Markdown
Member

@isaacs isaacs commented Apr 25, 2026

This uses the string passed into getSentryResource as a fallback, preferring instead to use the value in env.OTEL_SERVICE_NAME if set, or the service.name field in the comma-delimited key=value pairs in env.OTEL_RESOURCE_ATTRIBUTES pairs.

Additional env.OTEL_RESOURCE_ATTRIBUTES are also attached to the resource attributes.

fix: js-2280
fix: #20502

  • If you've added code that should be tested, please add tests.
  • Ensure your code lints and the test suite passes (yarn lint) & (yarn test).
  • Link an issue if there is one related to your pull request. If no issue is linked, one will be auto-generated and linked.

Closes #20502

This uses the string passed into `getSentryResource` as a fallback,
preferring instead to use the value in `env.OTEL_SERVICE_NAME` if set,
or the `service.name` field in the comma-delimited key=value pairs in
`env.OTEL_RESOURCE_ATTRIBUTES` pairs.

Additional `env.OTEL_RESOURCE_ATTRIBUTES` are also attached to the
resource attributes.

fix: js-2280
fix: #20502
@isaacs isaacs requested a review from mydea April 25, 2026 01:08
@linear-code
Copy link
Copy Markdown

linear-code Bot commented Apr 25, 2026

Comment on lines +51 to +57
for (const pair of raw.split(',')) {
const eq = pair.trim().indexOf('=');
if (eq === -1) {
continue;
}
const key = pair.substring(0, eq).trim();
const value = pair.substring(eq + 1).trim();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: The code calculates an index on a trimmed string but applies it to the untrimmed string, causing incorrect parsing of resource attributes with leading spaces.
Severity: HIGH

Suggested Fix

Trim the pair string first and store it in a variable. Use this new trimmed variable for both finding the index of the = separator and for the subsequent substring calls to extract the key and value.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: packages/opentelemetry/src/resource.ts#L51-L57

Potential issue: In `parseOtelResourceAttributes`, the index of the `=` separator is
calculated on a trimmed string (`pair.trim().indexOf('=')`) but is then applied to the
original, untrimmed `pair` string for substring operations. If an attribute pair has
leading whitespace, such as from an input like `"key1=value1, key2=value2"`, the
resulting pair `" key2=value2"` will be parsed incorrectly. The key will be truncated
(e.g., to `"key"`) and the value will be malformed (e.g., to `"=value2"`), causing
silent corruption of telemetry attributes.

Did we get this right? 👍 / 👎 to inform future reviews.

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 65f6897. Configure here.

continue;
}
const key = pair.substring(0, eq).trim();
const value = pair.substring(eq + 1).trim();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Leading whitespace corrupts parsed key/value pairs

High Severity

In parseOtelResourceAttributes, the index for splitting key=value pairs is calculated on a trimmed string but applied to the untrimmed input. This causes leading whitespace to corrupt resource attribute keys and values, silently affecting attributes like service.name when parsing OTEL_RESOURCE_ATTRIBUTES.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 65f6897. Configure here.

@github-actions
Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 25.96 kB - -
@sentry/browser - with treeshaking flags 24.44 kB - -
@sentry/browser (incl. Tracing) 43.89 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 45.53 kB - -
@sentry/browser (incl. Tracing, Profiling) 48.84 kB - -
@sentry/browser (incl. Tracing, Replay) 83.09 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 72.59 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 87.77 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 100.38 kB - -
@sentry/browser (incl. Feedback) 43.18 kB - -
@sentry/browser (incl. sendFeedback) 30.77 kB - -
@sentry/browser (incl. FeedbackAsync) 35.93 kB - -
@sentry/browser (incl. Metrics) 27.25 kB - -
@sentry/browser (incl. Logs) 27.38 kB - -
@sentry/browser (incl. Metrics & Logs) 28.07 kB - -
@sentry/react 27.72 kB - -
@sentry/react (incl. Tracing) 46.13 kB - -
@sentry/vue 30.81 kB - -
@sentry/vue (incl. Tracing) 45.71 kB - -
@sentry/svelte 25.98 kB - -
CDN Bundle 28.66 kB - -
CDN Bundle (incl. Tracing) 46.12 kB - -
CDN Bundle (incl. Logs, Metrics) 30.03 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 47.17 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 68.99 kB - -
CDN Bundle (incl. Tracing, Replay) 83.19 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 84.22 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 89 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 90.06 kB - -
CDN Bundle - uncompressed 83.91 kB - -
CDN Bundle (incl. Tracing) - uncompressed 137.82 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 88.06 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 141.23 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 211.63 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 255.26 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 258.66 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 268.96 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 272.35 kB - -
@sentry/nextjs (client) 48.62 kB - -
@sentry/sveltekit (client) 44.33 kB - -
@sentry/node-core 58.52 kB +0.02% +8 B 🔺
@sentry/node 175.99 kB +0.09% +156 B 🔺
@sentry/node - without tracing 98.49 kB +0.19% +178 B 🔺
@sentry/aws-serverless 115.65 kB +0.12% +128 B 🔺

View base workflow run

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Respect OTEL_SERVICE_NAME and OTEL_RESOURCE_ATTRIBUTES

1 participant