refactor: replace routeNormalizer with context pointer for route labeling - #19
Merged
Merged
Conversation
…ling routeNormalizer in the logging middleware independently reimplemented the same sort+match logic as the router. Any future change to routing behavior (trailing slash, exact-match, regex) would silently diverge the Prometheus labels from the actual routing decision. The middleware now allocates a *string and stores it in the request context under ctxkey.RouteLabelKey before calling next.ServeHTTP. The router writes the matched prefix through that pointer after route selection. A pointer is required because r.WithContext returns a new *http.Request local to the callee — a plain string value would not propagate back to the middleware. New package internal/ctxkey defines the shared key type, avoiding import cycles between middleware and router packages. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.
Summary
routeNormalizerin the logging middleware independently reimplemented the same sort+match logic as the router*stringand stores it in the request context underctxkey.RouteLabelKeybefore callingnext.ServeHTTPr.WithContextreturns a new*http.Requestlocal to the callee — a plain string would not propagate back to the middlewareinternal/ctxkeydefines the shared key type, avoiding import cycles betweenmiddlewareandrouterFiles changed
internal/ctxkey/ctxkey.go— new shared context key packageinternal/middleware/logging.go— removedrouteNormalizer, reads label via context pointerinternal/middleware/logging_test.go— replacedrouteNormalizerunit tests with behavior tests for the context flowinternal/router/router.go— writes matched prefix to context pointer after route selectioncmd/gateway/main.go— removedroutePrefixesslice and extra argument fromLogging()Test plan
go build ./...succeedsgo test -race ./...passes — 4 new tests covering context pointer flow, fallback to"unknown", plain context compatibilityCloses #10
🤖 Generated with Claude Code