diff --git a/go.mod b/go.mod index 50774ab83d1d11127096e5b6473e4a8293bb3bbd..8e619d7bc5743edb496890387f2f95ebafc6c58a 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,6 @@ require ( github.com/beevik/etree v1.4.0 github.com/coreos/go-oidc/v3 v3.11.0 github.com/dexidp/dex/api/v2 v2.1.0 - github.com/felixge/httpsnoop v1.0.4 github.com/fsnotify/fsnotify v1.7.0 github.com/ghodss/yaml v1.0.0 github.com/go-jose/go-jose/v4 v4.0.4 @@ -57,6 +56,7 @@ require ( github.com/coreos/go-semver v0.3.0 // indirect github.com/coreos/go-systemd/v22 v22.3.2 // indirect github.com/davecgh/go-spew v1.1.1 // indirect + github.com/felixge/httpsnoop v1.0.4 // indirect github.com/go-asn1-ber/asn1-ber v1.5.5 // indirect github.com/go-logr/logr v1.4.2 // indirect github.com/go-logr/stdr v1.2.2 // indirect diff --git a/server/server.go b/server/server.go index 1cf71c5038b1f61d69ea1aa45d514f60c27f06f1..e299e32f7d8ef2608c5dbf95a55624f2e20701b1 100644 --- a/server/server.go +++ b/server/server.go @@ -15,18 +15,17 @@ import ( "os" "path" "sort" - "strconv" "strings" "sync" "sync/atomic" "time" gosundheit "github.com/AppsFlyer/go-sundheit" - "github.com/felixge/httpsnoop" "github.com/google/uuid" "github.com/gorilla/handlers" "github.com/gorilla/mux" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" "golang.org/x/crypto/bcrypt" "github.com/dexidp/dex/connector" @@ -332,7 +331,7 @@ func newServer(ctx context.Context, c Config, rotationStrategy rotationStrategy) } } - instrumentHandlerCounter := func(_ string, handler http.Handler) http.HandlerFunc { + instrumentHandler := func(_ string, handler http.Handler) http.HandlerFunc { return handler.ServeHTTP } @@ -340,18 +339,28 @@ func newServer(ctx context.Context, c Config, rotationStrategy rotationStrategy) requestCounter := prometheus.NewCounterVec(prometheus.CounterOpts{ Name: "http_requests_total", Help: "Count of all HTTP requests.", - }, []string{"handler", "code", "method"}) - - err = c.PrometheusRegistry.Register(requestCounter) - if err != nil { - return nil, fmt.Errorf("server: Failed to register Prometheus HTTP metrics: %v", err) - } - - instrumentHandlerCounter = func(handlerName string, handler http.Handler) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - m := httpsnoop.CaptureMetrics(handler, w, r) - requestCounter.With(prometheus.Labels{"handler": handlerName, "code": strconv.Itoa(m.Code), "method": r.Method}).Inc() - } + }, []string{"code", "method", "handler"}) + + durationHist := prometheus.NewHistogramVec(prometheus.HistogramOpts{ + Name: "request_duration_seconds", + Help: "A histogram of latencies for requests.", + Buckets: []float64{.25, .5, 1, 2.5, 5, 10}, + }, []string{"code", "method", "handler"}) + + sizeHist := prometheus.NewHistogramVec(prometheus.HistogramOpts{ + Name: "response_size_bytes", + Help: "A histogram of response sizes for requests.", + Buckets: []float64{200, 500, 900, 1500}, + }, []string{"code", "method", "handler"}) + + c.PrometheusRegistry.MustRegister(requestCounter, durationHist, sizeHist) + + instrumentHandler = func(handlerName string, handler http.Handler) http.HandlerFunc { + return promhttp.InstrumentHandlerDuration(durationHist.MustCurryWith(prometheus.Labels{"handler": handlerName}), + promhttp.InstrumentHandlerCounter(requestCounter.MustCurryWith(prometheus.Labels{"handler": handlerName}), + promhttp.InstrumentHandlerResponseSize(sizeHist.MustCurryWith(prometheus.Labels{"handler": handlerName}), handler), + ), + ) } } @@ -401,7 +410,7 @@ func newServer(ctx context.Context, c Config, rotationStrategy rotationStrategy) } r = r.WithContext(rCtx) - instrumentHandlerCounter(handlerName, handler)(w, r) + instrumentHandler(handlerName, handler)(w, r) } }