-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
352 lines (297 loc) · 11.3 KB
/
metrics.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package minke
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
)
var metricsProvider MetricsProvider
func SetProvider(p MetricsProvider) {
workqueue.SetProvider(p)
cache.SetReflectorMetricsProvider(p)
metricsProvider = p
}
type SummaryMetric interface {
Observe(float64)
}
type GaugeMetric interface {
Set(float64)
}
type CounterMetric interface {
Inc()
}
type MetricsProvider interface {
NewListsMetric(name string) cache.CounterMetric
NewListDurationMetric(name string) cache.SummaryMetric
NewItemsInListMetric(name string) cache.SummaryMetric
NewWatchesMetric(name string) cache.CounterMetric
NewShortWatchesMetric(name string) cache.CounterMetric
NewWatchDurationMetric(name string) cache.SummaryMetric
NewItemsInWatchMetric(name string) cache.SummaryMetric
NewLastResourceVersionMetric(name string) cache.GaugeMetric
NewDepthMetric(name string) workqueue.GaugeMetric
NewAddsMetric(name string) workqueue.CounterMetric
NewLatencyMetric(name string) workqueue.HistogramMetric
NewLongestRunningProcessorSecondsMetric(name string) workqueue.SettableGaugeMetric
NewUnfinishedWorkSecondsMetric(name string) workqueue.SettableGaugeMetric
NewWorkDurationMetric(name string) workqueue.HistogramMetric
NewRetriesMetric(name string) workqueue.CounterMetric
NewHTTPTransportMetrics(upstream http.RoundTripper) http.RoundTripper
NewHTTPServerMetrics(upstream http.Handler) http.Handler
}
type prometheusMetricsProvider struct {
registry *prometheus.Registry
listsTotal *prometheus.CounterVec
listsDuration *prometheus.SummaryVec
itemsPerList *prometheus.SummaryVec
watchesTotal *prometheus.CounterVec
shortWatchesTotal *prometheus.CounterVec
watchDuration *prometheus.SummaryVec
itemsPerWatch *prometheus.SummaryVec
listWatchError *prometheus.GaugeVec
}
func NewPrometheusMetrics(r *prometheus.Registry) *prometheusMetricsProvider {
listsTotal := prometheus.NewCounterVec(prometheus.CounterOpts{
Subsystem: reflectorSubsystem,
Name: "lists_total",
Help: "Total number of API lists done by the reflectors",
}, []string{"name"})
listsDuration := prometheus.NewSummaryVec(prometheus.SummaryOpts{
Subsystem: reflectorSubsystem,
Name: "list_duration_seconds",
Help: "How long an API list takes to return and decode for the reflectors",
}, []string{"name"})
itemsPerList := prometheus.NewSummaryVec(prometheus.SummaryOpts{
Subsystem: reflectorSubsystem,
Name: "items_per_list",
Help: "How many items an API list returns to the reflectors",
}, []string{"name"})
watchesTotal := prometheus.NewCounterVec(prometheus.CounterOpts{
Subsystem: reflectorSubsystem,
Name: "watches_total",
Help: "Total number of API watches done by the reflectors",
}, []string{"name"})
shortWatchesTotal := prometheus.NewCounterVec(prometheus.CounterOpts{
Subsystem: reflectorSubsystem,
Name: "short_watches_total",
Help: "Total number of short API watches done by the reflectors",
}, []string{"name"})
watchDuration := prometheus.NewSummaryVec(prometheus.SummaryOpts{
Subsystem: reflectorSubsystem,
Name: "watch_duration_seconds",
Help: "How long an API watch takes to return and decode for the reflectors",
}, []string{"name"})
itemsPerWatch := prometheus.NewSummaryVec(prometheus.SummaryOpts{
Subsystem: reflectorSubsystem,
Name: "items_per_watch",
Help: "How many items an API watch returns to the reflectors",
}, []string{"name"})
listWatchError := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Subsystem: reflectorSubsystem,
Name: "list_watch_error",
Help: "Whether or not the reflector received an error on its last list or watch attempt",
}, []string{"name"})
p := &prometheusMetricsProvider{
registry: r,
listsTotal: listsTotal,
listsDuration: listsDuration,
itemsPerList: itemsPerList,
watchesTotal: watchesTotal,
shortWatchesTotal: shortWatchesTotal,
watchDuration: watchDuration,
itemsPerWatch: itemsPerWatch,
listWatchError: listWatchError,
}
p.registry.MustRegister(listsTotal)
p.registry.MustRegister(listsDuration)
p.registry.MustRegister(itemsPerList)
p.registry.MustRegister(watchesTotal)
p.registry.MustRegister(shortWatchesTotal)
p.registry.MustRegister(watchDuration)
p.registry.MustRegister(itemsPerWatch)
p.registry.MustRegister(listWatchError)
return p
}
func (p *prometheusMetricsProvider) NewDepthMetric(name string) workqueue.GaugeMetric {
depth := prometheus.NewGauge(prometheus.GaugeOpts{
Subsystem: "workqueue",
Name: "depth",
Help: "Current depth of workqueue",
ConstLabels: prometheus.Labels{"name": name},
})
p.registry.MustRegister(depth)
return depth
}
func (p *prometheusMetricsProvider) NewAddsMetric(name string) workqueue.CounterMetric {
adds := prometheus.NewCounter(prometheus.CounterOpts{
Subsystem: "workqueue",
Name: "adds",
Help: "Total number of adds handled by workqueue",
ConstLabels: prometheus.Labels{"name": name},
})
p.registry.MustRegister(adds)
return adds
}
func (p *prometheusMetricsProvider) NewLatencyMetric(name string) workqueue.HistogramMetric {
latency := prometheus.NewHistogram(prometheus.HistogramOpts{
Subsystem: "workqueue",
Name: "queue_latency",
Help: "How long an item stays in workqueue before being requested.",
ConstLabels: prometheus.Labels{"name": name},
})
p.registry.MustRegister(latency)
return latency
}
func (p *prometheusMetricsProvider) NewWorkDurationMetric(name string) workqueue.HistogramMetric {
workDuration := prometheus.NewHistogram(prometheus.HistogramOpts{
Subsystem: "workqueue",
Name: "work_duration",
Help: "How long processing an item from workqueue takes.",
ConstLabels: prometheus.Labels{"name": name},
})
p.registry.MustRegister(workDuration)
return workDuration
}
func (p *prometheusMetricsProvider) NewRetriesMetric(name string) workqueue.CounterMetric {
retries := prometheus.NewCounter(prometheus.CounterOpts{
Subsystem: "workqueue",
Name: "retries",
Help: "Total number of retries handled by workqueue",
ConstLabels: prometheus.Labels{"name": name},
})
p.registry.MustRegister(retries)
return retries
}
const reflectorSubsystem = "reflector"
func (p *prometheusMetricsProvider) NewListsMetric(name string) cache.CounterMetric {
return p.listsTotal.WithLabelValues(name)
}
// use summary to get averages and percentiles
func (p *prometheusMetricsProvider) NewListDurationMetric(name string) cache.SummaryMetric {
return p.listsDuration.WithLabelValues(name)
}
// use summary to get averages and percentiles
func (p *prometheusMetricsProvider) NewItemsInListMetric(name string) cache.SummaryMetric {
return p.itemsPerList.WithLabelValues(name)
}
func (p *prometheusMetricsProvider) NewWatchesMetric(name string) cache.CounterMetric {
return p.watchesTotal.WithLabelValues(name)
}
func (p *prometheusMetricsProvider) NewShortWatchesMetric(name string) cache.CounterMetric {
return p.shortWatchesTotal.WithLabelValues(name)
}
// use summary to get averages and percentiles
func (p *prometheusMetricsProvider) NewWatchDurationMetric(name string) cache.SummaryMetric {
return p.watchDuration.WithLabelValues(name)
}
// use summary to get averages and percentiles
func (p *prometheusMetricsProvider) NewItemsInWatchMetric(name string) cache.SummaryMetric {
return p.itemsPerWatch.WithLabelValues(name)
}
func (p *prometheusMetricsProvider) NewLastResourceVersionMetric(name string) cache.GaugeMetric {
rv := prometheus.NewGauge(prometheus.GaugeOpts{
Subsystem: reflectorSubsystem,
Name: "last_resource_version",
Help: "last resource version seen for the reflectors",
ConstLabels: prometheus.Labels{"name": name},
})
p.registry.MustRegister(rv)
return rv
}
func (p *prometheusMetricsProvider) NewLongestRunningProcessorSecondsMetric(name string) workqueue.SettableGaugeMetric {
rv := prometheus.NewGauge(prometheus.GaugeOpts{
Subsystem: "workqueue",
Name: "longest_running_processor",
Help: "How many seconds has the longest running processor for workqueue been running.",
ConstLabels: prometheus.Labels{"name": name},
})
p.registry.MustRegister(rv)
return rv
}
func (p *prometheusMetricsProvider) NewUnfinishedWorkSecondsMetric(name string) workqueue.SettableGaugeMetric {
rv := prometheus.NewGauge(prometheus.GaugeOpts{
Subsystem: "workqueue",
Name: "unfinished_work_seconds",
Help: "How many seconds of work has done that " +
"is in progress and hasn't been observed by work_duration. Large " +
"values indicate stuck threads. One can deduce the number of stuck " +
"threads by observing the rate at which this increases.",
ConstLabels: prometheus.Labels{"name": name},
})
p.registry.MustRegister(rv)
return rv
}
func (p *prometheusMetricsProvider) NewListWatchErrorMetric(name string) cache.GaugeMetric {
return p.listWatchError.WithLabelValues(name)
}
func (p *prometheusMetricsProvider) NewHTTPTransportMetrics(upstream http.RoundTripper) http.RoundTripper {
inFlightGauge := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "http_client_inflight_requests",
Help: "A gauge of in-flight requests for the wrapped client.",
})
tlsLatencyVec := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "http_client_tls_duration_seconds",
Help: "Trace tls latency histogram.",
Buckets: []float64{.05, .1, .25, .5},
},
[]string{"event"},
)
histVec := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "http_client_request_duration_seconds",
Help: "A histogram of request latencies.",
Buckets: prometheus.DefBuckets,
},
[]string{"code"},
)
p.registry.MustRegister(tlsLatencyVec, histVec, inFlightGauge)
trace := &promhttp.InstrumentTrace{
TLSHandshakeStart: func(t float64) {
tlsLatencyVec.WithLabelValues("tls_handshake_start")
},
TLSHandshakeDone: func(t float64) {
tlsLatencyVec.WithLabelValues("tls_handshake_done")
},
}
// Wrap the default RoundTripper with middleware.
roundTripper := promhttp.InstrumentRoundTripperInFlight(inFlightGauge,
promhttp.InstrumentRoundTripperTrace(trace,
promhttp.InstrumentRoundTripperDuration(histVec, upstream),
),
)
return roundTripper
}
func (p *prometheusMetricsProvider) NewHTTPServerMetrics(upstream http.Handler) http.Handler {
inFlightGauge := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "http_server_inflight_requests",
Help: "A gauge of requests currently being served by the wrapped handler.",
})
counter := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "http_server_requests_total",
Help: "A counter for requests to the wrapped handler.",
},
[]string{"code"},
)
histogramOpts := prometheus.HistogramOpts{
Name: "http_server_request_duration_seconds",
Help: "A histogram of latencies for requests.",
Buckets: []float64{.25, .5, 1, 2.5, 5, 10},
}
reqDurVec := prometheus.NewHistogramVec(
histogramOpts,
[]string{"code"},
)
// Register all of the metrics in the standard registry.
p.registry.MustRegister(inFlightGauge, counter, reqDurVec)
chain := promhttp.InstrumentHandlerInFlight(inFlightGauge,
promhttp.InstrumentHandlerCounter(counter,
promhttp.InstrumentHandlerDuration(reqDurVec,
upstream,
),
),
)
return chain
}