Skip to content

feat(trace): Correct clock drift between client and server spans - #132010

Draft
Lms24 wants to merge 4 commits into
masterfrom
lms/feat-trace-clock-drift-correction
Draft

Lms24 wants to merge 4 commits into
masterfrom
lms/feat-trace-clock-drift-correction

Conversation

@Lms24

@Lms24 Lms24 commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

This PR adds clock drift correction to the trace waterfall, gated behind the trace-waterfall-clock-drift-correction flag.

When the clocks of a client and a server are not in sync, a server span can show up before or after the client request that caused it. This happens often on client-side apps, given user devices are corrected via NTP, users have set clocks manually, etc. With getsentry/sentry-javascript#23054 in the JS SDK, we now more often adjust our telemetry timestamps to the users' wall clock (it's the only somewhat reliable clock in browsers), which likely increased the amount of out-of-sync client and server span trees.

The correction shifts the server subtree into the client span that made the request. It can be toggled in the trace preferences and is off by default. Shifted spans show a clock icon on the subtree root, and their duration label and the details panel explain the shift on hover. The criteria for applying the clock drift are:

  • the span pairs to check are http.client followed by a http.server child segment
  • the server span started or finished outside of the client span, with a 10ms margin
  • the server span's duration fits into the client span's duration

Clock drift makes traces across services hard to read, and the correction fixes the most common case. We want to try it with a few internal orgs first. This also centers duration labels on the span bar by layout instead of font metrics. That moves all labels by about 1px but keeps the icon aligned at every pixel density.

Screenshots

Clock drift correction toggle (off)

image

Clock drift correction toggle (on)

image

Raw waterfall (note the 2 min. drift)

image

Corrected waterfall

image

Waterfall tooltip

image

Detail panel tooltip

image

Testing

Some traces if anyone wants to check out the preview:

Flag: #132009. Rollout: getsentry/sentry-options-automator#10726.

🤖 Generated with Claude Code

@Lms24
Lms24 added this pull request to stack #132012 November 20, 2026 14:18
@github-actions github-actions Bot added the Scope: Frontend Automatically applied to PRs that change frontend components label Nov 20, 2026
@Lms24 Lms24 changed the title lms/feat trace clock drift correction feat(trace): Correct clock drift between client and server spans Nov 20, 2026
@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

📊 Type Coverage Diff

✅ no issues found

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

if (options.preferences?.clock_drift_correction) {
TraceTree.ApplyClockDriftCorrection(tree.root);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Clock drift runs before pageload swap

Medium Severity

Correction is applied before the pageload/server-handler swap, and shiftSubtree moves every descendant of the server span. When an http.client → http.server → pageload tree is still in its pre-swap shape, the browser pageload (and its children) inherit the server's offset even though they use the client clock. Toggling correction later runs on the already-swapped tree and does not shift pageload, so loading with the preference on and turning it on in the UI can produce different waterfalls.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 2785270. Configure here.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

issue,
node_space,
props.node.clockDriftOffsetMs
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Occurrence patterns ignore clock drift offset

Medium Severity

Performance-issue hatch patterns still position from the occurrence's raw timestamp, while the span bar now uses shifted space. After a clock-drift move, that timestamp falls outside the bar and is clamped to the start or end, so the pattern no longer lines up with the issue. Issue icons were updated to add clockDriftOffsetMs; these patterns were not.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 0c96acc. Configure here.

Lms24 and others added 4 commits November 23, 2026 19:18
Client devices often have clocks that disagree with the server's, so
server spans can start before or end after the client request that made
them. Relay only corrects drift of 55 minutes or more, so smaller drift
reaches the waterfall uncorrected and makes traces hard to read.

Shift each misaligned server subtree into its client span by the smallest
amount that fits. Requests crossing into a new segment are detected as
client/server pairs, and pairs are grouped by the client's segment, which
shares one clock. Within a segment, only requests that agree with the
majority are corrected, and shifts below 10ms are ignored. Only node
spaces change, recorded timestamps stay intact.

The correction is a trace preference (on by default) that can be toggled
off. Corrected spans show a clock icon in their duration label and a
"Shifted" tag in the drawer, both explaining the adjustment on hover.

Co-Authored-By: Claude <noreply@anthropic.com>
Only organizations with the trace-waterfall-clock-drift-correction flag
can use clock drift correction, and it is off by default. Without the
flag, the correction is never applied and its toggle is hidden, even if
it was stored as turned on.

Also explain the shift on hover over the whole duration label of every
shifted span, not only the icon, and reword the tooltips. Center
duration labels and the icon on the row by layout instead of font
metrics, so they line up the same at every pixel density.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Autogroup nodes did not take the offset of the spans they wrap, so issues
on grouped spans in a shifted subtree were drawn at their recorded time.

Also simplify the correction code: reuse formatTraceDuration, drop the
unused clientNode, the static TraceTree wrappers and the restore count,
narrow node types without casts, and fix two misplaced doc comments.

Co-Authored-By: Claude <noreply@anthropic.com>
The correction object on a node only carried the offset of its own shift,
which nothing outside of tests read: every consumer only checked whether
it was set. The total offset already lives in clockDriftOffsetMs, so a
boolean marks the root of a moved subtree instead.

Co-Authored-By: Claude <noreply@anthropic.com>

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

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 using default effort and found 1 potential issue.

There are 3 total unresolved issues (including 2 from previous reviews).

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 92b761c. Configure here.

// off everywhere, also if it was stored while the organization had the feature.
const initialPreferences = hasClockDriftCorrectionFeature(organization)
? props.initialPreferences
: {...props.initialPreferences, clock_drift_correction: false};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Flag off overwrites stored preference

Medium Severity

When the clock-drift feature flag is off, TraceStateProvider forces clock_drift_correction to false in reducer state, and the existing persist effect then writes that value to localStorage. Visiting an org without the flag, or a temporary flag-off during rollout, permanently clears a previously enabled preference for every org that shares that storage key.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 92b761c. Configure here.

This branch was successfully deployed

1 active deployment
Preview — 92b761ca Deployed Nov 23, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Frontend Automatically applied to PRs that change frontend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant