Skip to content

fix: incorrect latency when there is at least one retry#325

Draft
duyhungtnn wants to merge 1 commit intofix-invalid-durationfrom
fix-invalid-duration-kenji
Draft

fix: incorrect latency when there is at least one retry#325
duyhungtnn wants to merge 1 commit intofix-invalid-durationfrom
fix-invalid-duration-kenji

Conversation

@duyhungtnn
Copy link
Copy Markdown
Collaborator

@duyhungtnn duyhungtnn commented May 8, 2026

This pull request improves how per-attempt latency is measured for API requests that may be retried, ensuring that latency metrics reflect only the most recent (final) attempt rather than the total time including retries and backoff delays. It does this by adding an optional onAttemptStart callback to the postInternal function, which is called at the start of each attempt, allowing the caller to reset timing or other per-attempt state. Extensive tests have been added to verify the correct behavior.

Enhancements to latency measurement and retry handling:

  • Added an optional onAttemptStart callback to the postInternal function in src/internal/remote/post.ts, which is invoked at the start of every request attempt (including retries). This enables accurate per-attempt latency measurement by allowing the caller to reset timers for each attempt. [1] [2]

  • Updated ApiClientImpl.getEvaluations in src/internal/remote/ApiClient.ts to use the new onAttemptStart callback, ensuring that the measured latency reflects only the duration of the final attempt, not the cumulative time across retries.

Testing improvements:

  • Added comprehensive tests in test/internal/remote/post.spec.ts to verify that onAttemptStart is called the correct number of times (once per attempt), and that postInternal works as expected both with and without the callback.

  • Updated existing tests in test/internal/remote/ApiClientImpl.spec.ts to account for the new onAttemptStart parameter in calls to postInternal.

  • Added a new test in test/internal/remote/ApiClientEvaluationLatency.spec.ts to confirm that ApiClientImpl.getEvaluations measures latency only for the last attempt, even when retries occur.

Copy link
Copy Markdown

Copilot AI left a comment

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 fixes latency metrics for retried API requests by resetting the latency start timestamp at the beginning of each retry attempt, so the reported latency reflects only the final (successful) attempt rather than total time across attempts plus backoff.

Changes:

  • Added an optional onAttemptStart callback to postInternal, invoked at the start of every attempt (initial + retries).
  • Updated ApiClientImpl.getEvaluations to reset its latency start time via onAttemptStart so seconds reflects the last attempt only.
  • Added/updated tests to validate attempt-callback invocation counts and correct last-attempt-only latency behavior during retries.

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/internal/remote/post.ts Adds onAttemptStart hook and invokes it per attempt inside the retry wrapper.
src/internal/remote/ApiClient.ts Resets startMillis on each attempt so evaluation latency excludes retry/backoff time.
test/internal/remote/post.spec.ts Adds coverage for onAttemptStart being called once (no retry) and once per attempt (with retries).
test/internal/remote/ApiClientImpl.spec.ts Updates expectation for the additional postInternal argument.
test/internal/remote/ApiClientEvaluationLatency.spec.ts Adds regression test ensuring getEvaluations.seconds reflects only the final attempt duration.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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