Files
upstream-centrifugo/internal/middleware/http_instrumentation.go
upstream-mirror b352a9a30d
Some checks failed
test / Lint (push) Failing after 3s
test / Test with Go 1.26.8 (push) Failing after 2s
snapshot of https://github.com/centrifugal/centrifugo.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:17:21 +05:00

31 lines
1.2 KiB
Go

package middleware
import (
"net/http"
"strconv"
"github.com/centrifugal/centrifugo/v6/internal/metrics"
)
// HTTPServerInstrumentation is a middleware to instrument HTTP handlers.
// Since it adds and extra layer of response wrapping Centrifugo doesn't use it by default.
// Note, we can not simply collect durations here because we have handlers with long-lived
// connections which require special care. So for now we just count requests.
func HTTPServerInstrumentation(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rw := &statusResponseWriter{w, http.StatusOK}
next.ServeHTTP(rw, r)
status := strconv.Itoa(rw.status)
// Use the matched route pattern, not the raw request path. Subtree
// handlers (admin, swagger, dev, debug) accept arbitrary client-controlled
// sub-paths, so labeling by r.URL.Path would grow the Prometheus series map
// without bound (memory DoS). r.Pattern is the registered pattern that
// matched (e.g. "/admin/"), so cardinality is bounded by the route count.
path := r.Pattern
if path == "" {
path = "other"
}
metrics.HTTPRequestsTotal.WithLabelValues(path, r.Method, status).Inc()
})
}