Repository navigation
fix(core): re-anchor hrTime() to the wall clock after the host sleeps - #7218
Open
overbalance wants to merge 1 commit into
Open
overbalance wants to merge 1 commit into
overbalance wants to merge 1 commit into
Conversation
overbalance
added a commit
to embrace-io/opentelemetry-js
that referenced
this pull request
Oct 19, 2026
Pull request dashboard statusWaiting on the author · refreshed 2026-10-24 00:31 UTC Resolve merge conflicts. Respond to 1 review item (e.g. link a commit, explain why not, ask a follow-up):
Status above doesn't look right?
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #7218 +/- ##
=======================================
Coverage 95.13% 95.13%
=======================================
Files 384 384
Lines 13548 13569 +21
Branches 3128 3133 +5
=======================================
+ Hits 12889 12909 +20
- Misses 659 660 +1
🚀 New features to boost your workflow:
|
overbalance
force-pushed
the
overbalance/hrtime-drift-correction
branch
from
October 19, 2026 21:34
9da020d to
70cc9c6
Compare
overbalance
force-pushed
the
overbalance/hrtime-drift-correction
branch
from
October 19, 2026 21:52
70cc9c6 to
aecc45e
Compare
| const now = millisToHrTime( | ||
| typeof performanceNow === 'number' ? performanceNow : performance.now() | ||
| const currentPerformanceNow = performance.now(); | ||
| updateClockDrift(currentPerformanceNow); |
Contributor
There was a problem hiding this comment.
Could we keep using a monotonic clock for durations? It's an edge case, but if the wall clock moves backward during a request, we can now get a negative duration that the histogram drops. The HTTP and SDK duration calculations still use hrTime(), so I think we need to update those too.
2 tasks done
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which problem is this PR solving?
After the host sleeps,
hrTime()returns timestamps that are behind the wall clock by the length of the sleep.Fixes #7217
Short description of the changes
hrTime()stores the drift betweenDate.now()andperformance.timeOrigin + performance.now()as a list of segments. It starts a new segment only when the drift differs from the latest one by more than 1 second.hrTime()uses the latest drift.hrTime(performanceNow)uses the drift of the segment that containsperformanceNow, so a timestamp from before a sleep is not moved by that sleep.Span._getTimeconverts performance timestamps with its own per-span offset directly. Before, it calledhrTime(inp + offset), which would now apply the correction twice.instrumentation-xml-http-requesttests: the timeout helpers moved the fakeDateforward 2 seconds and kept the fakeperformance.now()at 0. The new code sees this as a sleep and re-anchors. The tests passed before only because the resource-matching window was[0, 0], which matched no fixture resource. The helpers now advance both fake clocks together and start after the fixture resources, so the window still matches no resource.Type of change
How Has This Been Tested?
hrTimetests: re-anchor after a sleep, no change for a performance timestamp from before a sleep, correction for one from after it, the drift of each segment, no re-anchor below the threshold, re-anchor when the wall clock steps backward, and a stable anchor after a re-anchorSpantest: a performance timestamp after a sleep is corrected once. Without theSpanchange, the sleep is counted twice.npm test,npm run test:browser,npm run test:webworker, andnpm run lintall passChecklist: