Commit 81fc0554 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Add open gauge counter

parent 73345bef
...@@ -19,11 +19,18 @@ const ( ...@@ -19,11 +19,18 @@ const (
var ( var (
registerHandlerHits = prometheus.NewCounterVec( registerHandlerHits = prometheus.NewCounterVec(
prometheus.CounterOpts{ prometheus.CounterOpts{
Name: "gitlab_workhorse_builds_register_handler", Name: "gitlab_workhorse_builds_register_handler_hits",
Help: "How many connections gitlab-workhorse has opened in total. Can be used to track Redis connection rate for this process", Help: "Describes how many requests in different states hit a register handler",
}, },
[]string{"status"}, []string{"status"},
) )
registerHandlerOpen = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "gitlab_workhorse_builds_register_handler_open",
Help: "Describes how many requests is currently open in given state",
},
[]string{"state"},
)
) )
type largeBodyError struct{ error } type largeBodyError struct{ error }
...@@ -32,6 +39,7 @@ type watchError struct{ error } ...@@ -32,6 +39,7 @@ type watchError struct{ error }
func init() { func init() {
prometheus.MustRegister( prometheus.MustRegister(
registerHandlerHits, registerHandlerHits,
registerHandlerOpen,
) )
} }
...@@ -45,6 +53,27 @@ func readRunnerToken(r *http.Request) (string, error) { ...@@ -45,6 +53,27 @@ func readRunnerToken(r *http.Request) (string, error) {
return token, nil return token, nil
} }
func readRunnerBody(w http.ResponseWriter, r *http.Request) ([]byte, error) {
registerHandlerOpen.WithLabelValues("reading").Inc()
defer registerHandlerOpen.WithLabelValues("reading").Dec()
return helper.ReadRequestBody(w, r, maxRegisterBodySize)
}
func proxyRegisterRequest(h http.Handler, w http.ResponseWriter, r *http.Request) {
registerHandlerOpen.WithLabelValues("proxying").Inc()
defer registerHandlerOpen.WithLabelValues("proxying").Dec()
h.ServeHTTP(w, r)
}
func watchForRunnerChange(token, lastUpdate string, duration time.Duration) (redis.WatchKeyStatus, error) {
registerHandlerOpen.WithLabelValues("watching").Inc()
defer registerHandlerOpen.WithLabelValues("watching").Dec()
return redis.WatchKey(runnerBuildQueue+token, lastUpdate, duration)
}
func RegisterHandler(h http.Handler, pollingDuration time.Duration) http.Handler { func RegisterHandler(h http.Handler, pollingDuration time.Duration) http.Handler {
if pollingDuration == 0 { if pollingDuration == 0 {
return h return h
...@@ -53,13 +82,12 @@ func RegisterHandler(h http.Handler, pollingDuration time.Duration) http.Handler ...@@ -53,13 +82,12 @@ func RegisterHandler(h http.Handler, pollingDuration time.Duration) http.Handler
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
lastUpdate := r.Header.Get(runnerBuildQueueValue) lastUpdate := r.Header.Get(runnerBuildQueueValue)
if lastUpdate == "" { if lastUpdate == "" {
// The client doesn't have update, fail
registerHandlerHits.WithLabelValues("missing-value").Inc() registerHandlerHits.WithLabelValues("missing-value").Inc()
h.ServeHTTP(w, r) proxyRegisterRequest(h, w, r)
return return
} }
requestBody, err := helper.ReadRequestBody(w, r, maxRegisterBodySize) requestBody, err := readRunnerBody(w, r)
if err != nil { if err != nil {
registerHandlerHits.WithLabelValues("body-read-error").Inc() registerHandlerHits.WithLabelValues("body-read-error").Inc()
helper.Fail500(w, r, &largeBodyError{err}) helper.Fail500(w, r, &largeBodyError{err})
...@@ -69,11 +97,11 @@ func RegisterHandler(h http.Handler, pollingDuration time.Duration) http.Handler ...@@ -69,11 +97,11 @@ func RegisterHandler(h http.Handler, pollingDuration time.Duration) http.Handler
runnerToken, err := readRunnerToken(helper.CloneRequestWithNewBody(r, requestBody)) runnerToken, err := readRunnerToken(helper.CloneRequestWithNewBody(r, requestBody))
if runnerToken == "" || err != nil { if runnerToken == "" || err != nil {
registerHandlerHits.WithLabelValues("body-parse-error").Inc() registerHandlerHits.WithLabelValues("body-parse-error").Inc()
h.ServeHTTP(w, r) proxyRegisterRequest(h, w, r)
return return
} }
result, err := redis.WatchKey(runnerBuildQueue+runnerToken, lastUpdate, pollingDuration) result, err := watchForRunnerChange(runnerToken, lastUpdate, pollingDuration)
if err != nil { if err != nil {
registerHandlerHits.WithLabelValues("watch-error").Inc() registerHandlerHits.WithLabelValues("watch-error").Inc()
helper.Fail500(w, r, &watchError{err}) helper.Fail500(w, r, &watchError{err})
...@@ -85,7 +113,7 @@ func RegisterHandler(h http.Handler, pollingDuration time.Duration) http.Handler ...@@ -85,7 +113,7 @@ func RegisterHandler(h http.Handler, pollingDuration time.Duration) http.Handler
// We proxy request to Rails, to see whether we can receive the build // We proxy request to Rails, to see whether we can receive the build
case redis.WatchKeyStatusAlreadyChanged: case redis.WatchKeyStatusAlreadyChanged:
registerHandlerHits.WithLabelValues("already-changed").Inc() registerHandlerHits.WithLabelValues("already-changed").Inc()
h.ServeHTTP(w, helper.CloneRequestWithNewBody(r, requestBody)) proxyRegisterRequest(h, w, helper.CloneRequestWithNewBody(r, requestBody))
// It means that we detected a change after watching. // It means that we detected a change after watching.
// We could potentially proxy request to Rails, but... // We could potentially proxy request to Rails, but...
......
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