Skip to content

ref(node): Improve span flushing #16577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open

Conversation

mydea
Copy link
Member

@mydea mydea commented Jun 13, 2025

Follow up to #16416, slightly moving things around and making some small improvements to performance and logic (some not related to that PR but another improvement we noticed when BYK looked int this):

  1. Actually debounce the flushing properly, including a maxWait of 100ms. Previously, it was technically possible to debounce flushing forever, if a span was flushed every ms. Now, at least after 100ms it should always flush.
  2. Simplify overhead by avoid checking for the 5min timeout of sent spans. As this check isSpanAlreadySent has been called for every single span that ended, it is quite high impact, and meant a little bit of additional work (although it should not necessarily run too often, but still). Instead, we now simply check for map.has(id) which should be good enough for what we want to achieve, IMHO. We still clean up the sent spans when flushing, so old stuff should still go away eventually.
  3. Made stuff that is not needed as public API private on the span exporter class. this is technically breaking but I think it is OK, this should not be public API surface as it does not need to be called from outside.

For this I moved the already existing debounce function from replay to core. We re-implement this in replay with a different setTimeout impl. which is needed for some angular stuff.

@mydea mydea requested review from BYK, AbhiPrasad and andreiborza June 13, 2025 07:34
@mydea mydea self-assigned this Jun 13, 2025
@mydea mydea requested a review from a team as a code owner June 13, 2025 07:34
Copy link
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 23.99 kB - -
@sentry/browser - with treeshaking flags 23.76 kB - -
@sentry/browser (incl. Tracing) 38.69 kB - -
@sentry/browser (incl. Tracing, Replay) 76.81 kB +0.04% +28 B 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 69.9 kB +0.05% +28 B 🔺
@sentry/browser (incl. Tracing, Replay with Canvas) 81.57 kB +0.04% +28 B 🔺
@sentry/browser (incl. Tracing, Replay, Feedback) 93.63 kB +0.03% +28 B 🔺
@sentry/browser (incl. Feedback) 40.73 kB - -
@sentry/browser (incl. sendFeedback) 28.7 kB - -
@sentry/browser (incl. FeedbackAsync) 33.59 kB - -
@sentry/react 25.76 kB - -
@sentry/react (incl. Tracing) 40.67 kB - -
@sentry/vue 28.36 kB - -
@sentry/vue (incl. Tracing) 40.55 kB -0.01% -1 B 🔽
@sentry/svelte 24.01 kB - -
CDN Bundle 25.48 kB - -
CDN Bundle (incl. Tracing) 38.77 kB - -
CDN Bundle (incl. Tracing, Replay) 74.67 kB +0.05% +32 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) 80.13 kB +0.05% +34 B 🔺
CDN Bundle - uncompressed 74.48 kB - -
CDN Bundle (incl. Tracing) - uncompressed 114.89 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 228.95 kB +0.04% +84 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 241.77 kB +0.04% +84 B 🔺
@sentry/nextjs (client) 42.33 kB - -
@sentry/sveltekit (client) 39.17 kB - -
@sentry/node 150.61 kB +0.08% +106 B 🔺
@sentry/node - without tracing 98.49 kB +0.11% +106 B 🔺
@sentry/aws-serverless 124.25 kB +0.09% +107 B 🔺

View base workflow run

this._flushTimeout = setTimeout(() => {
this.flush();
}, 1);
if (!localParentId || this._sentSpans.has(localParentId)) {
Copy link
Member

Choose a reason for hiding this comment

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

should we also run cleanup on sent spans here (the _flushSentSpanCache method)?

The _sentSpans is checked for both flush and export, but only cleaned up in flush.

};
type CallbackFunction = () => unknown;
type DebounceOptions = {
maxWait?: number;
Copy link
Member

Choose a reason for hiding this comment

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

maxWait needs a comment.

Comment on lines 129 to 131
const finishedSpans: ReadableSpan[] = this._finishedSpanBuckets.flatMap(bucket =>
bucket ? Array.from(bucket.spans) : [],
);
Copy link
Member

Choose a reason for hiding this comment

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

The intermediate array creation + flatMap usage is probably pretty expensive as well.

Suggested change
const finishedSpans: ReadableSpan[] = this._finishedSpanBuckets.flatMap(bucket =>
bucket ? Array.from(bucket.spans) : [],
);
const finishedSpans: ReadableSpan[] = [];
for (const bucket of this._finishedSpanBuckets) {
if (bucket) {
for (const span of bucket.spans) {
finishedSpans.push(span);
}
}
}

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