2020-12-02 10:09:37 -05:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2020-12-08 22:09:24 -05:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
2020-12-02 10:09:37 -05:00
|
|
|
|
|
|
|
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
directionIn = "in"
|
|
|
|
directionOut = "out"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-12-08 22:09:24 -05:00
|
|
|
gitHTTPSessionsActive = promauto.NewGauge(prometheus.GaugeOpts{
|
2020-12-02 10:09:37 -05:00
|
|
|
Name: "gitlab_workhorse_git_http_sessions_active",
|
|
|
|
Help: "Number of Git HTTP request-response cycles currently being handled by gitlab-workhorse.",
|
|
|
|
})
|
|
|
|
|
2020-12-08 22:09:24 -05:00
|
|
|
gitHTTPRequests = promauto.NewCounterVec(
|
2020-12-02 10:09:37 -05:00
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: "gitlab_workhorse_git_http_requests",
|
|
|
|
Help: "How many Git HTTP requests have been processed by gitlab-workhorse, partitioned by request type and agent.",
|
|
|
|
},
|
|
|
|
[]string{"method", "code", "service", "agent"},
|
|
|
|
)
|
|
|
|
|
2020-12-08 22:09:24 -05:00
|
|
|
gitHTTPBytes = promauto.NewCounterVec(
|
2020-12-02 10:09:37 -05:00
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: "gitlab_workhorse_git_http_bytes",
|
|
|
|
Help: "How many Git HTTP bytes have been sent by gitlab-workhorse, partitioned by request type, agent and direction.",
|
|
|
|
},
|
|
|
|
[]string{"method", "code", "service", "agent", "direction"},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
type HttpResponseWriter struct {
|
|
|
|
helper.CountingResponseWriter
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHttpResponseWriter(rw http.ResponseWriter) *HttpResponseWriter {
|
|
|
|
gitHTTPSessionsActive.Inc()
|
|
|
|
return &HttpResponseWriter{
|
|
|
|
CountingResponseWriter: helper.NewCountingResponseWriter(rw),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *HttpResponseWriter) Log(r *http.Request, writtenIn int64) {
|
|
|
|
service := getService(r)
|
|
|
|
agent := getRequestAgent(r)
|
|
|
|
|
|
|
|
gitHTTPSessionsActive.Dec()
|
|
|
|
gitHTTPRequests.WithLabelValues(r.Method, strconv.Itoa(w.Status()), service, agent).Inc()
|
|
|
|
gitHTTPBytes.WithLabelValues(r.Method, strconv.Itoa(w.Status()), service, agent, directionIn).
|
|
|
|
Add(float64(writtenIn))
|
|
|
|
gitHTTPBytes.WithLabelValues(r.Method, strconv.Itoa(w.Status()), service, agent, directionOut).
|
|
|
|
Add(float64(w.Count()))
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRequestAgent(r *http.Request) string {
|
|
|
|
u, _, ok := r.BasicAuth()
|
|
|
|
if !ok {
|
|
|
|
return "anonymous"
|
|
|
|
}
|
|
|
|
|
|
|
|
if u == "gitlab-ci-token" {
|
|
|
|
return "gitlab-ci"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "logged"
|
|
|
|
}
|