Skip to content

Conversation

@Lms24
Copy link
Member

@Lms24 Lms24 commented Nov 12, 2025

This PR adds support for scope attributes on logs. For now, only primitive attribute values are supported despite type declarations of Scope.setAttribute(s) also allowing array attribute values. The reason for this limited support is that Relay still discards array attribute values. Therefore, our serialization strategy for now is:

  • As previously: We continue to stringify non-primitive values for log/metric attributes
  • New: We apply only primitive scope attributes on logs/metrics and discard any non-primitive values
  • Future: We'll uniformly handle arrays (properly) in v11 (i.e. no longer stringify them), i.e. treat all attributes equally.

Usage Example

Sentry.getCurrenScope().setAttribute('user_is_admin', true);

Sentry.logger.info(`user ${user.id} logged in`, { activeSince: 100 });
Sentry.logger.warn('unsupported version');

// `user_is_admin` attribute is applied to both logs

Some behavior notes:

  • Scope attributes are merged from all active scopes (current, isolation, global scopes) when the log is captured
  • Log attributes have precedence over scope attributes

closes #18159

@Lms24 Lms24 changed the base branch from develop to lms/feat-core-scope-setAttributes November 12, 2025 11:46
@github-actions
Copy link
Contributor

github-actions bot commented Nov 12, 2025

size-limit report 📦

Path Size % Change Change
@sentry/browser 24.82 kB +0.07% +16 B 🔺
@sentry/browser - with treeshaking flags 23.32 kB +0.08% +17 B 🔺
@sentry/browser (incl. Tracing) 41.57 kB +0.05% +18 B 🔺
@sentry/browser (incl. Tracing, Profiling) 46.18 kB +0.05% +20 B 🔺
@sentry/browser (incl. Tracing, Replay) 80 kB +0.03% +20 B 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 69.72 kB +0.03% +17 B 🔺
@sentry/browser (incl. Tracing, Replay with Canvas) 84.67 kB +0.03% +20 B 🔺
@sentry/browser (incl. Tracing, Replay, Feedback) 96.91 kB +0.03% +20 B 🔺
@sentry/browser (incl. Feedback) 41.54 kB +0.06% +21 B 🔺
@sentry/browser (incl. sendFeedback) 29.51 kB +0.07% +19 B 🔺
@sentry/browser (incl. FeedbackAsync) 34.5 kB +0.06% +20 B 🔺
@sentry/react 26.54 kB +0.08% +19 B 🔺
@sentry/react (incl. Tracing) 43.77 kB +0.04% +17 B 🔺
@sentry/vue 29.29 kB +0.07% +18 B 🔺
@sentry/vue (incl. Tracing) 43.38 kB +0.05% +21 B 🔺
@sentry/svelte 24.84 kB +0.07% +16 B 🔺
CDN Bundle 27.25 kB +0.09% +24 B 🔺
CDN Bundle (incl. Tracing) 42.24 kB +0.05% +20 B 🔺
CDN Bundle (incl. Tracing, Replay) 78.76 kB +0.03% +19 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) 84.22 kB +0.03% +22 B 🔺
CDN Bundle - uncompressed 80.06 kB +0.05% +34 B 🔺
CDN Bundle (incl. Tracing) - uncompressed 125.41 kB +0.03% +34 B 🔺
CDN Bundle (incl. Tracing, Replay) - uncompressed 241.44 kB +0.02% +34 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 254.2 kB +0.02% +34 B 🔺
@sentry/nextjs (client) 45.99 kB +0.04% +17 B 🔺
@sentry/sveltekit (client) 41.95 kB +0.05% +18 B 🔺
@sentry/node-core 51.61 kB +0.04% +16 B 🔺
@sentry/node 161.5 kB +0.02% +19 B 🔺
@sentry/node - without tracing 93.05 kB +0.03% +19 B 🔺
@sentry/aws-serverless 108.56 kB +0.02% +16 B 🔺

View base workflow run

@github-actions
Copy link
Contributor

github-actions bot commented Nov 12, 2025

node-overhead report 🧳

Note: This is a synthetic benchmark with a minimal express app and does not necessarily reflect the real-world performance impact in an application.

Scenario Requests/s % of Baseline Prev. Requests/s Change %
GET Baseline 8,802 - 8,535 +3%
GET With Sentry 1,749 20% 1,608 +9%
GET With Sentry (error only) 6,127 70% 5,850 +5%
POST Baseline 1,205 - 1,166 +3%
POST With Sentry 595 49% 546 +9%
POST With Sentry (error only) 1,055 88% 1,023 +3%
MYSQL Baseline 3,267 - 3,188 +2%
MYSQL With Sentry 489 15% 427 +15%
MYSQL With Sentry (error only) 2,674 82% 2,607 +3%

View base workflow run

@Lms24 Lms24 force-pushed the lms/feat-core-logs-apply-scope-attributes branch 2 times, most recently from 52339f2 to b97b3f4 Compare November 18, 2025 12:15
@Lms24 Lms24 force-pushed the lms/feat-core-scope-setAttributes branch 2 times, most recently from a3e204d to 205752c Compare November 18, 2025 16:51
@Lms24 Lms24 force-pushed the lms/feat-core-logs-apply-scope-attributes branch from 5053159 to e5785ea Compare November 18, 2025 16:52
@Lms24 Lms24 force-pushed the lms/feat-core-scope-setAttributes branch 2 times, most recently from fbbd768 to 04d75b1 Compare November 21, 2025 14:23
Base automatically changed from lms/feat-core-scope-setAttributes to develop November 21, 2025 14:42
@Lms24 Lms24 force-pushed the lms/feat-core-logs-apply-scope-attributes branch from e5785ea to 561085c Compare November 21, 2025 14:47
@Lms24 Lms24 force-pushed the lms/feat-core-logs-apply-scope-attributes branch from e575427 to 5388d82 Compare December 12, 2025 11:52
@Lms24 Lms24 self-assigned this Dec 12, 2025
@Lms24
Copy link
Member Author

Lms24 commented Dec 12, 2025

UPDATE: See PR description for updated behaviour

OK, so we still have 2 problems and a minor inconvenience here:

  1. Relay still drops array attributes. Our scope attribute API supports adding arrays but these attributes would simply not make it to Sentry. This is not really a problem for scope attributes (since we don't document them yet) but if we switch log attributes to the same serialization logic as for scope attributes, array attributes on logs would no longer be stringified but also dropped.
  2. Suppose Relay supports array attributes, we'd still have a somewhat breaking change in that log array attributes would no longer be stringified but sent as arrays. I don't think this is the end of the world but we should keep this in mind and tell the logs folks about it
  3. (minor inconvenience) units are also dropped by Relay. Again only a proble for scope attributes for now but we could easily add support for log attributes as well. The values of the attributes with units are still forwarded correctly.

@Lms24
Copy link
Member Author

Lms24 commented Dec 16, 2025

To at least offer partial support, we settled on only serializing primitive, and discarding non-primitive scope attribute values. I update the PR and the PR description accordingly.

@Lms24 Lms24 marked this pull request as ready for review December 16, 2025 12:59
@Lms24 Lms24 requested review from AbhiPrasad and cleptric December 16, 2025 13:00
* @param value - The value of the log attribute.
* @returns The serialized log attribute.
*/
export function logAttributeToSerializedLogAttribute(value: unknown): SerializedLogAttributeValue {
Copy link
Member Author

Choose a reason for hiding this comment

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

Since we have a fallback parameter in attributeValueToTypedAttributeValue, we can get rid of this logic. For now the bundle size savings are minimal but once we migrate logs to the new unified function, it should pay off.


mergeAndOverwriteScopeData(data, 'extra', extra);
mergeAndOverwriteScopeData(data, 'tags', tags);
mergeAndOverwriteScopeData(data, 'attributes', attributes);
Copy link
Member Author

Choose a reason for hiding this comment

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

This was still missing in #18165

@Lms24 Lms24 requested review from chargome and s1gr1d December 16, 2025 13:05
attributes: {
...commonAttributes,
log_attr: { value: 'log_attr_1', type: 'string' },
},
Copy link

Choose a reason for hiding this comment

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

Bug: Node integration test uses partial matching for attributes (Bugbot Rules)

Flagging per review rules: The test uses toMatchObject (via assertSentryLogContainer) which is a relaxed assertion. The test scenario sets array_attr: [1, 2, 3] on the global scope, which should NOT appear in logs (since non-primitive scope attributes are discarded). However, the test only verifies that expected attributes are present, not that array_attr is absent. If a regression caused non-primitive scope attributes to leak into logs, this test would not catch it. The browser integration test correctly uses toEqual for exact matching.

Fix in Cursor Fix in Web

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.

Apply Scope attributes to logs

3 participants