Skip to content
Snippets Groups Projects
Unverified Commit 1a16aa48 authored by IvoGoman's avatar IvoGoman Committed by GitHub
Browse files

feat(metrics): add response_size, request_duration histograms (#3748)


replaces felixge/httpsnoop with prometheus/client_golang instrumentation
adds histograms for response_size_bytes & request_duration_seconds

Signed-off-by: default avatarIvo Gosemann <ivo.gosemann@sap.com>
parent 8333c84e
No related branches found
No related tags found
No related merge requests found
...@@ -11,7 +11,6 @@ require ( ...@@ -11,7 +11,6 @@ require (
github.com/beevik/etree v1.4.0 github.com/beevik/etree v1.4.0
github.com/coreos/go-oidc/v3 v3.11.0 github.com/coreos/go-oidc/v3 v3.11.0
github.com/dexidp/dex/api/v2 v2.1.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/fsnotify/fsnotify v1.7.0
github.com/ghodss/yaml v1.0.0 github.com/ghodss/yaml v1.0.0
github.com/go-jose/go-jose/v4 v4.0.4 github.com/go-jose/go-jose/v4 v4.0.4
...@@ -57,6 +56,7 @@ require ( ...@@ -57,6 +56,7 @@ require (
github.com/coreos/go-semver v0.3.0 // indirect github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/davecgh/go-spew v1.1.1 // 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-asn1-ber/asn1-ber v1.5.5 // indirect
github.com/go-logr/logr v1.4.2 // indirect github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect github.com/go-logr/stdr v1.2.2 // indirect
......
...@@ -15,18 +15,17 @@ import ( ...@@ -15,18 +15,17 @@ import (
"os" "os"
"path" "path"
"sort" "sort"
"strconv"
"strings" "strings"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
gosundheit "github.com/AppsFlyer/go-sundheit" gosundheit "github.com/AppsFlyer/go-sundheit"
"github.com/felixge/httpsnoop"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/gorilla/handlers" "github.com/gorilla/handlers"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
"github.com/dexidp/dex/connector" "github.com/dexidp/dex/connector"
...@@ -332,7 +331,7 @@ func newServer(ctx context.Context, c Config, rotationStrategy rotationStrategy) ...@@ -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 return handler.ServeHTTP
} }
...@@ -340,18 +339,28 @@ func newServer(ctx context.Context, c Config, rotationStrategy rotationStrategy) ...@@ -340,18 +339,28 @@ func newServer(ctx context.Context, c Config, rotationStrategy rotationStrategy)
requestCounter := prometheus.NewCounterVec(prometheus.CounterOpts{ requestCounter := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "http_requests_total", Name: "http_requests_total",
Help: "Count of all HTTP requests.", Help: "Count of all HTTP requests.",
}, []string{"handler", "code", "method"}) }, []string{"code", "method", "handler"})
err = c.PrometheusRegistry.Register(requestCounter) durationHist := prometheus.NewHistogramVec(prometheus.HistogramOpts{
if err != nil { Name: "request_duration_seconds",
return nil, fmt.Errorf("server: Failed to register Prometheus HTTP metrics: %v", err) Help: "A histogram of latencies for requests.",
} Buckets: []float64{.25, .5, 1, 2.5, 5, 10},
}, []string{"code", "method", "handler"})
instrumentHandlerCounter = func(handlerName string, handler http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { sizeHist := prometheus.NewHistogramVec(prometheus.HistogramOpts{
m := httpsnoop.CaptureMetrics(handler, w, r) Name: "response_size_bytes",
requestCounter.With(prometheus.Labels{"handler": handlerName, "code": strconv.Itoa(m.Code), "method": r.Method}).Inc() 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) ...@@ -401,7 +410,7 @@ func newServer(ctx context.Context, c Config, rotationStrategy rotationStrategy)
} }
r = r.WithContext(rCtx) r = r.WithContext(rCtx)
instrumentHandlerCounter(handlerName, handler)(w, r) instrumentHandler(handlerName, handler)(w, r)
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment