Skip to content
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

wfe: use wildcard patterns in HTTP handlers #7791

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
metrics: call ServeHTTP directly
This allows us to take advantage of http.ServeMux's wildcard handling. If we
first retrieve the handler for a pattern, and then call it ourselves, the values
in `request.PathValue(...)` do not get filled.
jsha committed Nov 7, 2024
commit 17efd068ada3e5dad4a13cdc5c6c043425aafaca
4 changes: 4 additions & 0 deletions cmd/ocsp-responder/main.go
Original file line number Diff line number Diff line change
@@ -276,6 +276,10 @@ func (om *ocspMux) Handler(_ *http.Request) (http.Handler, string) {
return om.handler, "/"
}

func (om *ocspMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
om.handler.ServeHTTP(w, r)
}

func mux(responderPath string, source responder.Source, timeout time.Duration, stats prometheus.Registerer, oTelHTTPOptions []otelhttp.Option, logger blog.Logger, sampleRate int) http.Handler {
stripPrefix := http.StripPrefix(responderPath, responder.NewResponder(source, timeout, stats, logger, sampleRate))
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
7 changes: 4 additions & 3 deletions metrics/measured_http/http.go
Original file line number Diff line number Diff line change
@@ -31,12 +31,13 @@ func (r *responseWriterWithStatus) Write(body []byte) (int, error) {
return r.ResponseWriter.Write(body)
}

// serveMux is a partial interface wrapper for the method http.ServeMux
// serveMux is a partial interface wrapper for the methods http.ServeMux
// exposes that we use. This is needed so that we can replace the default
// http.ServeMux in ocsp-responder where we don't want to use its path
// canonicalization.
type serveMux interface {
Handler(*http.Request) (http.Handler, string)
ServeHTTP(w http.ResponseWriter, r *http.Request)
}

// MeasuredHandler wraps an http.Handler and records prometheus stats
@@ -80,7 +81,7 @@ func (h *MeasuredHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
begin := h.clk.Now()
rwws := &responseWriterWithStatus{w, 0}

subHandler, pattern := h.Handler(r)
_, pattern := h.serveMux.Handler(r)
h.inFlightRequestsGauge.WithLabelValues(pattern).Inc()
defer h.inFlightRequestsGauge.WithLabelValues(pattern).Dec()

@@ -104,5 +105,5 @@ func (h *MeasuredHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}).Observe(h.clk.Since(begin).Seconds())
}()

subHandler.ServeHTTP(rwws, r)
h.serveMux.ServeHTTP(rwws, r)
}