summaryrefslogtreecommitdiff
path: root/internal/metrics/metrics.go
blob: c46fb8f65ab256c36ee3de8cd1a5fcb521ae5aab (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
package metrics

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_shell"
	sshdSubsystem = "sshd"
	httpSubsystem = "http"

	httpInFlightRequestsMetricName       = "in_flight_requests"
	httpRequestsTotalMetricName          = "requests_total"
	httpRequestDurationSecondsMetricName = "request_duration_seconds"

	sshdConnectionsInFlightName = "in_flight_connections"
	sshdConnectionDuration      = "connection_duration_seconds"
	sshdHitMaxSessions          = "concurrent_limited_sessions_total"
)

var (
	SshdConnectionDuration = promauto.NewHistogram(
		prometheus.HistogramOpts{
			Namespace: namespace,
			Subsystem: sshdSubsystem,
			Name:      sshdConnectionDuration,
			Help:      "A histogram of latencies for connections to gitlab-shell sshd.",
			Buckets: []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, /* 5m */
			},
		},
	)

	SshdConnectionsInFlight = promauto.NewGauge(
		prometheus.GaugeOpts{
			Namespace: namespace,
			Subsystem: sshdSubsystem,
			Name:      sshdConnectionsInFlightName,
			Help:      "A gauge of connections currently being served by gitlab-shell sshd.",
		},
	)

	SshdHitMaxSessions = promauto.NewCounter(
		prometheus.CounterOpts{
			Namespace: namespace,
			Subsystem: sshdSubsystem,
			Name:      sshdHitMaxSessions,
			Help:      "The number of times the concurrent sessions limit was hit in gitlab-shell sshd.",
		},
	)

	// The metrics and the buckets size are similar to the ones we have for handlers in Labkit
	// When the MR: https://gitlab.com/gitlab-org/labkit/-/merge_requests/150 is merged,
	// these metrics can be refactored out of Gitlab Shell code by using the helper function from Labkit
	httpRequestsTotal = promauto.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: namespace,
			Subsystem: httpSubsystem,
			Name:      httpRequestsTotalMetricName,
			Help:      "A counter for http requests.",
		},
		[]string{"code", "method"},
	)

	httpRequestDurationSeconds = promauto.NewHistogramVec(
		prometheus.HistogramOpts{
			Namespace: namespace,
			Subsystem: httpSubsystem,
			Name:      httpRequestDurationSecondsMetricName,
			Help:      "A histogram of latencies for http requests.",
			Buckets: []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, /* 5m */
			},
		},
		[]string{"code", "method"},
	)

	httpInFlightRequests = promauto.NewGauge(
		prometheus.GaugeOpts{
			Namespace: namespace,
			Subsystem: httpSubsystem,
			Name:      httpInFlightRequestsMetricName,
			Help:      "A gauge of requests currently being performed.",
		},
	)
)

func NewRoundTripper(next http.RoundTripper) promhttp.RoundTripperFunc {
	rt := next

	rt = promhttp.InstrumentRoundTripperCounter(httpRequestsTotal, rt)
	rt = promhttp.InstrumentRoundTripperDuration(httpRequestDurationSeconds, rt)
	return promhttp.InstrumentRoundTripperInFlight(httpInFlightRequests, rt)
}