Skip to content

feat(core): Add span serialization utilities#19140

Merged
Lms24 merged 11 commits intolms/feat-span-firstfrom
lms/span-first-pr7-span-serialization
Feb 4, 2026
Merged

feat(core): Add span serialization utilities#19140
Lms24 merged 11 commits intolms/feat-span-firstfrom
lms/span-first-pr7-span-serialization

Conversation

@Lms24
Copy link
Member

@Lms24 Lms24 commented Feb 3, 2026

This PR adds span JSON conversion and serialization helpers for span streaming:

  • spanToStreamedSpanJSON: Converts a Span instance to a JSON object used as intermediate representation as outlined in feat(core/spans): Add span v2 and envelope type definitions #19100
    • Adds SentrySpan::getStreamedSpanJSON method to convert our own spans
    • Directly converts any OTel spans
    • This is analogous to how spanToJSON works today.
  • spanJsonToSerializedSpan: Converts a StreamedSpanJSON into the final SerializedSpan to be sent to Sentry.

This PR also adds unit tests for both helpers.

ref #17836

This PR adds utilities for serializing spans to the new V2 format:

- `spanToV2JSON`: Converts a span to SerializedSpan (V2 format)
- `getV2SpanLinks`: Converts span links with serialized attributes
- `getV2StatusMessage`: Converts status to 'ok' | 'error'
- `INTERNAL_getSegmentSpan`: Renamed from getRootSpan (with alias kept)

These utilities are needed for the span streaming feature and will be
used by subsequent PRs to serialize spans before sending.

Co-authored-by: Cursor <cursoragent@cursor.com>
@Lms24 Lms24 marked this pull request as draft February 3, 2026 14:54
@github-actions
Copy link
Contributor

github-actions bot commented Feb 3, 2026

Codecov Results 📊


Generated by Codecov Action

@github-actions
Copy link
Contributor

github-actions bot commented Feb 3, 2026

size-limit report 📦

Path Size % Change Change
@sentry/browser 25.44 kB added added
@sentry/browser - with treeshaking flags 23.91 kB added added
@sentry/browser (incl. Tracing) 42.39 kB added added
@sentry/browser (incl. Tracing, Profiling) 47.04 kB added added
@sentry/browser (incl. Tracing, Replay) 81.07 kB added added
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 70.64 kB added added
@sentry/browser (incl. Tracing, Replay with Canvas) 85.77 kB added added
@sentry/browser (incl. Tracing, Replay, Feedback) 97.94 kB added added
@sentry/browser (incl. Feedback) 42.16 kB added added
@sentry/browser (incl. sendFeedback) 30.12 kB added added
@sentry/browser (incl. FeedbackAsync) 35.13 kB added added
@sentry/browser (incl. Metrics) 26.55 kB added added
@sentry/browser (incl. Logs) 26.7 kB added added
@sentry/browser (incl. Metrics & Logs) 27.36 kB added added
@sentry/react 27.17 kB added added
@sentry/react (incl. Tracing) 44.64 kB added added
@sentry/vue 30.03 kB added added
@sentry/vue (incl. Tracing) 44.2 kB added added
@sentry/svelte 25.45 kB added added
CDN Bundle 27.98 kB added added
CDN Bundle (incl. Tracing) 43.14 kB added added
CDN Bundle (incl. Logs, Metrics) 28.81 kB added added
CDN Bundle (incl. Tracing, Logs, Metrics) 43.98 kB added added
CDN Bundle (incl. Replay, Logs, Metrics) 67.76 kB added added
CDN Bundle (incl. Tracing, Replay) 79.88 kB added added
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 80.74 kB added added
CDN Bundle (incl. Tracing, Replay, Feedback) 85.34 kB added added
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 86.22 kB added added
CDN Bundle - uncompressed 81.81 kB added added
CDN Bundle (incl. Tracing) - uncompressed 127.81 kB added added
CDN Bundle (incl. Logs, Metrics) - uncompressed 84.65 kB added added
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 130.65 kB added added
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 208.03 kB added added
CDN Bundle (incl. Tracing, Replay) - uncompressed 244.42 kB added added
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 247.24 kB added added
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 257.22 kB added added
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 260.03 kB added added
@sentry/nextjs (client) 46.98 kB added added
@sentry/sveltekit (client) 42.77 kB added added
@sentry/node-core 52.18 kB added added
@sentry/node 166.36 kB added added
@sentry/node - without tracing 93.96 kB added added
@sentry/aws-serverless 109.48 kB added added

@github-actions
Copy link
Contributor

github-actions bot commented Feb 3, 2026

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 9,316 - - added
GET With Sentry 1,749 19% - added
GET With Sentry (error only) 6,091 65% - added
POST Baseline 1,199 - - added
POST With Sentry 591 49% - added
POST With Sentry (error only) 1,041 87% - added
MYSQL Baseline 3,297 - - added
MYSQL With Sentry 449 14% - added
MYSQL With Sentry (error only) 2,642 80% - added

@Lms24 Lms24 self-assigned this Feb 3, 2026
@Lms24 Lms24 marked this pull request as ready for review February 3, 2026 15:47
Copy link

@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.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

@Lms24 Lms24 changed the title feat(core): Span serialization utilities (spanToV2JSON) feat(core): Add span serialization utilities (spanToV2JSON) Feb 3, 2026
@JPeer264
Copy link
Member

JPeer264 commented Feb 4, 2026

I am now a little confused if it is spanToV2JSON or spanToStreamedSpanJSON. If spanToV2Json is correct, then you can ignore my comments

@Lms24 Lms24 mentioned this pull request Feb 4, 2026
22 tasks
@Lms24 Lms24 changed the title feat(core): Add span serialization utilities (spanToV2JSON) feat(core): Add span serialization utilities Feb 4, 2026
Co-authored-by: Jan Peer Stöcklmair <jan.peer@sentry.io>
* of SDK code. If you need to get a JSON representation of a span,
* use `spanToV2JSON(span)` instead.
*/
public getStreamedSpanJSON(): StreamedSpanJSON {
Copy link
Member

Choose a reason for hiding this comment

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

To make this even more visible that this is an internal API: Should this maybe prefixed with _INTERNAL_?

Copy link
Member Author

Choose a reason for hiding this comment

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

Users should never interact with SentrySpan directly but leave it up to start*Span* APIs which kind of Span (as in interface) they get back or work with.

I oriented myself here primarily on the already existing getSpanJSON method. In light of keeping the name short for bundle size, I'd tend to keep it that way, unless you think we should still do it?

@Lms24
Copy link
Member Author

Lms24 commented Feb 4, 2026

@JPeer264

I am now a little confused if it is spanToV2JSON or spanToStreamedSpanJSON. If spanToV2Json is correct, then you can ignore my comments

Fully understandable! I removed a bunch of the remaining v2JSON jargon.
My general thinking is:

  • whatever is (potentially) user facing, should be called "streamedSpan"
  • only internally once we generate an envelope with the final serialized spans, we use "v2", to distinguish from the pre-existing v1 span envelope.

@JPeer264
Copy link
Member

JPeer264 commented Feb 4, 2026

Not sure if a mixed jargon is helping. Especially when should we use v2 vs streaming.

However, merging this in won't harm. Internally we can always rename (it's just the muscle memory which I'm worried about)

@Lms24
Copy link
Member Author

Lms24 commented Feb 4, 2026

ok you convinced me. I'll go with "streamedSpan" everywhere then. Will make some changes to this PR and open another one to adjust the envelope type names

@Lms24 Lms24 merged commit 4eeed05 into lms/feat-span-first Feb 4, 2026
418 of 423 checks passed
@Lms24 Lms24 deleted the lms/span-first-pr7-span-serialization branch February 4, 2026 16:00
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.

3 participants