Commit 83e148de authored by Jacob Vosmaer (GitLab)'s avatar Jacob Vosmaer (GitLab)

Merge branch 'bjk/histogram' into 'master'

Add histograms to routes

See merge request !184
parents e6cfe0c0 d1522d6a
......@@ -4,8 +4,10 @@ import (
"net/http"
"path"
"regexp"
"time"
"github.com/gorilla/websocket"
"github.com/prometheus/client_golang/prometheus"
apipkg "gitlab.com/gitlab-org/gitlab-workhorse/internal/api"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/artifacts"
......@@ -39,6 +41,20 @@ const (
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 {
if len(regexpStr) == 0 {
return nil
......@@ -47,11 +63,19 @@ func compileRegexp(regexpStr string) *regexp.Regexp {
return regexp.MustCompile(regexpStr)
}
func instrumentDuration(h http.Handler, method string, regexpStr string) 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())
})
}
func route(method, regexpStr string, handler http.Handler, matchers ...matcherFunc) routeEntry {
return routeEntry{
method: method,
regex: compileRegexp(regexpStr),
handler: denyWebsocket(handler),
handler: instrumentDuration(denyWebsocket(handler), method, regexpStr),
matchers: matchers,
}
}
......@@ -60,7 +84,7 @@ func wsRoute(regexpStr string, handler http.Handler, matchers ...matcherFunc) ro
return routeEntry{
method: "GET",
regex: compileRegexp(regexpStr),
handler: handler,
handler: instrumentDuration(handler, "GET", regexpStr),
matchers: append(matchers, websocket.IsWebSocketUpgrade),
}
}
......
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