Skip to content

fix(trace): accumulate request attempt duration across retries in TraceInfo - #1267

Open
AdamMagued wants to merge 2 commits into
go-resty:v3from
AdamMagued:fix-issue-1142-traceinfo-totaltime-retries
Open

AdamMagued wants to merge 2 commits into
go-resty:v3from
AdamMagued:fix-issue-1142-traceinfo-totaltime-retries

Conversation

@AdamMagued

Copy link
Copy Markdown
Contributor

Fixes #1142

Summary

When requests undergo retries with client trace enabled, TraceInfo values could be overwritten or produce distorted intervals due to trace context chaining across retry attempts. In addition, duration calculations lacked guard checks against out-of-order hook timestamps.

Changes

  • Isolate baseCtx on Request so each retry attempt creates an independent clientTrace rooted in the original request context.
  • Add boundary and order checks across TraceInfo calculation fields to prevent negative durations when transport callbacks complete prematurely.
  • Reset req.StartTime per attempt in Client.execute so attempt duration aligns with per-attempt trace lifecycle.
  • Add unit tests in retry_test.go verifying trace isolation across retries, backoff handling, transport error retries, and behavior when tracing is disabled.

@codecov

codecov Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.76%. Comparing base (78dfc5c) to head (c9827d9).
⚠️ Report is 15 commits behind head on v3.

Additional details and impacted files
@@           Coverage Diff            @@
##               v3    #1267    +/-   ##
========================================
  Coverage   99.75%   99.76%            
========================================
  Files          20       20            
  Lines        3697     3835   +138     
========================================
+ Hits         3688     3826   +138     
  Misses          9        9            
Flag Coverage Δ
unittests 99.76% <100.00%> (+<0.01%) ⬆️

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

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

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

@nk-batman21

Copy link
Copy Markdown
Contributor

I found one way the previous attempt’s trace still gets callbacks on 765c760. In a 503 → 200 test with tracing enabled, I added a value for the next attempt inside AddRetryHooks:

req := res.Request
req.SetContext(context.WithValue(req.Context(), attemptKey{}, 2))

I saved the first attempt’s req.trace pointer and gotConn timestamp in that hook. After the second attempt, that same trace’s gotConn had moved past the first attempt’s endTime. The value 2 reached the second attempt. Without the SetContext call, the first trace was unchanged on the PR head; on the base it changed even in that control.

At hook time req.Context() already contains the first attempt’s HTTP trace. SetContext stores this derived context as baseCtx, so the next httptrace.WithClientTrace composes with the previous callbacks again. Deriving the value from the original caller context instead preserved both the annotation and the first timestamp in my test. Could TestTraceInfoMultiRetryWithBackoff cover this hook case, or could the caller-context requirement be documented?

…ling tracing

Signed-off-by: AdamMagued <adamismailmageud@gmail.com>
@AdamMagued

Copy link
Copy Markdown
Contributor Author

@nk-batman21 Fixed in c9827d9 by disabling the prior clientTrace when initTraceIfEnabled or SetTrace(false) runs, so composed httptrace.ClientTrace callbacks from SetContext(context.WithValue(req.Context(), ...)) in AddRetryHooks do not mutate earlier attempts while preserving caller values and caller-provided traces. Added TestTraceInfoRetryHookSetContext and TestTraceInfoTCPConnTimeFallbacks covering the hook retry case and the remaining TraceInfo branches.

@nk-batman21

Copy link
Copy Markdown
Contributor

Retested the same 503 → 200 case against 765c760 and c9827d9. The old head still moves attempt 1’s gotConn past its end time when the retry hook derives a value from req.Context(). On c9827d9 the timestamp stays unchanged and the value reaches attempt 2. Both new trace tests also passed three runs with -race. This resolves the case I reported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Resp.Request.TraceInfo().TotalTime not include time spend to retry

2 participants