-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(browser): Avoid recording long task spans starting before their parent span #14183
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import * as Sentry from '@sentry/browser'; | ||
|
||
window.Sentry = Sentry; | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
integrations: [ | ||
Sentry.browserTracingIntegration({ | ||
idleTimeout: 9000, | ||
enableLongAnimationFrame: false, | ||
instrumentPageLoad: false, | ||
instrumentNavigation: true, | ||
enableInp: false, | ||
enableLongTask: true, | ||
}), | ||
], | ||
tracesSampleRate: 1, | ||
debug: true, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const longTaskButton = document.getElementById('myButton'); | ||
|
||
longTaskButton?.addEventListener('click', () => { | ||
const startTime = Date.now(); | ||
|
||
function getElapsed() { | ||
const time = Date.now(); | ||
return time - startTime; | ||
} | ||
|
||
while (getElapsed() < 500) { | ||
// | ||
} | ||
|
||
// trigger a navigation in the same event loop tick | ||
window.history.pushState({}, '', '/#myHeading'); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
</head> | ||
<body> | ||
<div>Rendered Before Long Task</div> | ||
<script src="https://example.com/path/to/script.js"></script> | ||
|
||
<button id="myButton">Start long task</button> | ||
<h1 id="myHeading">Heading</h1> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { expect } from '@playwright/test'; | ||
import type { Event } from '@sentry/types'; | ||
|
||
import { sentryTest } from '../../../../utils/fixtures'; | ||
import { getFirstSentryEnvelopeRequest, shouldSkipTracingTest } from '../../../../utils/helpers'; | ||
|
||
sentryTest( | ||
"doesn't capture long task spans starting before a navigation in the navigation transaction", | ||
async ({ browserName, getLocalTestPath, page }) => { | ||
// Long tasks only work on chrome | ||
if (shouldSkipTracingTest() || browserName !== 'chromium') { | ||
sentryTest.skip(); | ||
} | ||
const url = await getLocalTestPath({ testDir: __dirname }); | ||
|
||
await page.goto(url); | ||
|
||
await page.locator('#myButton').click(); | ||
|
||
const navigationTransactionEvent = await getFirstSentryEnvelopeRequest<Event>(page, url); | ||
|
||
expect(navigationTransactionEvent.contexts?.trace?.op).toBe('navigation'); | ||
|
||
const longTaskSpans = navigationTransactionEvent?.spans?.filter(span => span.op === 'ui.long-task'); | ||
expect(longTaskSpans).toHaveLength(0); | ||
}, | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,24 +105,32 @@ export function startTrackingWebVitals({ recordClsStandaloneSpans }: StartTracki | |
*/ | ||
export function startTrackingLongTasks(): void { | ||
addPerformanceInstrumentationHandler('longtask', ({ entries }) => { | ||
if (!getActiveSpan()) { | ||
const parent = getActiveSpan(); | ||
if (!parent) { | ||
return; | ||
} | ||
|
||
const { op: parentOp, start_timestamp: parentStartTimestamp } = spanToJSON(parent); | ||
|
||
for (const entry of entries) { | ||
const startTime = msToSec((browserPerformanceTimeOrigin as number) + entry.startTime); | ||
const duration = msToSec(entry.duration); | ||
|
||
const span = startInactiveSpan({ | ||
if (parentOp === 'navigation' && parentStartTimestamp && startTime < parentStartTimestamp) { | ||
// Skip adding a span if the long task started before the navigation started. | ||
// `startAndEndSpan` will otherwise adjust the parent's start time to the span's start | ||
// time, potentially skewing the duration of the actual navigation as reported via our | ||
// routing instrumentations | ||
continue; | ||
} | ||
|
||
startAndEndSpan(parent, startTime, startTime + duration, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. small refactor: I switched from manually starting and ending the span to using the |
||
name: 'Main UI thread blocked', | ||
op: 'ui.long-task', | ||
startTime, | ||
attributes: { | ||
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.browser.metrics', | ||
}, | ||
}); | ||
if (span) { | ||
span.end(startTime + duration); | ||
} | ||
} | ||
}); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we're just ~15 bytes over the previous limit but I figured I'd give the limit a slightly bigger bump as we usually do