Skip to content

fix: get body using request.GetBody to enable request reuse#8352

Merged
dmathieu merged 16 commits intoopen-telemetry:mainfrom
adomaskizogian:main
Jan 9, 2026
Merged

fix: get body using request.GetBody to enable request reuse#8352
dmathieu merged 16 commits intoopen-telemetry:mainfrom
adomaskizogian:main

Conversation

@adomaskizogian
Copy link
Contributor

@adomaskizogian adomaskizogian commented Jan 6, 2026

fixes #8258

get http request body from GetBody if it is set, otherwise fallback to Body field.
This allows reusing the request body bytes if the same http request struct is reused on subsequent requests.

@adomaskizogian adomaskizogian requested review from a team and dmathieu as code owners January 6, 2026 15:02
@codecov
Copy link

codecov bot commented Jan 6, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.3%. Comparing base (7d8be10) to head (260a8b3).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@          Coverage Diff          @@
##            main   #8352   +/-   ##
=====================================
  Coverage   82.3%   82.3%           
=====================================
  Files        186     186           
  Lines      13765   13772    +7     
=====================================
+ Hits       11330   11337    +7     
  Misses      2030    2030           
  Partials     405     405           
Files with missing lines Coverage Δ
instrumentation/net/http/otelhttp/transport.go 95.6% <100.0%> (+0.2%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dmathieu dmathieu merged commit 464789f into open-telemetry:main Jan 9, 2026
28 checks passed
@MrAlias MrAlias added this to the v1.40.0 milestone Jan 30, 2026
if r.GetBody != nil {
b, err := r.GetBody()
if err != nil {
otel.Handle(fmt.Errorf("http.Request GetBody returned an error: %w", err))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this just return the error, as the stdlib does?

https://github.com/golang/go/blob/go1.25.6/src/net/http/client.go#L673

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two issues, actually:

  • If I use transport := http.DefaultTransport and set a breakpoint in the GetBody impl in that test, it is not triggered by the test execution. But it is triggered with the OTEL transport. This means OTEL transport calls GetBody when stdlib does not. stdlib only calls it if it needs another instance of the body (i.e. for second, third, etc attempts).
  • stdlib returns the error from GetBody, it does not proceed. OTEL transport reports it and proceeds.

The unit test encodes incorrect behavior.

My understanding is that the code should:

  • wrap the body that is set in the request, if it is not nil and not NoBody.
  • wrap the GetBody func but not call it. The underlying transport will call it if necessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}

bw := request.NewBodyWrapper(body, func(int64) {})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should only be allocated if it is actually needed - to reduce RAM usage.

Also, this object is unconditionally used below - it shouldn't be if it is not used as a body.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Unable to reuse http.Request with body when client is instrumented with otelhttp.NewTransport

5 participants