Skip to content
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

fix(browser): Fix bug causing unintentional dropping of transactions #12933

Merged
merged 2 commits into from
Jul 17, 2024

Conversation

lforst
Copy link
Member

@lforst lforst commented Jul 16, 2024

We saw instances of pageload transactions being dropped because they contained spans with negative durations (timestamp < start_timestamp).

After investigating it seems like performance entries by the browser are emitted with a negative duration. We could only observe this happening in Chrome.

The particular environment this could be (flakily) reproduced is on Chrome, for the scaffold Next.js app, with the Sentry SDK and Replay installed. The negative duration span that was recorded was for the replay worker resource.

Other people also seem to be running into this but so far I don't really understand why the duration is negative and how we would mitigate this properly. This PR is more of a hot-fix than anything else.

Attempts to fix: #12914

@andreiborza
Copy link
Member

andreiborza commented Jul 16, 2024

_ No description provided. _

To add some context, this happens for me with the replay integration and particularly for the spans for the request to the worker blob, and only in Chrome.

@lforst lforst force-pushed the lforst-prevent-dropping-of-transactions branch from 5ef066c to e96b4af Compare July 17, 2024 08:24
Copy link
Contributor

github-actions bot commented Jul 17, 2024

size-limit report 📦

Path Size
@sentry/browser 22.3 KB (0%)
@sentry/browser (incl. Tracing) 33.69 KB (+0.02% 🔺)
@sentry/browser (incl. Tracing, Replay) 69.78 KB (+0.01% 🔺)
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 63.08 KB (+0.01% 🔺)
@sentry/browser (incl. Tracing, Replay with Canvas) 74.17 KB (+0.01% 🔺)
@sentry/browser (incl. Tracing, Replay, Feedback) 86.49 KB (+0.01% 🔺)
@sentry/browser (incl. Tracing, Replay, Feedback, metrics) 88.37 KB (+0.01% 🔺)
@sentry/browser (incl. metrics) 26.59 KB (0%)
@sentry/browser (incl. Feedback) 38.98 KB (0%)
@sentry/browser (incl. sendFeedback) 26.93 KB (0%)
@sentry/browser (incl. FeedbackAsync) 31.54 KB (0%)
@sentry/react 25.06 KB (0%)
@sentry/react (incl. Tracing) 36.76 KB (+0.02% 🔺)
@sentry/vue 26.41 KB (0%)
@sentry/vue (incl. Tracing) 35.57 KB (+0.02% 🔺)
@sentry/svelte 22.44 KB (0%)
CDN Bundle 23.52 KB (0%)
CDN Bundle (incl. Tracing) 35.47 KB (+0.01% 🔺)
CDN Bundle (incl. Tracing, Replay) 69.88 KB (+0.01% 🔺)
CDN Bundle (incl. Tracing, Replay, Feedback) 75.14 KB (+0.01% 🔺)
CDN Bundle - uncompressed 69 KB (0%)
CDN Bundle (incl. Tracing) - uncompressed 104.94 KB (+0.02% 🔺)
CDN Bundle (incl. Tracing, Replay) - uncompressed 216.72 KB (+0.01% 🔺)
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 229.44 KB (+0.01% 🔺)
@sentry/nextjs (client) 36.62 KB (+0.02% 🔺)
@sentry/sveltekit (client) 34.35 KB (+0.02% 🔺)
@sentry/node 111.28 KB (0%)
@sentry/node - without tracing 88.73 KB (0%)
@sentry/aws-serverless 97.87 KB (0%)

// There is a SO post attempting to explain this, but it leaves one with open questions: https://stackoverflow.com/questions/23191918/peformance-getentries-and-negative-duration-display
// The way we clamp the value is probably not accurate, since we have observed this happen for things that may take a while to load, like for example the replay worker.
// TODO: Investigate why this happens and how to properly mitigate. For now, this is a workaround to prevent transactions being dropped due to negative duration spans.
entry.duration < 0 ? 0 : entry.duration,
Copy link
Member

Choose a reason for hiding this comment

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

l: any reason for not using Math.max?

Suggested change
entry.duration < 0 ? 0 : entry.duration,
Math.max(0, entry.duration),

Copy link
Member Author

Choose a reason for hiding this comment

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

I have a theory that performance is better for the inline if but can change

@lforst lforst marked this pull request as ready for review July 17, 2024 11:16
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.

Just a comment on the comment but it looks like a reasonable guard.

const duration = msToSec(
// Inexplicibly, Chrome sometimes emits a negative duration. We need to work around this.
// There is a SO post attempting to explain this, but it leaves one with open questions: https://stackoverflow.com/questions/23191918/peformance-getentries-and-negative-duration-display
// The way we clamp the value is probably not accurate, since we have observed this happen for things that may take a while to load, like for example the replay worker.
Copy link
Member

@Lms24 Lms24 Jul 17, 2024

Choose a reason for hiding this comment

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

does the worker take a while to load though? I'd imagine creating the blob should be fairly fast since the code ships with the already loaded JS SDK bundle by default? My guess yesterday was the opposite: that it's too fast and therefore causing weird timing problems 😅
(maybe this is also blob-specific?)

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it loaded in something like 4ms when we tested it (which was locally in dev mode). So not too fast. Even if it loaded "too fast" a negative duration doesn't make sense in my eyes 🤔

@lforst lforst merged commit e710f3b into develop Jul 17, 2024
116 checks passed
@lforst lforst deleted the lforst-prevent-dropping-of-transactions branch July 17, 2024 12:34
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.

Pageloads traces aren't being captured by sentry/nextjs
3 participants