-
Notifications
You must be signed in to change notification settings - Fork 13
fix(csharp): emit retry telemetry from RetryHttpHandler #497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
eric-wang-1990
wants to merge
7
commits into
main
Choose a base branch
from
tracing/479-retry-telemetry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0ed2691
test(csharp): assert retry attempt events + summary tags (failing for…
eric-wang-1990 e6ffba8
fix(csharp): emit retry telemetry from RetryHttpHandler
eric-wang-1990 dc6cd7a
refactor(csharp): simplify retry telemetry to events-only on attemptC…
eric-wang-1990 a8a8350
test(csharp): simplify retry telemetry tests
eric-wang-1990 f566718
refactor(csharp): fold per-retry span tags into the retry.attempt event
eric-wang-1990 8d65edf
Merge branch 'main' into tracing/479-retry-telemetry
eric-wang-1990 bc038a8
fix(csharp): address issue #497 (2 review threads)
peco-engineer-bot[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| */ | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Net; | ||
| using System.Net.Http; | ||
|
|
@@ -142,10 +143,6 @@ protected override async Task<HttpResponseMessage> SendAsync( | |
| attemptCount++; | ||
| lastTransportException = ex; | ||
|
|
||
| activity?.SetTag("http.retry.attempt", attemptCount); | ||
| activity?.SetTag("http.retry.transport_error", ex.GetType().Name); | ||
| activity?.SetTag("http.retry.transport_error_message", ex.Message); | ||
|
|
||
| int transportWaitSeconds = CalculateBackoffWithJitter(currentBackoffSeconds); | ||
| lastErrorMessage = $"Transport error ({ex.GetType().Name}: {ex.Message}). Using exponential backoff of {transportWaitSeconds} seconds. Attempt {attemptCount}."; | ||
|
|
||
|
|
@@ -161,6 +158,19 @@ protected override async Task<HttpResponseMessage> SendAsync( | |
| } | ||
| totalTransportErrorRetrySeconds += transportWaitSeconds; | ||
|
|
||
| // Issue #479: emit a per-retry event BEFORE we sleep so the | ||
| // trace makes the retry visible even if the process dies | ||
| // during the delay. The event is self-contained — it carries | ||
| // the per-retry detail that used to be written as overwritten | ||
| // span tags (attempt number, error type via reason, error message). | ||
| activity?.AddEvent("retry.attempt", new List<KeyValuePair<string, object?>> | ||
| { | ||
| new("http.retry.attempt_number", attemptCount), | ||
| new("http.retry.delay_ms", (long)transportWaitSeconds * 1000L), | ||
| new("http.retry.reason", $"transport_error_{ex.GetType().Name}"), | ||
| new("http.retry.error_message", ex.Message) | ||
| }); | ||
|
|
||
| await Task.Delay(TimeSpan.FromSeconds(transportWaitSeconds), cancellationToken); | ||
|
Comment on lines
+165
to
174
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All 4 RetryTelemetry tests pass, including the transport-error event test with the new Pushed bc038a8 (bundled with 1 other thread(s)). |
||
| currentBackoffSeconds = Math.Min(currentBackoffSeconds * 2, _maxBackoffSeconds); | ||
| continue; | ||
|
|
@@ -183,12 +193,14 @@ protected override async Task<HttpResponseMessage> SendAsync( | |
| HttpStatusCode statusCode = response.StatusCode; | ||
| bool isTooManyRequests = statusCode == (HttpStatusCode)429; | ||
|
|
||
| // Log this retry attempt | ||
| activity?.SetTag("http.retry.attempt", attemptCount); | ||
| activity?.SetTag("http.response.status_code", (int)statusCode); | ||
|
|
||
| int waitSeconds; | ||
|
|
||
| // Issue #479: derive a reason string the retry.attempt | ||
| // event below will carry. We thread it alongside the | ||
| // existing lastErrorMessage so the event tag and the | ||
| // exception text stay consistent. | ||
| string retryReason; | ||
|
|
||
| // Check for Retry-After header | ||
| if (response.Headers.TryGetValues("Retry-After", out var retryAfterValues)) | ||
| { | ||
|
|
@@ -199,19 +211,22 @@ protected override async Task<HttpResponseMessage> SendAsync( | |
| // Use the Retry-After value | ||
| waitSeconds = retryAfterSeconds; | ||
| lastErrorMessage = $"Service temporarily unavailable (HTTP {(int)statusCode}). Using server-specified retry after {waitSeconds} seconds. Attempt {attemptCount}."; | ||
| retryReason = $"retry_after_{(int)statusCode}"; | ||
| } | ||
| else | ||
| { | ||
| // Invalid Retry-After value, use exponential backoff | ||
| waitSeconds = CalculateBackoffWithJitter(currentBackoffSeconds); | ||
| lastErrorMessage = $"Service temporarily unavailable (HTTP {(int)statusCode}). Invalid Retry-After header, using exponential backoff of {waitSeconds} seconds. Attempt {attemptCount}."; | ||
| retryReason = $"retry_after_{(int)statusCode}_invalid_header"; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| // No Retry-After header, use exponential backoff | ||
| waitSeconds = CalculateBackoffWithJitter(currentBackoffSeconds); | ||
| lastErrorMessage = $"Service temporarily unavailable (HTTP {(int)statusCode}). Using exponential backoff of {waitSeconds} seconds. Attempt {attemptCount}."; | ||
| retryReason = $"retry_after_{(int)statusCode}_no_header"; | ||
| } | ||
|
|
||
| // Dispose the response before retrying | ||
|
|
@@ -246,6 +261,22 @@ protected override async Task<HttpResponseMessage> SendAsync( | |
| totalServiceUnavailableRetrySeconds += waitSeconds; | ||
| } | ||
|
|
||
| // Issue #479: emit a retry.attempt event BEFORE Task.Delay | ||
| // below. This lives on the SendAsync activity (created by | ||
| // TraceActivityAsync above) so a single completed activity | ||
| // carries all its retry attempts as ordered events. The event is | ||
| // self-contained — it carries the per-retry detail that used to be | ||
| // written as overwritten span tags (attempt number, status code). | ||
| // Attribute keys are namespaced to match the driver's telemetry | ||
| // vocabulary (http.retry.* / OTel http.response.status_code). | ||
| activity?.AddEvent("retry.attempt", new List<KeyValuePair<string, object?>> | ||
| { | ||
| new("http.retry.attempt_number", attemptCount), | ||
| new("http.retry.delay_ms", (long)waitSeconds * 1000L), | ||
| new("http.retry.reason", retryReason), | ||
| new("http.response.status_code", (int)statusCode) | ||
| }); | ||
|
|
||
| // Wait for the calculated time | ||
| await Task.Delay(TimeSpan.FromSeconds(waitSeconds), cancellationToken); | ||
|
|
||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔵 Low — This replaces per-attempt span tags (
http.retry.attempt,http.retry.transport_error,http.retry.transport_error_message, and the Retry-After path's top-levelhttp.response.status_code) withretry.attemptevents. That's the intended design (documented in the PR), and I confirmed no in-repo code or test reads the removed tags. Flagging only so a maintainer can confirm no external dashboard/log query depends on those top-level span tags — event attributes are queried differently than span tags in most backends, so any downstream consumer keyed on the old tags would silently go dark. No code change needed if none exist.(Anchored to the nearest changed line — see the description for the exact location.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No code change warranted: the span-tag → event migration is the intended, PR-documented design. I re-verified via grep that no in-repo code or test reads the removed top-level span tags (http.retry.attempt, http.retry.transport_error, http.retry.transport_error_message, or the old top-level Retry-After http.response.status_code) — remaining references are the new retry.attempt event attributes and an unrelated CloudFetch DownloadFile span tag. The only open point is whether an external dashboard/log query depends on the old top-level tags, which cannot be determined from this repo and is exactly the maintainer confirmation the reviewer requested — needs human judgment on out-of-band observability consumers.