← Back to Pull requests

fix(otelhttp): record client metrics when request body is drained but not closed #10008

Open
unknown opened this pull request with 309 lines diff across 3 files
View on GitHub
unknown unknown commented on Oct 23, 2026 13:09 UTC
Author

Fixes #10003

Since #8684 (v0.72.0), Transport waits for the request body to be closed before recording http.client.request.duration and http.client.request.body.size. Retrying RoundTrippers placed under otelhttp.NewTransport, such as go-retryablehttp, read the body with io.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.EOF as a final body state alongside Close. A drained body yields no more bytes, so its read count is final. This keeps the rule from #8684 that a read count compared to ContentLength is never treated as completion: only Close or io.EOF finalize the body.

Changes

  • trackedRequestBody.Read marks the body final on io.EOF and calls maybeRecord.

  • New test TestTransportMetricsRecordedWhenRequestBodyDrainedWithoutClose, using a body-replaying RoundTripper modelled on go-retryablehttp. It fails on main (no client metrics recorded) and passes with this change.

  • asyncBlockedCloseTransport now reads exactly ContentLength bytes instead of draining to EOF. Without this, TestTransportMetricsRecordedWhenRequestBodyCloseBlocks would be satisfied by the EOF path and stop covering the blocking-Close path. I checked that it still fails if trackedRequestBody.Close stops finalizing before delegating.
  • Testing

  • go test -race -count=3 ./... in instrumentation/net/http/otelhttp

  • golangci-lint run (v2.15.0 from tools/go.mod): 0 issues

  • End to end with the real github.com/hashicorp/go-retryablehttp v0.7.8 under otelhttp.NewTransport, one GET and one POST: http.client.request.duration count is 1 with v0.72.0 and 2 with this branch.

  • make precommit passes.

  • BenchmarkTransportRoundTrip, main vs this branch, -cpu=1, 16 interleaved runs each, compared with benchstat: 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 one io.EOF comparison per Read, plus an atomic store and a lock round-trip in maybeRecord once the body reaches EOF.
  • A RoundTripper that neither reads the body to EOF nor closes it would still drop the metrics. That breaks the RoundTripper contract, 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.

    Diff: 309 lines (+231 / -78) across 3 files Reviewer: MrAlias Review duration: 2890m
    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
    0.40
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 40.2% (MEDIUM) · Higher = larger blast radius
    40.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 21.4% · Safe to merge
    21.4%