summaryrefslogtreecommitdiff
path: root/internal/metrics/metrics.go
blob: 713a99a028ebabad41681e494c175e61c76d0617 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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"
	gitalySubsystem = "gitaly"

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

	sshdConnectionsInFlightName               = "in_flight_connections"
	sshdHitMaxSessionsName                    = "concurrent_limited_sessions_total"
	sshdSessionDurationSecondsName            = "session_duration_seconds"
	sshdSessionEstablishedDurationSecondsName = "session_established_duration_seconds"
	sshdCanceledSessionsName                  = "canceled_sessions"

	sliSshdSessionsTotalName       = "gitlab_sli:shell_sshd_sessions:total"
	sliSshdSessionsErrorsTotalName = "gitlab_sli:shell_sshd_sessions:errors_total"

	gitalyConnectionsTotalName = "connections_total"
)

var (
	SshdSessionDuration = promauto.NewHistogram(
		prometheus.HistogramOpts{
			Namespace: namespace,
			Subsystem: sshdSubsystem,
			Name:      sshdSessionDurationSecondsName,
			Help:      "A histogram of latencies for connections to gitlab-shell sshd.",
			Buckets: []float64{
				5.0,  /* 5s */
				30.0, /* 30s */
				60.0, /* 1m */
			},
		},
	)

	SshdSessionEstablishedDuration = promauto.NewHistogram(
		prometheus.HistogramOpts{
			Namespace: namespace,
			Subsystem: sshdSubsystem,
			Name:      sshdSessionEstablishedDurationSecondsName,
			Help:      "A histogram of latencies until session established to gitlab-shell sshd.",
			Buckets: []float64{
				0.5, /* 5ms */
				1.0, /* 1s */
				5.0, /* 5s */
			},
		},
	)

	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:      sshdHitMaxSessionsName,
			Help:      "The number of times the concurrent sessions limit was hit in gitlab-shell sshd.",
		},
	)

	SliSshdSessionsTotal = promauto.NewCounter(
		prometheus.CounterOpts{
			Name: sliSshdSessionsTotalName,
			Help: "Number of SSH sessions that have been established",
		},
	)

	SliSshdSessionsErrorsTotal = promauto.NewCounter(
		prometheus.CounterOpts{
			Name: sliSshdSessionsErrorsTotalName,
			Help: "Number of SSH sessions that have failed",
		},
	)

	GitalyConnectionsTotal = promauto.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: namespace,
			Subsystem: gitalySubsystem,
			Name:      gitalyConnectionsTotalName,
			Help:      "Number of Gitaly connections that have been established",
		},
		[]string{"status"},
	)

	// 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)
}