Commit be1c3faa authored by Tomasz Maczukin's avatar Tomasz Maczukin

Add PoC of git clone/fetch requests metric

parent eb451e03
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
...@@ -27,6 +28,14 @@ var ( ...@@ -27,6 +28,14 @@ var (
}, },
[]string{"code", "method"}, []string{"code", "method"},
) )
cloneFetchRequests = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gitlab_workhorse_git_clone_fetch_requests",
Help: "How many Git clone/fetch requests for CI have been processed by gitlab-workhorse, partitioned by CI yes/no status.",
},
[]string{"ci"},
)
) )
func init() { func init() {
...@@ -41,6 +50,7 @@ func SetCustomResponseLogger(writer io.Writer) { ...@@ -41,6 +50,7 @@ func SetCustomResponseLogger(writer io.Writer) {
func registerPrometheusMetrics() { func registerPrometheusMetrics() {
prometheus.MustRegister(sessionsActive) prometheus.MustRegister(sessionsActive)
prometheus.MustRegister(requestsTotal) prometheus.MustRegister(requestsTotal)
prometheus.MustRegister(cloneFetchRequests)
} }
type LoggingResponseWriter struct { type LoggingResponseWriter struct {
...@@ -88,6 +98,24 @@ func (l *LoggingResponseWriter) Log(r *http.Request) { ...@@ -88,6 +98,24 @@ func (l *LoggingResponseWriter) Log(r *http.Request) {
l.status, l.written, r.Referer(), r.UserAgent(), duration.Seconds(), l.status, l.written, r.Referer(), r.UserAgent(), duration.Seconds(),
) )
l.countCloneFetchRequests(r)
sessionsActive.Dec() sessionsActive.Dec()
requestsTotal.WithLabelValues(strconv.Itoa(l.status), r.Method).Inc() requestsTotal.WithLabelValues(strconv.Itoa(l.status), r.Method).Inc()
} }
func (l *LoggingResponseWriter) countCloneFetchRequests(r *http.Request) {
if l.status == 401 || !strings.Contains(r.RequestURI, "/info/refs?service=git-upload-pack") {
return
}
u, _, ok := r.BasicAuth()
var forCi string
if ok && u == "gitlab-ci-token" {
forCi = "1"
} else {
forCi = "0"
}
cloneFetchRequests.WithLabelValues(forCi).Inc()
}
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