Skip to content

Remove static state in SystemClock - #2041

Open
SamMousa wants to merge 1 commit into
open-telemetry:mainfrom
collecthor:oc/systemclock-instance-reference-time
Open

Remove static state in SystemClock#2041
SamMousa wants to merge 1 commit into
open-telemetry:mainfrom
collecthor:oc/systemclock-instance-reference-time

Conversation

@SamMousa

Copy link
Copy Markdown

In the library code base SystemClock is used as a singleton (via Clock::getDefault()). So this change will not actually impact much.

Practically it simplifies SystemClock by removing its internal static state, which I consider to be a code smell.

  • We maintain internal monotonicity by computing the reference time at construction
  • The library maintains monotonicity by only using the singleton instance

We lose monotonicity between different instances, I do not think this is a bad thing, it could actually be a good thing.
For long running PHP processes (for example ReactPHP or other scenarios where a single process handles many requests) we could choose to instantiate a system clock for each request. Having a single reference time at process start means we can drift away from real time because we're not getting resynced with NTP.

FYI: While the code was edited by opencode, this description is handwritten.

@SamMousa
SamMousa requested a review from a team as a code owner August 24, 2026 13:22
@linux-foundation-easycla

Copy link
Copy Markdown

CLA Missing ID

  • ❌ The email address for the commit (3d764f6) is not linked to the GitHub account, preventing the EasyCLA check. Consult this Help Article and GitHub Help to resolve. (To view the commit's email address, add .patch at the end of this PR page's URL.) For further assistance with EasyCLA, please visit our EasyCLA portal and chat with our support bot.

@SamMousa

Copy link
Copy Markdown
Author

I'll get the CLA thing fixed, already contacted them to enroll our organisation etc.

@codecov

codecov Bot commented Aug 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 68.10%. Comparing base (b9c9963) to head (3d764f6).

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##               main    #2041      +/-   ##
============================================
- Coverage     68.14%   68.10%   -0.04%     
+ Complexity     3083     3081       -2     
============================================
  Files           459      459              
  Lines          9031     9027       -4     
============================================
- Hits           6154     6148       -6     
- Misses         2877     2879       +2     
Flag Coverage Δ
8.1 67.82% <100.00%> (+0.05%) ⬆️
8.2 67.98% <100.00%> (-0.06%) ⬇️
8.3 68.04% <100.00%> (+0.09%) ⬆️
8.4 68.00% <100.00%> (+0.01%) ⬆️
8.5 68.04% <100.00%> (+0.08%) ⬆️
8.6 68.00% <100.00%> (+0.13%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/API/Common/Time/SystemClock.php 100.00% <100.00%> (+35.71%) ⬆️

... and 5 files with indirect coverage changes


Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b9c9963...3d764f6. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI 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.

Pull request overview

Simplifies SystemClock by moving its wall-clock reference from static state to each clock instance.

Changes:

  • Computes the reference time during construction.
  • Uses an immutable instance property in now().

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +19 to +22
$this->referenceTime = self::calculateReferenceTime(
microtime(true),
hrtime(true)
);
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.

2 participants