forked from gravitational/teleport
/
prometheus.go
309 lines (284 loc) · 9.9 KB
/
prometheus.go
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* Teleport
* Copyright (C) 2023 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package proxy
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"github.com/gravitational/teleport"
tracehttp "github.com/gravitational/teleport/api/observability/tracing/http"
"github.com/gravitational/teleport/lib/observability/metrics"
)
const (
// kubernetesSubsystem is used to prefix Prometheus metrics for this
// subsystem.
// See https://prometheus.io/docs/practices/naming/#subsystem-name
kubernetesSubsystem = "kubernetes"
)
func init() {
metrics.RegisterPrometheusCollectors(
clientRequestCounter,
clientTLSLatencyVec,
clientRequestDurationHistVec,
clientInFlightGauge,
clietGotConnLatencyVec,
clientFirstByteLatencyVec,
serverInFlightGauge,
serverRequestCounter,
serverRequestDurationHist,
serverResponseSizeHist,
execSessionsInFlightGauge,
execSessionsRequestCounter,
portforwardSessionsInFlightGauge,
portforwardRequestCounter,
joinSessionsInFlightGauge,
joinSessionsRequestCounter,
)
}
// The following section defines Prometheus metrics for the clients used by
// Teleport proxy to connect to the Teleport Kubernetes service and by the
// Teleport Kubernetes service to connect to the Kubernetes cluster.
var (
clientInFlightGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: teleport.MetricNamespace,
Subsystem: kubernetesSubsystem,
Name: "client_in_flight_requests",
Help: "In-flight requests waiting for the upstream response.",
},
[]string{"component"},
)
clientRequestCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: teleport.MetricNamespace,
Subsystem: kubernetesSubsystem,
Name: "client_requests_total",
Help: "Total number of requests sent to the upstream teleport proxy, kube_service or Kubernetes Cluster servers.",
},
[]string{"component", "code", "method"},
)
clientTLSLatencyVec = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: teleport.MetricNamespace,
Subsystem: kubernetesSubsystem,
Name: "client_tls_duration_seconds",
Help: "Latency distribution of TLS handshakes.",
Buckets: prometheus.DefBuckets,
},
[]string{"component", "event"},
)
clietGotConnLatencyVec = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: teleport.MetricNamespace,
Subsystem: kubernetesSubsystem,
Name: "client_got_conn_duration_seconds",
Help: "A histogram of latency to dial to the upstream server.",
Buckets: prometheus.DefBuckets,
},
[]string{"component"},
)
clientFirstByteLatencyVec = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: teleport.MetricNamespace,
Subsystem: kubernetesSubsystem,
Name: "client_first_byte_response_duration_seconds",
Help: "Teleport Kubernetes Service | Latency distribution of time to receive the first response byte from the upstream server.",
Buckets: prometheus.DefBuckets,
},
[]string{"component"},
)
clientRequestDurationHistVec = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: teleport.MetricNamespace,
Subsystem: kubernetesSubsystem,
Name: "client_request_duration_seconds",
Help: "Latency distribution of the upstream request time.",
Buckets: prometheus.DefBuckets,
},
[]string{"component"},
)
)
// instrumentedRoundtripper instruments the provided RoundTripper with
// Prometheus metrics and OpenTelemetry tracing.
func instrumentedRoundtripper(component string, tr http.RoundTripper) http.RoundTripper {
// Define functions for the available httptrace.ClientTrace hook
// functions that we want to instrument.
httpTrace := &promhttp.InstrumentTrace{
GotConn: func(t float64) {
clietGotConnLatencyVec.WithLabelValues(component).Observe(t)
},
GotFirstResponseByte: func(t float64) {
clientFirstByteLatencyVec.WithLabelValues(component).Observe(t)
},
TLSHandshakeStart: func(t float64) {
clientTLSLatencyVec.WithLabelValues(component, "tls_handshake_start").Observe(t)
},
TLSHandshakeDone: func(t float64) {
clientTLSLatencyVec.WithLabelValues(component, "tls_handshake_done").Observe(t)
},
}
curryWith := prometheus.Labels{"component": component}
return tracehttp.NewTransportWithInner(
promhttp.InstrumentRoundTripperInFlight(
clientInFlightGauge.WithLabelValues(component),
promhttp.InstrumentRoundTripperCounter(
clientRequestCounter.MustCurryWith(curryWith),
promhttp.InstrumentRoundTripperTrace(
httpTrace,
promhttp.InstrumentRoundTripperDuration(clientRequestDurationHistVec.MustCurryWith(curryWith), tr),
),
),
),
// Pass the original RoundTripper to the inner transport so that it can
// be used to close idle connections because promhttp roundtrippers don't
// implement CloseIdleConnections.
tr,
)
}
// The following section defines Prometheus metrics for the HTTP server used by
// the Teleport Kubernetes Proxy and the Teleport Kubernetes service.
var (
serverInFlightGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: teleport.MetricNamespace,
Subsystem: kubernetesSubsystem,
Name: "server_in_flight_requests",
Help: "In-flight requests currently handled by the server.",
},
[]string{"component"},
)
serverRequestCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: teleport.MetricNamespace,
Subsystem: kubernetesSubsystem,
Name: "server_api_requests_total",
Help: "Total number of requests handled by the server.",
},
[]string{"component", "code", "method"},
)
serverRequestDurationHist = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: teleport.MetricNamespace,
Subsystem: kubernetesSubsystem,
Name: "server_request_duration_seconds",
Help: "Latency distribution of the total request time.",
Buckets: prometheus.DefBuckets,
},
[]string{"component", "method"},
)
serverResponseSizeHist = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: teleport.MetricNamespace,
Subsystem: kubernetesSubsystem,
Name: "server_response_size_bytes",
Help: "Distribution of the response size.",
// The following exponential buckets are equivalent to the following:
// [50B 150B 450B 1.32KB 3.96KB 11.87KB 35.6KB 106.79KB 320.36KB 961.08KB 2.82MB 8.45MB 25.34MB]
Buckets: prometheus.ExponentialBuckets(50, 3, 13),
},
[]string{"component"},
)
execSessionsInFlightGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: teleport.MetricNamespace,
Subsystem: kubernetesSubsystem,
Name: "server_exec_in_flight_sessions",
Help: "Number of active kubectl exec sessions.",
},
[]string{"component"},
)
execSessionsRequestCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: teleport.MetricNamespace,
Subsystem: kubernetesSubsystem,
Name: "server_exec_sessions_total",
Help: "Total number of kubectl exec sessions. ",
},
[]string{"component"},
)
portforwardSessionsInFlightGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: teleport.MetricNamespace,
Subsystem: kubernetesSubsystem,
Name: "server_portforward_in_flight_sessions",
Help: " Number of active kubectl portforward sessions.",
},
[]string{"component"},
)
portforwardRequestCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: teleport.MetricNamespace,
Subsystem: kubernetesSubsystem,
Name: "server_portforward_sessions_total",
Help: "Number of active kubectl portforward sessions.",
},
[]string{"component"},
)
joinSessionsInFlightGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: teleport.MetricNamespace,
Subsystem: kubernetesSubsystem,
Name: "server_join_in_flight_sessions",
Help: "Number of active joining sessions,",
},
[]string{"component"},
)
joinSessionsRequestCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: teleport.MetricNamespace,
Subsystem: kubernetesSubsystem,
Name: "server_join_sessions_total",
Help: "Total number of joining sessions.",
},
[]string{"component"},
)
)
// instrumentHTTPHandler instruments the HTTP handler with OpenTelemetry and
// Prometheus metrics.
func instrumentHTTPHandler(component string, handler http.Handler) http.Handler {
return otelhttp.NewHandler(
instrumentHTTPHandlerWithPrometheus(component, handler),
component,
otelhttp.WithMessageEvents(otelhttp.ReadEvents, otelhttp.WriteEvents),
)
}
// instrumentHTTPHandlerWithPrometheus instruments the HTTP handler with
// Prometheus metrics.
func instrumentHTTPHandlerWithPrometheus(component string, handler http.Handler) http.Handler {
curryWith := prometheus.Labels{"component": component}
return promhttp.InstrumentHandlerInFlight(
serverInFlightGauge.WithLabelValues(component),
promhttp.InstrumentHandlerDuration(
serverRequestDurationHist.MustCurryWith(
curryWith,
),
promhttp.InstrumentHandlerCounter(
serverRequestCounter.MustCurryWith(
curryWith,
),
promhttp.InstrumentHandlerResponseSize(
serverResponseSizeHist.MustCurryWith(
curryWith,
),
handler,
),
),
),
)
}