Skip to content

fix(test): stabilize flaky SpanLifeCycle custom endTime test - #2754

Merged
Hector Hernandez (hectorhdzg) merged 1 commit into
microsoft:mainfrom
hectorhdzg:fix/spanlifecycle-custom-endtime-flaky
Aug 12, 2026
Merged

fix(test): stabilize flaky SpanLifeCycle custom endTime test#2754
Hector Hernandez (hectorhdzg) merged 1 commit into
microsoft:mainfrom
hectorhdzg:fix/spanlifecycle-custom-endtime-flaky

Conversation

@hectorhdzg

Copy link
Copy Markdown
Member

The "EndTime: custom endTime is respected" test used Date.now() as the custom span end time and asserted the recorded endTime round-tripped within 10ms. The span start time is derived from performance.timeOrigin + performance.now(), not Date.now(). On loaded/virtualized CI hosts (observed on the Windows build container, Node 20) the system wall clock can drift slightly behind the span's perf-based start time. When Date.now() lands before the span start, span.end() clamps the duration to zero and sets endTime === startTime, so the round-trip difference exceeds the 10ms tolerance and the test intermittently fails (763 tests, 1 failed).

Derive the custom end time from the span's own startTime so the comparison stays within the same time domain the SDK uses internally. This guarantees end > start (no zero-duration clamping) and keeps the value in the epoch range for an exact millisToHrTime round-trip, making the assertion deterministic.

The "EndTime: custom endTime is respected" test used Date.now() as the
custom span end time and asserted the recorded endTime round-tripped within
10ms. The span start time is derived from performance.timeOrigin +
performance.now(), not Date.now(). On loaded/virtualized CI hosts (observed
on the Windows build container, Node 20) the system wall clock can drift
slightly behind the span's perf-based start time. When Date.now() lands
before the span start, span.end() clamps the duration to zero and sets
endTime === startTime, so the round-trip difference exceeds the 10ms
tolerance and the test intermittently fails (763 tests, 1 failed).

Derive the custom end time from the span's own startTime so the comparison
stays within the same time domain the SDK uses internally. This guarantees
end > start (no zero-duration clamping) and keeps the value in the epoch
range for an exact millisToHrTime round-trip, making the assertion
deterministic.
@hectorhdzg
Hector Hernandez (hectorhdzg) requested a review from a team as a code owner August 12, 2026 22:16
Copilot AI lite review requested due to automatic review settings August 12, 2026 22:16

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

This PR stabilizes a flaky unit test around custom span endTime handling by ensuring the test uses the same time domain as the SDK’s perf-based start time, avoiding intermittent failures caused by CI host wall-clock drift.

Changes:

  • Derives the custom span end time from the span’s own startTime (HrTime) instead of Date.now().
  • Adds explanatory comments documenting the CI drift scenario and why Date.now() can cause zero-duration clamping.

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

Comment on lines +580 to +582
const startTime = span?.startTime;
const startMs = startTime ? (startTime[0] * 1000 + startTime[1] / 1000000) : Date.now();
const customEndTime = startMs + 1000; // 1 second after the span start

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.

LGTM after copilot comment is resolved.

@hectorhdzg
Hector Hernandez (hectorhdzg) merged commit cae0327 into microsoft:main Aug 12, 2026
9 checks passed
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