fix: Use monotonic clock to compute durations #631
Merged
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.
In summary, care must be taken when computing durations. Monotonic
clocks are not subject to system clock adjustments or system clock skew.
The difference between any two chronologically recorded time values is
guaranteed to never be negative.
The same guarantee above does not exist for the difference between two
calls to datetime.now() and friends.
More details and rationale see PEP 418.
Resources:
PEP 418 -- Add monotonic time, performance counter, and process time functions
https://www.python.org/dev/peps/pep-0418/
PEP 564 -- Add new time functions with nanosecond resolution
https://www.python.org/dev/peps/pep-0564/
Know limitation
At present, we only address timestamp computation within a single span.
A more complete patch should compute all durations in a transaction using a monotonic clock and set all timestamps relative to a single reading of a wall clock time source.