summaryrefslogtreecommitdiff
path: root/workhorse/internal/git/responsewriter.go
blob: c4d4ac252d48f0f59c22df37b8b3bc59346121ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package git

import (
	"net/http"
	"strconv"

	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promauto"

	"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
)

const (
	directionIn  = "in"
	directionOut = "out"
)

var (
	gitHTTPSessionsActive = promauto.NewGauge(prometheus.GaugeOpts{
		Name: "gitlab_workhorse_git_http_sessions_active",
		Help: "Number of Git HTTP request-response cycles currently being handled by gitlab-workhorse.",
	})

	gitHTTPRequests = promauto.NewCounterVec(
		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"},
	)

	gitHTTPBytes = promauto.NewCounterVec(
		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"
}