fix: incorrect latency when there is at least one retry#325
Draft
duyhungtnn wants to merge 1 commit intofix-invalid-durationfrom
Draft
fix: incorrect latency when there is at least one retry#325duyhungtnn wants to merge 1 commit intofix-invalid-durationfrom
duyhungtnn wants to merge 1 commit intofix-invalid-durationfrom
Conversation
There was a problem hiding this comment.
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
onAttemptStartcallback topostInternal, invoked at the start of every attempt (initial + retries). - Updated
ApiClientImpl.getEvaluationsto reset its latency start time viaonAttemptStartsosecondsreflects 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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
onAttemptStartcallback to thepostInternalfunction, 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
onAttemptStartcallback to thepostInternalfunction insrc/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.getEvaluationsinsrc/internal/remote/ApiClient.tsto use the newonAttemptStartcallback, 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.tsto verify thatonAttemptStartis called the correct number of times (once per attempt), and thatpostInternalworks as expected both with and without the callback.Updated existing tests in
test/internal/remote/ApiClientImpl.spec.tsto account for the newonAttemptStartparameter in calls topostInternal.Added a new test in
test/internal/remote/ApiClientEvaluationLatency.spec.tsto confirm thatApiClientImpl.getEvaluationsmeasures latency only for the last attempt, even when retries occur.