-
Notifications
You must be signed in to change notification settings - Fork 755
fix: get body using request.GetBody to enable request reuse #8352
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
Changes from all commits
cd1f928
02efc3f
7a3096c
5daa015
8500cce
9d61c93
eb4ee53
2377ac9
fb1fb57
0129686
d3055f8
124549d
0b3beb3
0f5a179
880b589
260a8b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ package otelhttp // import "go.opentelemetry.io/contrib/instrumentation/net/http | |
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "net/http/httptrace" | ||
|
|
@@ -83,6 +84,8 @@ func defaultTransportFormatter(_ string, r *http.Request) string { | |
| // RoundTrip creates a Span and propagates its context via the provided request's headers | ||
| // before handing the request to the configured base RoundTripper. The created span will | ||
| // end when the response body is closed or when a read from the body returns io.EOF. | ||
| // If GetBody returns an error, the error is reported via otel.Handle and the request | ||
| // continues with the original Body. | ||
| func (t *Transport) RoundTrip(r *http.Request) (*http.Response, error) { | ||
| requestStartTime := time.Now() | ||
| for _, f := range t.filters { | ||
|
|
@@ -117,11 +120,22 @@ func (t *Transport) RoundTrip(r *http.Request) (*http.Response, error) { | |
|
|
||
| r = r.Clone(ctx) // According to RoundTripper spec, we shouldn't modify the origin request. | ||
|
|
||
| // if request body is nil or NoBody, we don't want to mutate the body as it | ||
| // GetBody is preferred over direct access to Body if the function is set. | ||
| // If the resulting body is nil or is NoBody, we don't want to mutate the body as it | ||
| // will affect the identity of it in an unforeseeable way because we assert | ||
| // ReadCloser fulfills a certain interface and it is indeed nil or NoBody. | ||
| bw := request.NewBodyWrapper(r.Body, func(int64) {}) | ||
| if r.Body != nil && r.Body != http.NoBody { | ||
| body := r.Body | ||
| if r.GetBody != nil { | ||
| b, err := r.GetBody() | ||
| if err != nil { | ||
| otel.Handle(fmt.Errorf("http.Request GetBody returned an error: %w", err)) | ||
|
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. 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
Member
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. See #8352 (comment)
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. There are two issues, actually:
The unit test encodes incorrect behavior. My understanding is that the code should:
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. |
||
| } else { | ||
| body = b | ||
| } | ||
| } | ||
|
|
||
| bw := request.NewBodyWrapper(body, func(int64) {}) | ||
|
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. 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. |
||
| if body != nil && body != http.NoBody { | ||
dmathieu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| r.Body = bw | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.