Skip to content

otelhttp: Remove custom wrapper after handling request #6914

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

VirrageS
Copy link

Closes #6908

@VirrageS VirrageS requested review from dmathieu and a team as code owners March 11, 2025 10:39
@VirrageS VirrageS force-pushed the fix-handler branch 4 times, most recently from a47db8c to 25e2f1c Compare March 11, 2025 10:48
Copy link

codecov bot commented Mar 11, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 75.6%. Comparing base (3ea6585) to head (a3c36c8).
Report is 21 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@          Coverage Diff          @@
##            main   #6914   +/-   ##
=====================================
  Coverage   75.5%   75.6%           
=====================================
  Files        207     207           
  Lines      19319   19362   +43     
=====================================
+ Hits       14601   14651   +50     
+ Misses      4283    4275    -8     
- Partials     435     436    +1     
Files with missing lines Coverage Δ
...trumentation/github.com/gorilla/mux/otelmux/mux.go 95.7% <100.0%> (+0.1%) ⬆️
instrumentation/net/http/otelhttp/handler.go 93.0% <100.0%> (+0.1%) ⬆️

... and 6 files with indirect coverage changes

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

@dmathieu
Copy link
Member

Could you add a changelog entry?

@VirrageS VirrageS force-pushed the fix-handler branch 2 times, most recently from 6834b55 to dc01b21 Compare March 12, 2025 10:51
@dmathieu
Copy link
Member

Should we do this every time we use the body wrapper?
https://github.com/search?q=repo%3Aopen-telemetry%2Fopentelemetry-go-contrib%20NewBodyWrapper&type=code

@VirrageS
Copy link
Author

Should we do this every time we use the body wrapper? https://github.com/search?q=repo%3Aopen-telemetry%2Fopentelemetry-go-contrib%20NewBodyWrapper&type=code

I wasn't sure in the beginning as I didn't know gorilla/mux that much. But from what I see it is simple muxer. So yes, I think we should update it too.

@dmathieu
Copy link
Member

Could you do that in this PR?

@VirrageS
Copy link
Author

Yeah, it is done already :)


// Restore the original body after the request is processed to avoid issues
// with extra wrapper since `http/server.go` later checks type of `r.Body`.
defer func() { r.Body = prevBody }()
Copy link
Member

Choose a reason for hiding this comment

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

This code is missing tests.

Copy link
Author

Choose a reason for hiding this comment

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

Added. Turns out that we don't really need this code in gorilla/mux as internally gorilla/mux does the following:

...
	if r.Match(req, &match) {
		handler = match.Handler
		req = requestWithVars(req, match.Vars)
		req = requestWithRoute(req, match.Route)
	}
...

func requestWithVars(r *http.Request, vars map[string]string) *http.Request {
	ctx := context.WithValue(r.Context(), varsKey, vars)
	return r.WithContext(ctx)
}

func requestWithRoute(r *http.Request, route *Route) *http.Request {
	ctx := context.WithValue(r.Context(), routeKey, route)
	return r.WithContext(ctx)
}

and r.WithContext(ctx) does a shallow copy of the http.Request so that the processed request is different than the original request and therefore r.Body is a different pointer (but still pointing to the same memory). Thus, original r.Body is left untouched.

But I would still leave this code just in case something changes in the future in gorilla/mux and for consistency with net/http code.

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.

otelhttp.NewHandler body wrapper alters default behaviour of Expect: 100-continue header handling
2 participants