Skip to content

Fix(SDK): Auto root span start timestamps precision#1985

Merged
bobstrecansky merged 3 commits into
open-telemetry:mainfrom
zigzagdev:fix/auto-root-span-start-timestamps-precision
Jun 17, 2026
Merged

Fix(SDK): Auto root span start timestamps precision#1985
bobstrecansky merged 3 commits into
open-telemetry:mainfrom
zigzagdev:fix/auto-root-span-start-timestamps-precision

Conversation

@zigzagdev

Copy link
Copy Markdown
Contributor

Summary

Content

Fixed a precision loss bug in AutoRootSpan::create() where the start timestamp was calculated incorrectly when REQUEST_TIME_FLOAT is not available.

Root Cause

The original code cast microtime(true) to int before multiplying via NANOS_PER_SECOND:

$startTime = array_key_exists('REQUEST_TIME_FLOAT', $request->getServerParams())
    ? $request->getServerParams()['REQUEST_TIME_FLOAT'] : (int) microtime(true);          // ← precision loss here
->setStartTimestamp((int) ($startTime * ClockInterface::NANOS_PER_SECOND))

This caused up to 999,999,999ns (~1 second) of error because the sub-second precision was discarded before nanosecond conversioning.

  • GET /?foo=bar with microtime(true) = 1718123456.789 → cast to 1718123456 → multiplied by 1e9 → last 9 digits always 000000000

What I have done

Multiply first, then cast to int to preserve sub-second precision like:

$startTimeInNanos = array_key_exists('REQUEST_TIME_FLOAT', $request->getServerParams())
? (int) ($request->getServerParams()['REQUEST_TIME_FLOAT'] * ClockInterface::NANOS_PER_SECOND)
    : (int) (microtime(true) * ClockInterface::NANOS_PER_SECOND)
->setStartTimestamp($startTimeInNanos)

Test Results

screenshot 2026-06-11 23 33 52

@zigzagdev zigzagdev requested a review from a team as a code owner June 11, 2026 14:45
@welcome

welcome Bot commented Jun 11, 2026

Copy link
Copy Markdown

Thanks for opening your first pull request! If you haven't yet signed our Contributor License Agreement (CLA), then please do so that we can accept your contribution. A link should appear shortly in this PR if you have not already signed one.

@codecov

codecov Bot commented Jun 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 67.92%. Comparing base (2f1c57f) to head (e912631).

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##               main    #1985      +/-   ##
============================================
+ Coverage     67.87%   67.92%   +0.04%     
  Complexity     3046     3046              
============================================
  Files           459      459              
  Lines          8891     8891              
============================================
+ Hits           6035     6039       +4     
+ Misses         2856     2852       -4     
Flag Coverage Δ
8.1 67.64% <100.00%> (+0.06%) ⬆️
8.2 67.75% <100.00%> (-0.06%) ⬇️
8.3 67.73% <100.00%> (-0.08%) ⬇️
8.4 67.84% <100.00%> (+0.07%) ⬆️
8.5 67.75% <100.00%> (+0.01%) ⬆️

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

Files with missing lines Coverage Δ
src/SDK/Trace/AutoRootSpan.php 87.23% <100.00%> (+2.12%) ⬆️

... 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 2f1c57f...e912631. Read the comment docs.

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

@ChrisLightfootWild ChrisLightfootWild 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.

Thanks for the PR @zigzagdev 💪

@ChrisLightfootWild ChrisLightfootWild requested a review from a team June 12, 2026 09:54
@bobstrecansky bobstrecansky merged commit 9b684b7 into open-telemetry:main Jun 17, 2026
12 checks passed
@zigzagdev zigzagdev deleted the fix/auto-root-span-start-timestamps-precision branch June 18, 2026 05:53
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.

3 participants