fix(otelhttp): record client metrics when request body is drained but not closed #10008
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.40) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.40 · 309 lines diff across 3 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Shallow Review Depth
shallow_review_depth
0.344
Diff Size
diff_size
0.343
Real PR Conversation Timeline
1 comments
"Review active on #10008"
general
weight: 0.35
MrAlias
Fixes #10003
Since #8684 (v0.72.0),
Transportwaits for the request body to be closed before recordinghttp.client.request.durationandhttp.client.request.body.size. Retrying RoundTrippers placed underotelhttp.NewTransport, such asgo-retryablehttp, read the body withio.ReadAll, send a buffered copy, and never close the original. The tracker never sees a final state, so both metrics are dropped for every request with a body.This change treats
io.EOFas a final body state alongsideClose. A drained body yields no more bytes, so its read count is final. This keeps the rule from #8684 that a read count compared toContentLengthis never treated as completion: onlyCloseorio.EOFfinalize the body.Changes
trackedRequestBody.Readmarks the body final onio.EOFand callsmaybeRecord.TestTransportMetricsRecordedWhenRequestBodyDrainedWithoutClose, using a body-replaying RoundTripper modelled ongo-retryablehttp. It fails onmain(no client metrics recorded) and passes with this change.asyncBlockedCloseTransportnow reads exactlyContentLengthbytes instead of draining to EOF. Without this,TestTransportMetricsRecordedWhenRequestBodyCloseBlockswould be satisfied by the EOF path and stop covering the blocking-Closepath. I checked that it still fails iftrackedRequestBody.Closestops finalizing before delegating.Testing
go test -race -count=3 ./...ininstrumentation/net/http/otelhttpgolangci-lint run(v2.15.0 fromtools/go.mod): 0 issuesgithub.com/hashicorp/go-retryablehttpv0.7.8 underotelhttp.NewTransport, one GET and one POST:http.client.request.durationcount is 1 with v0.72.0 and 2 with this branch.make precommitpasses.BenchmarkTransportRoundTrip,mainvs this branch,-cpu=1, 16 interleaved runs each, compared withbenchstat: no significant change in time (geomean −0.05%, every case p > 0.1), and identical B/op and allocs/op in every case. The added work is oneio.EOFcomparison perRead, plus an atomic store and a lock round-trip inmaybeRecordonce the body reaches EOF.A RoundTripper that neither reads the body to EOF nor closes it would still drop the metrics. That breaks the
RoundTrippercontract, and handling it would need a fallback such as recording when the response body is finalized. I left that out to keep this fix narrow, but I'm happy to add it if you think it's worth it.