Skip to content

feat(core): Always flush streamed span traces after segment end by default - #23714

Open
mydea wants to merge 8 commits into
developfrom
feat/share-span-streaming-integration
Open

feat(core): Always flush streamed span traces after segment end by default#23714
mydea wants to merge 8 commits into
developfrom
feat/share-span-streaming-integration

Conversation

@mydea

@mydea mydea commented Aug 28, 2026

Copy link
Copy Markdown
Member

The browser (browserSpanStreaming.ts) and server (spanStreaming.ts) span streaming integrations were near-identical: the afterSpanEnd buffering was byte-for-byte the same, and they only differed in the eager-flush trigger. This collapses them into a single core integration, and makes flushing a trace shortly after its segment span ends the default for all runtimes, browser and server alike.

Both install sites now call spanStreamingIntegration() with no arguments: browserSpanApi._INTERNAL_ensureBrowserSpanStreaming and ServerRuntimeClient share one behavior. A trace is flushed:

  • eagerly on flushTraceSpans (the Cloudflare drain),
  • 500ms after its segment span ends, and
  • otherwise on the SpanBuffer's own timeout/size thresholds.

Why default segment-end flushing on for the server too

Previously the server relied solely on the buffer thresholds and only the browser flushed on segment end, to avoid the server emitting more, smaller envelopes and to keep the browser's 500ms timer from holding the Node event loop open. The event-loop concern is already handled by safeUnref on the timer (a no-op in the browser), so there's no longer a reason to gate the segment-end path by runtime — flushing traces timely once their segment ends is the behavior we want everywhere.

The flushOnSegmentEnd option is retained (now defaulting to true) so a runtime can still opt out and fall back to threshold-only flushing.

Why keep it in core rather than moving to browser-utils/server-utils

The integrations are consumed internally by core — ServerRuntimeClient auto-adds it, and browserSpanApi/idleSpan auto-install it (a deliberate tree-shaking indirection). Since browser-utils and server-utils both depend on @sentry/core, core cannot import back from them without a circular dependency. Collapsing in-core sidesteps that entirely.

Export change

The integration now lives in shared-exports as a single shared symbol (star-exported by all entries) instead of being duplicated in browser-exports and server-exports. Previously the two entries resolved to different symbols, making the name an ambiguous star export that index.ts disambiguated with an explicit re-export. Now that both resolve to the same symbol, that explicit line collided with the star exports (leaving the root namespace undefined) and is removed. The startSpan/startInactiveSpan/startSpanManual disambiguation stays, as those remain genuinely distinct browser/server variants.

Comment thread packages/core/src/integrations/spanStreaming.ts
Comment thread packages/core/src/integrations/spanStreaming.ts Outdated
@github-actions

github-actions Bot commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

⚠️ Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

Path Size % Change Change
@sentry/browser 28.56 kB - -
@sentry/browser - with treeshaking flags 26.92 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 26.82 kB - -
@sentry/browser (incl. Tracing) 48.67 kB +0.08% +35 B 🔺
@sentry/browser (incl. Tracing + Span Streaming) 48.67 kB +0.07% +34 B 🔺
@sentry/browser (incl. Tracing, Profiling) 51.58 kB +0.07% +35 B 🔺
@sentry/browser (incl. Tracing, Replay) 88.12 kB +0.05% +37 B 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 77.56 kB +0.05% +38 B 🔺
@sentry/browser (incl. Tracing, Replay with Canvas) 92.83 kB +0.05% +38 B 🔺
@sentry/browser (incl. Tracing, Replay, Feedback) 105.78 kB +0.04% +41 B 🔺
@sentry/browser (incl. Feedback) 46.05 kB - -
@sentry/browser (incl. sendFeedback) 33.62 kB - -
@sentry/browser (incl. FeedbackAsync) 38.73 kB - -
@sentry/browser (incl. Metrics) 29.51 kB - -
@sentry/browser (incl. Logs) 29.8 kB - -
@sentry/browser (incl. Metrics & Logs) 30.43 kB - -
@sentry/react 30.3 kB - -
@sentry/react (incl. Tracing) 50.87 kB +0.07% +35 B 🔺
@sentry/vue 35.73 kB +0.11% +39 B 🔺
@sentry/vue (incl. Tracing) 50.9 kB +0.08% +39 B 🔺
@sentry/svelte 28.59 kB - -
CDN Bundle 30.35 kB - -
CDN Bundle (incl. Tracing) 49.26 kB +0.05% +23 B 🔺
CDN Bundle (incl. Logs, Metrics) 32.58 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 51.15 kB +0.06% +28 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics) 73.17 kB - -
CDN Bundle (incl. Tracing, Replay) 86.75 kB +0.03% +26 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 88.63 kB +0.03% +25 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) 92.64 kB +0.03% +27 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 94.53 kB +0.03% +26 B 🔺
CDN Bundle - uncompressed 89.95 kB - -
CDN Bundle (incl. Tracing) - uncompressed 146.91 kB +0.07% +89 B 🔺
CDN Bundle (incl. Logs, Metrics) - uncompressed 96.24 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 152.6 kB +0.06% +89 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 225.41 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 266.41 kB +0.04% +89 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 272.08 kB +0.04% +89 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 280.11 kB +0.04% +89 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 285.78 kB +0.04% +89 B 🔺
@sentry/nextjs (client) 53.44 kB +0.06% +31 B 🔺
@sentry/sveltekit (client) 49.1 kB +0.07% +31 B 🔺
@sentry/core/server 65.19 kB +0.05% +31 B 🔺
@sentry/core/browser 51.83 kB +0.06% +29 B 🔺
@sentry/node 122.78 kB +0.05% +56 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 85.23 kB - -
@sentry/node - without tracing 87.74 kB +0.03% +25 B 🔺
@sentry/node - without channel injection 102.44 kB +0.06% +56 B 🔺
@sentry/aws-serverless 95.95 kB +0.08% +69 B 🔺
@sentry/cloudflare (withSentry) - minified 199.83 kB +0.08% +143 B 🔺
@sentry/cloudflare (withSentry) 496.56 kB +0.08% +367 B 🔺

View base workflow run

@mydea
mydea requested a review from Lms24 August 28, 2026 07:42
Comment thread packages/browser/src/integrations/browserSpanStreaming.ts Outdated
@mydea
mydea marked this pull request as ready for review August 28, 2026 08:42
@mydea
mydea requested a review from a team as a code owner August 28, 2026 08:42
@mydea
mydea requested review from msonnb and removed request for a team August 28, 2026 08:42
Comment thread packages/browser/src/integrations/browserSpanStreaming.ts Outdated

@Lms24 Lms24 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we can do something better here which is what we decided on last week but I didn't have time to implement yet: Let's just always flush at segment end (with the delay), regardless of environment.

In Python, we already made that change after noticing that the 5s flushing interval accumulates a lot of memory in high-throughput environments. Since we make one request per trace anyway, the number of requests shouldn't be drastically higher.

That is, as long as we can expect that traces are generally well-formed (i.e. child spans ending before their segment span). For malformed traces, with long-running child spans we would increase the request count. I think this is a fine tradeoff IMHO.

If you have (or anyone else has) concerns about this, I'd prefer keeping the two integrations separate because the merge increases bundle size for the browser version. Which was the reason why I opted against combining them initially.

@mydea

mydea commented Aug 28, 2026

Copy link
Copy Markdown
Member Author

I think we can do something better here which is what we decided on last week but I didn't have time to implement yet: Let's just always flush at segment end (with the delay), regardless of environment.

In Python, we already made that change after noticing that the 5s flushing interval accumulates a lot of memory in high-throughput environments. Since we make one request per trace anyway, the number of requests shouldn't be drastically higher.

That is, as long as we can expect that traces are generally well-formed (i.e. child spans ending before their segment span). For malformed traces, with long-running child spans we would increase the request count. I think this is a fine tradeoff IMHO.

If you have (or anyone else has) concerns about this, I'd prefer keeping the two integrations separate because the merge increases bundle size for the browser version. Which was the reason why I opted against combining them initially.

sounds good to me, so I'll just change this so that the default for flushing is true I'd say, so you can still opt out?

@mydea mydea changed the title ref(core): Merge browser and server spanStreamingIntegration into one shared integration feat(core): Always flush streamed span traces after segment end by default Aug 28, 2026
@mydea
mydea force-pushed the feat/share-span-streaming-integration branch from ea19f07 to 023fef5 Compare August 28, 2026 09:45
@mydea
mydea requested a review from a team as a code owner August 28, 2026 09:45
@mydea
mydea requested review from chargome and s1gr1d and removed request for a team August 28, 2026 09:45
Comment thread packages/core/src/integrations/spanStreaming.ts
Comment thread packages/core/test/integrations/spanStreaming.test.ts
Comment thread packages/core/src/integrations/spanStreaming.ts
mydea and others added 7 commits August 28, 2026 11:54
… shared integration

The browser and server span streaming integrations only differed in their
eager-flush trigger, so collapse them into a single core integration
parameterized by a `flushOnSegmentEnd` option. The browser install site
(`browserSpanApi`) passes `{ flushOnSegmentEnd: true }`; the server side
(`ServerRuntimeClient`) keeps the same call and behavior.

The integration is now exported from `shared-exports` (a single shared symbol
across all entries) instead of being duplicated in `browser-exports` and
`server-exports`. Because both entries previously resolved to *different*
symbols, the name was an ambiguous star export that `index.ts` disambiguated
explicitly; now that it resolves to the same symbol, that explicit re-export
collided with the star exports and is removed.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The `flushOnSegmentEnd` 500ms `setTimeout` now lives in shared core code and
`flushOnSegmentEnd` is a public option, so enabling it on a Node runtime could
keep a process alive until the timer fires. Wrap it in `safeUnref` (as the
`SpanBuffer` already does for its own flush timer); it's a no-op in the browser,
where this is the default path.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@mydea
mydea force-pushed the feat/share-span-streaming-integration branch from 023fef5 to 5f6ffb5 Compare August 28, 2026 09:54

@Lms24 Lms24 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for reworking this!

Small positive side-effect for our tests: Span streaming tests should now run faster, given we flush way more quickly.

Comment thread packages/core/src/integrations/spanStreaming.ts Outdated
Comment thread packages/core/src/integrations/spanStreaming.ts Outdated

@cursor cursor Bot left a comment

Copy link
Copy Markdown

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 5f6ffb5. Configure here.


export declare const linkedErrorsIntegration: typeof clientSdk.linkedErrorsIntegration;
export declare const contextLinesIntegration: typeof clientSdk.contextLinesIntegration;
export declare const spanStreamingIntegration: typeof clientSdk.spanStreamingIntegration;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Types drop public integration export

Medium Severity

Removing the explicit spanStreamingIntegration re-export from framework index.types.ts files drops it from the public type surface. TypeScript export * from both client and server omits colliding names, so consumers of packages such as @sentry/astro and @sentry/nextjs can no longer import it. This is flagged because the review rules treat removal of a publicly exported function without a deprecation notice as a breaking change. The same explicit re-export is still required for other shared core symbols such as withStaticSpan.

Additional Locations (2)
Fix in Cursor Fix in Web

Triggered by project rule: PR Review Guidelines for Cursor Bot

Reviewed by Cursor Bugbot for commit 5f6ffb5. Configure here.

Co-authored-by: Lukas Stracke <lukas.stracke@sentry.io>
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.

2 participants