Welcome to mirror list, hosted at ThFree Co, Russian Federation.

metrics.go « upstream « internal « workhorse - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1a11bdc8b5355dfb9d2f3fca703c02490bea91bc (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package upstream

import (
	"net/http"

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

const (
	namespace     = "gitlab_workhorse"
	httpSubsystem = "http"
)

func secondsDurationBuckets() []float64 {
	return []float64{
		0.005, /* 5ms */
		0.025, /* 25ms */
		0.1,   /* 100ms */
		0.5,   /* 500ms */
		1.0,   /* 1s */
		10.0,  /* 10s */
		30.0,  /* 30s */
		60.0,  /* 1m */
		300.0, /* 10m */
	}
}

func byteSizeBuckets() []float64 {
	return []float64{
		10,
		64,
		256,
		1024,             /* 1kB */
		64 * 1024,        /* 64kB */
		256 * 1024,       /* 256kB */
		1024 * 1024,      /* 1mB */
		64 * 1024 * 1024, /* 64mB */
	}
}

var (
	httpInFlightRequests = promauto.NewGauge(prometheus.GaugeOpts{
		Namespace: namespace,
		Subsystem: httpSubsystem,
		Name:      "in_flight_requests",
		Help:      "A gauge of requests currently being served by workhorse.",
	})

	httpRequestsTotal = promauto.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: namespace,
			Subsystem: httpSubsystem,
			Name:      "requests_total",
			Help:      "A counter for requests to workhorse.",
		},
		[]string{"code", "method", "route"},
	)

	httpRequestDurationSeconds = promauto.NewHistogramVec(
		prometheus.HistogramOpts{
			Namespace: namespace,
			Subsystem: httpSubsystem,
			Name:      "request_duration_seconds",
			Help:      "A histogram of latencies for requests to workhorse.",
			Buckets:   secondsDurationBuckets(),
		},
		[]string{"code", "method", "route"},
	)

	httpRequestSizeBytes = promauto.NewHistogramVec(
		prometheus.HistogramOpts{
			Namespace: namespace,
			Subsystem: httpSubsystem,
			Name:      "request_size_bytes",
			Help:      "A histogram of sizes of requests to workhorse.",
			Buckets:   byteSizeBuckets(),
		},
		[]string{"code", "method", "route"},
	)

	httpResponseSizeBytes = promauto.NewHistogramVec(
		prometheus.HistogramOpts{
			Namespace: namespace,
			Subsystem: httpSubsystem,
			Name:      "response_size_bytes",
			Help:      "A histogram of response sizes for requests to workhorse.",
			Buckets:   byteSizeBuckets(),
		},
		[]string{"code", "method", "route"},
	)

	httpTimeToWriteHeaderSeconds = promauto.NewHistogramVec(
		prometheus.HistogramOpts{
			Namespace: namespace,
			Subsystem: httpSubsystem,
			Name:      "time_to_write_header_seconds",
			Help:      "A histogram of request durations until the response headers are written.",
			Buckets:   secondsDurationBuckets(),
		},
		[]string{"code", "method", "route"},
	)

	httpGeoProxiedRequestsTotal = promauto.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: namespace,
			Subsystem: httpSubsystem,
			Name:      "geo_proxied_requests_total",
			Help:      "A counter for Geo proxied requests through workhorse.",
		},
		[]string{"code", "method", "route"},
	)
)

func instrumentRoute(next http.Handler, method string, regexpStr string) http.Handler {
	handler := next

	handler = promhttp.InstrumentHandlerCounter(httpRequestsTotal.MustCurryWith(map[string]string{"route": regexpStr}), handler)
	handler = promhttp.InstrumentHandlerDuration(httpRequestDurationSeconds.MustCurryWith(map[string]string{"route": regexpStr}), handler)
	handler = promhttp.InstrumentHandlerInFlight(httpInFlightRequests, handler)
	handler = promhttp.InstrumentHandlerRequestSize(httpRequestSizeBytes.MustCurryWith(map[string]string{"route": regexpStr}), handler)
	handler = promhttp.InstrumentHandlerResponseSize(httpResponseSizeBytes.MustCurryWith(map[string]string{"route": regexpStr}), handler)
	handler = promhttp.InstrumentHandlerTimeToWriteHeader(httpTimeToWriteHeaderSeconds.MustCurryWith(map[string]string{"route": regexpStr}), handler)

	return handler
}

func instrumentGeoProxyRoute(next http.Handler, method string, regexpStr string) http.Handler {
	return promhttp.InstrumentHandlerCounter(httpGeoProxiedRequestsTotal.MustCurryWith(map[string]string{"route": regexpStr}), next)
}