Skip to content

fix(browser): Fix idle span ending #12306

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

Merged
merged 2 commits into from
May 31, 2024
Merged

fix(browser): Fix idle span ending #12306

merged 2 commits into from
May 31, 2024

Conversation

mydea
Copy link
Member

@mydea mydea commented May 31, 2024

I noticed here https://github.com/getsentry/sentry-javascript/pulls that some tests were becoming flaky. Investigating this some more, I figured out what was happening:

  1. For idle spans, when the span is ended (in on('spanEnded'), which is triggered in span.end()), we call onIdleSpanEnded
  2. In there, we call beforeSpanEnd
  3. This is used by browserTracingIntegration to call addPerformanceEntries(span), which adds performance spans to the idle span
  4. After that, in onIdleSpanEnded, any spans having start/end timestamps outside of the idle span start/end timestamp will be discarded

This lead to cases where performance spans were discarded because they were out of bounds of the idle span - which is not what we want!

Now, this PR changes the timing of this a bit:

Now, we actually patch span.end of the SentrySpan, to ensure that we can always run beforeSpanEnd with the correct timing, taking all spans that are added in beforeSpanEnd into account to adjust the start/end time of the idle span.

@mydea mydea self-assigned this May 31, 2024
Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

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

Good catch!

Any chance we can test this reliably? No worries if not or if it'd be too much work; was just wondering

Copy link
Contributor

github-actions bot commented May 31, 2024

size-limit report 📦

Path Size
@sentry/browser 21.74 KB (added)
@sentry/browser (incl. Tracing) 32.76 KB (added)
@sentry/browser (incl. Tracing, Replay) 68.33 KB (added)
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 61.64 KB (added)
@sentry/browser (incl. Tracing, Replay with Canvas) 72.39 KB (added)
@sentry/browser (incl. Tracing, Replay, Feedback) 84.5 KB (added)
@sentry/browser (incl. Tracing, Replay, Feedback, metrics) 86.35 KB (added)
@sentry/browser (incl. metrics) 25.92 KB (added)
@sentry/browser (incl. Feedback) 37.9 KB (added)
@sentry/browser (incl. sendFeedback) 26.32 KB (added)
@sentry/browser (incl. FeedbackAsync) 30.86 KB (added)
@sentry/react 24.51 KB (added)
@sentry/react (incl. Tracing) 35.8 KB (added)
@sentry/vue 25.73 KB (added)
@sentry/vue (incl. Tracing) 34.59 KB (added)
@sentry/svelte 21.87 KB (added)
CDN Bundle 23.11 KB (added)
CDN Bundle (incl. Tracing) 34.5 KB (added)
CDN Bundle (incl. Tracing, Replay) 68.42 KB (added)
CDN Bundle (incl. Tracing, Replay, Feedback) 73.6 KB (added)
CDN Bundle - uncompressed 68 KB (added)
CDN Bundle (incl. Tracing) - uncompressed 102.16 KB (added)
CDN Bundle (incl. Tracing, Replay) - uncompressed 212.05 KB (added)
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 224.52 KB (added)
@sentry/nextjs (client) 35.14 KB (added)
@sentry/sveltekit (client) 33.39 KB (added)
@sentry/node 115.24 KB (added)
@sentry/aws-serverless 103.72 KB (added)

@mydea
Copy link
Member Author

mydea commented May 31, 2024

Yeah not 100% sure yet, none of our tests has caught this, I guess because it will only really be noticeable if one of the performance spans, for whatever reason, has an earlier start or end timestamp, which does not seem to happen all the time but sometimes 🤔 maybe I can simulate it somehow at least...

@mydea mydea force-pushed the fn/fix-idle-span-ending branch from 03e6338 to a4efccc Compare May 31, 2024 09:13
@mydea
Copy link
Member Author

mydea commented May 31, 2024

I adjusted a test to ensure this actually works (the test failed before)!

@mydea mydea marked this pull request as ready for review May 31, 2024 09:13
Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

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

thx!

@mydea mydea force-pushed the fn/fix-idle-span-ending branch from 4732edf to f3666ed Compare May 31, 2024 09:24
@mydea mydea merged commit 0738a96 into develop May 31, 2024
108 checks passed
@mydea mydea deleted the fn/fix-idle-span-ending branch May 31, 2024 09:44
c298lee pushed a commit that referenced this pull request Jun 4, 2024
I noticed here https://github.com/getsentry/sentry-javascript/pulls that
some tests were becoming flaky. Investigating this some more, I figured
out what was happening:

1. For idle spans, when the span is ended (in `on('spanEnded')`, which
is triggered in `span.end()`), we call `onIdleSpanEnded`
2. In there, we call `beforeSpanEnd`
3. This is used by `browserTracingIntegration` to call
`addPerformanceEntries(span)`, which adds performance spans to the idle
span
4. After that, in `onIdleSpanEnded`, any spans having start/end
timestamps outside of the idle span start/end timestamp will be
discarded

This lead to cases where performance spans were discarded because they
were out of bounds of the idle span - which is not what we want!

Now, this PR changes the timing of this a bit:

Now, we actually patch `span.end` of the SentrySpan, to ensure that we
can always run `beforeSpanEnd` with the correct timing, taking all spans
that are added in `beforeSpanEnd` into account to adjust the start/end
time of the idle 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