Commit 32b3fa9f authored by Tomasz Maczukin's avatar Tomasz Maczukin

Move metrics from helper/logging.go to git/git-http.go

parent bad25752
...@@ -16,10 +16,35 @@ import ( ...@@ -16,10 +16,35 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/prometheus/client_golang/prometheus"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/api" "gitlab.com/gitlab-org/gitlab-workhorse/internal/api"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper" "gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
) )
var (
gitHTTPRequests = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gitlab_workhorse_git_http_requests",
Help: "How many Git HTTP requests have been processed by gitlab-workhorse, partitioned by CI yes/no status.",
},
[]string{"ci"},
)
gitHTTPBytes = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gitlab_workhorse_git_http_bytes",
Help: "How many Git HTTP bytes have been send by gitlab-workhorse, partitioned by CI yes/no status.",
},
[]string{"ci"},
)
)
func init() {
prometheus.MustRegister(gitHTTPRequests)
prometheus.MustRegister(gitHTTPBytes)
}
func GetInfoRefs(a *api.API) http.Handler { func GetInfoRefs(a *api.API) http.Handler {
return repoPreAuthorizeHandler(a, handleGetInfoRefs) return repoPreAuthorizeHandler(a, handleGetInfoRefs)
} }
...@@ -38,8 +63,25 @@ func looksLikeRepo(p string) bool { ...@@ -38,8 +63,25 @@ func looksLikeRepo(p string) bool {
return true return true
} }
func forCI(r *http.Request) string {
u, _, ok := r.BasicAuth()
if ok && u == "gitlab-ci-token" {
return "1"
} else {
return "0"
}
}
func repoPreAuthorizeHandler(myAPI *api.API, handleFunc api.HandleFunc) http.Handler { func repoPreAuthorizeHandler(myAPI *api.API, handleFunc api.HandleFunc) http.Handler {
return myAPI.PreAuthorizeHandler(func(w http.ResponseWriter, r *http.Request, a *api.Response) { return myAPI.PreAuthorizeHandler(func(w http.ResponseWriter, r *http.Request, a *api.Response) {
gitHTTPRequests.WithLabelValues(forCI(r)).Inc()
defer func() {
lw, ok := w.(*helper.LoggingResponseWriter)
if ok {
gitHTTPBytes.WithLabelValues(forCI(r)).Add(float64(lw.Size()))
}
}()
if a.RepoPath == "" { if a.RepoPath == "" {
helper.Fail500(w, r, fmt.Errorf("repoPreAuthorizeHandler: RepoPath empty")) helper.Fail500(w, r, fmt.Errorf("repoPreAuthorizeHandler: RepoPath empty"))
return return
......
...@@ -7,7 +7,6 @@ import ( ...@@ -7,7 +7,6 @@ import (
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
...@@ -28,22 +27,6 @@ var ( ...@@ -28,22 +27,6 @@ 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"},
)
cloneFetchBytes = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gitlab_workhorse_git_clone_fetch_bytes",
Help: "How many Git clone/fetch bytes for CI have been send by gitlab-workhorse, partitioned by CI yes/no status.",
},
[]string{"ci"},
)
) )
func init() { func init() {
...@@ -58,8 +41,6 @@ func SetCustomResponseLogger(writer io.Writer) { ...@@ -58,8 +41,6 @@ func SetCustomResponseLogger(writer io.Writer) {
func registerPrometheusMetrics() { func registerPrometheusMetrics() {
prometheus.MustRegister(sessionsActive) prometheus.MustRegister(sessionsActive)
prometheus.MustRegister(requestsTotal) prometheus.MustRegister(requestsTotal)
prometheus.MustRegister(cloneFetchRequests)
prometheus.MustRegister(cloneFetchBytes)
} }
type LoggingResponseWriter struct { type LoggingResponseWriter struct {
...@@ -107,25 +88,10 @@ func (l *LoggingResponseWriter) Log(r *http.Request) { ...@@ -107,25 +88,10 @@ 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) { func (l *LoggingResponseWriter) Size() int64 {
if l.status == 401 || !strings.Contains(r.RequestURI, "/info/refs?service=git-upload-pack") { return l.written
return
}
u, _, ok := r.BasicAuth()
var forCi string
if ok && u == "gitlab-ci-token" {
forCi = "1"
} else {
forCi = "0"
}
cloneFetchRequests.WithLabelValues(forCi).Inc()
cloneFetchBytes.WithLabelValues(forCi).Add(float64(l.written))
} }
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