Commit 98929304 authored by Ben Kochie's avatar Ben Kochie Committed by Ben Kochie

Add histograms to routes

Add instrumentation middleware function to produce histograms per
route/method.
parent e6cfe0c0
...@@ -4,8 +4,10 @@ import ( ...@@ -4,8 +4,10 @@ import (
"net/http" "net/http"
"path" "path"
"regexp" "regexp"
"time"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/prometheus/client_golang/prometheus"
apipkg "gitlab.com/gitlab-org/gitlab-workhorse/internal/api" apipkg "gitlab.com/gitlab-org/gitlab-workhorse/internal/api"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/artifacts" "gitlab.com/gitlab-org/gitlab-workhorse/internal/artifacts"
...@@ -39,6 +41,20 @@ const ( ...@@ -39,6 +41,20 @@ const (
projectPattern = `^/([^/]+/){1,}[^/]+/` projectPattern = `^/([^/]+/){1,}[^/]+/`
) )
var (
routeRequestDurations = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "gitlab_workhorse_request_duration_seconds",
Help: "A histogram of request times in seconds",
Buckets: prometheus.ExponentialBuckets(0.01, 2.5, 10),
},
[]string{"method", "route"},
)
)
func init() {
prometheus.MustRegister(routeRequestDurations)
}
func compileRegexp(regexpStr string) *regexp.Regexp { func compileRegexp(regexpStr string) *regexp.Regexp {
if len(regexpStr) == 0 { if len(regexpStr) == 0 {
return nil return nil
...@@ -48,10 +64,17 @@ func compileRegexp(regexpStr string) *regexp.Regexp { ...@@ -48,10 +64,17 @@ func compileRegexp(regexpStr string) *regexp.Regexp {
} }
func route(method, regexpStr string, handler http.Handler, matchers ...matcherFunc) routeEntry { func route(method, regexpStr string, handler http.Handler, matchers ...matcherFunc) routeEntry {
instr := func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
h.ServeHTTP(w, r)
routeRequestDurations.WithLabelValues(method, regexpStr).Observe(time.Since(start).Seconds())
})
}
return routeEntry{ return routeEntry{
method: method, method: method,
regex: compileRegexp(regexpStr), regex: compileRegexp(regexpStr),
handler: denyWebsocket(handler), handler: instr(denyWebsocket(handler)),
matchers: matchers, matchers: matchers,
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment