Skip to content

Commit

Permalink
feat(metrics): add response_size, request_duration histograms (#3748)
Browse files Browse the repository at this point in the history
replaces felixge/httpsnoop with prometheus/client_golang instrumentation
adds histograms for response_size_bytes & request_duration_seconds

Signed-off-by: Ivo Gosemann <ivo.gosemann@sap.com>
  • Loading branch information
IvoGoman authored Sep 17, 2024
1 parent 8333c84 commit 1a16aa4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
41 changes: 25 additions & 16 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -332,26 +331,36 @@ 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
}

if c.PrometheusRegistry != nil {
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),
),
)
}
}

Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 1a16aa4

Please sign in to comment.