Files
coder/coderd/httpmw/pprof.go
T
Steven Masley 34c46c0748 chore: rename service -> coder_service, remove agent_id label (#19241)
Pyroscope uses `service` tag for top level distinction. So move our
`service` -> `coder_service`
2025-08-07 13:58:39 -05:00

44 lines
1.2 KiB
Go

package httpmw
import (
"context"
"net/http"
"runtime/pprof"
"github.com/coder/coder/v2/coderd/pproflabel"
)
// WithProfilingLabels adds a pprof label to all http request handlers. This is
// primarily used to determine if load is coming from background jobs, or from
// http traffic.
func WithProfilingLabels(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// Label to differentiate between http and websocket requests. Websocket requests
// are assumed to be long-lived and more resource consuming.
requestType := "http"
if r.Header.Get("Upgrade") == "websocket" {
requestType = "websocket"
}
pprof.Do(ctx, pproflabel.Service(pproflabel.ServiceHTTPServer, pproflabel.RequestTypeTag, requestType), func(ctx context.Context) {
r = r.WithContext(ctx)
next.ServeHTTP(rw, r)
})
})
}
func WithStaticProfilingLabels(labels pprof.LabelSet) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
pprof.Do(ctx, labels, func(ctx context.Context) {
r = r.WithContext(ctx)
next.ServeHTTP(rw, r)
})
})
}
}