middleware: set http.Request.Pattern to the matched route#500
Merged
fredbi merged 1 commit intoJul 15, 2026
Merged
Conversation
Since Go 1.22, http.Request.Pattern carries the route pattern that matched a request; router-agnostic observability middleware (otelhttp, Prometheus, structured logging) reads it to label spans/metrics by route template instead of raw path. net/http.ServeMux populates it. Set it from RouteInfo to the matched route (BasePath + PathPattern) so those middleware work with go-openapi's router without bespoke glue. It is set in place, mirroring ServeMux, so a handler wrapping the router observes it, and stays empty for unmatched requests. Signed-off-by: delthas <git@delthas.fr>
fdd1e9d to
718c0f6
Compare
Member
|
Thank you @delthas I am going to review this ASAP (most likely tomorrow). See ya. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #500 +/- ##
==========================================
- Coverage 83.42% 83.36% -0.07%
==========================================
Files 64 64
Lines 4520 4521 +1
==========================================
- Hits 3771 3769 -2
- Misses 581 583 +2
- Partials 168 169 +1 ☔ View full report in Codecov by Harness. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Since Go 1.22,
http.Request.Patterncarries the route pattern that matched a request. Router-agnostic observability middleware — OpenTelemetry'sotelhttp, Prometheus, structured logging — reads it to label spans/metrics by route template (bounded cardinality) rather than raw path.net/http.ServeMuxpopulates it; third-party routers increasingly do the same.go-openapi already matches the route (
middleware.MatchedRouteFrom), but consumers must hand-write a bespoke middleware to copy the matched pattern into their tracing/metrics labels — see e.g. go-swagger/go-swagger#3124, where users assemble anotelhttpwrapper by hand. PopulatingRequest.Patternlets standard middleware pick up the route with zero go-openapi-specific code (e.g.otelhttp.NewHandlerthen names spans by the matched route).Change
In
Context.RouteInfo, when a route matches, setrequest.PatterntoBasePath + PathPattern(e.g./api/pets/{id}). It is set in place — mirroringnet/http.ServeMux— so a handler wrapping the router observes it (aWithContextcopy would only be visible downstream). It stays empty for unmatched requests, exactly likeServeMux. No behavior change for code that does not readRequest.Pattern.Request.Patternrequires Go 1.22+; this module is already ongo 1.25.Test
Added
TestRouteInfoSetsRequestPattern(petstore fixture): assertsRequest.Patternequals the matchedBasePath+PathPatternon both the returned and the in-place request, and stays empty when no route matches.