generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 228
/
metrics.go
391 lines (334 loc) · 14.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package metrics
import (
"time"
"github.com/prometheus/client_golang/prometheus"
"sigs.k8s.io/controller-runtime/pkg/metrics"
kueue "sigs.k8s.io/kueue/apis/kueue/v1beta1"
"sigs.k8s.io/kueue/pkg/constants"
"sigs.k8s.io/kueue/pkg/features"
)
type AdmissionResult string
type ClusterQueueStatus string
const (
AdmissionResultSuccess AdmissionResult = "success"
AdmissionResultInadmissible AdmissionResult = "inadmissible"
PendingStatusActive = "active"
PendingStatusInadmissible = "inadmissible"
// CQStatusPending means the ClusterQueue is accepted but not yet active,
// this can be because of:
// - a missing ResourceFlavor referenced by the ClusterQueue
// - a missing or inactive AdmissionCheck referenced by the ClusterQueue
// - the ClusterQueue is stopped
// In this state, the ClusterQueue can't admit new workloads and its quota can't be borrowed
// by other active ClusterQueues in the cohort.
CQStatusPending ClusterQueueStatus = "pending"
// CQStatusActive means the ClusterQueue can admit new workloads and its quota
// can be borrowed by other ClusterQueues in the cohort.
CQStatusActive ClusterQueueStatus = "active"
// CQStatusTerminating means the clusterQueue is in pending deletion.
CQStatusTerminating ClusterQueueStatus = "terminating"
)
var (
CQStatuses = []ClusterQueueStatus{CQStatusPending, CQStatusActive, CQStatusTerminating}
AdmissionAttemptsTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: constants.KueueName,
Name: "admission_attempts_total",
Help: `The total number of attempts to admit workloads.
Each admission attempt might try to admit more than one workload.
The label 'result' can have the following values:
- 'success' means that at least one workload was admitted.,
- 'inadmissible' means that no workload was admitted.`,
}, []string{"result"},
)
admissionAttemptDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: constants.KueueName,
Name: "admission_attempt_duration_seconds",
Help: `The latency of an admission attempt.
The label 'result' can have the following values:
- 'success' means that at least one workload was admitted.,
- 'inadmissible' means that no workload was admitted.`,
}, []string{"result"},
)
// Metrics tied to the queue system.
PendingWorkloads = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: constants.KueueName,
Name: "pending_workloads",
Help: `The number of pending workloads, per 'cluster_queue' and 'status'.
'status' can have the following values:
- "active" means that the workloads are in the admission queue.
- "inadmissible" means there was a failed admission attempt for these workloads and they won't be retried until cluster conditions, which could make this workload admissible, change`,
}, []string{"cluster_queue", "status"},
)
QuotaReservedWorkloadsTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: constants.KueueName,
Name: "quota_reserved_workloads_total",
Help: "The total number of quota reserved workloads per 'cluster_queue'",
}, []string{"cluster_queue"},
)
quotaReservedWaitTime = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: constants.KueueName,
Name: "quota_reserved_wait_time_seconds",
Help: "The time between a workload was created or requeued until it got quota reservation, per 'cluster_queue'",
Buckets: generateExponentialBuckets(14),
}, []string{"cluster_queue"},
)
AdmittedWorkloadsTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: constants.KueueName,
Name: "admitted_workloads_total",
Help: "The total number of admitted workloads per 'cluster_queue'",
}, []string{"cluster_queue"},
)
admissionWaitTime = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: constants.KueueName,
Name: "admission_wait_time_seconds",
Help: "The time between a workload was created or requeued until admission, per 'cluster_queue'",
Buckets: generateExponentialBuckets(14),
}, []string{"cluster_queue"},
)
admissionChecksWaitTime = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: constants.KueueName,
Name: "admission_checks_wait_time_seconds",
Help: "The time from when a workload got the quota reservation until admission, per 'cluster_queue'",
Buckets: generateExponentialBuckets(14),
}, []string{"cluster_queue"},
)
EvictedWorkloadsTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: constants.KueueName,
Name: "evicted_workloads_total",
Help: `The number of evicted workloads per 'cluster_queue',
The label 'reason' can have the following values:
- "Preempted" means that the workload was evicted in order to free resources for a workload with a higher priority or reclamation of nominal quota.
- "PodsReadyTimeout" means that the eviction took place due to a PodsReady timeout.
- "AdmissionCheck" means that the workload was evicted because at least one admission check transitioned to False.
- "ClusterQueueStopped" means that the workload was evicted because the ClusterQueue is stopped.
- "InactiveWorkload" means that the workload was evicted because spec.active is set to false`,
}, []string{"cluster_queue", "reason"},
)
// Metrics tied to the cache.
ReservingActiveWorkloads = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: constants.KueueName,
Name: "reserving_active_workloads",
Help: "The number of Workloads that are reserving quota, per 'cluster_queue'",
}, []string{"cluster_queue"},
)
AdmittedActiveWorkloads = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: constants.KueueName,
Name: "admitted_active_workloads",
Help: "The number of admitted Workloads that are active (unsuspended and not finished), per 'cluster_queue'",
}, []string{"cluster_queue"},
)
ClusterQueueByStatus = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: constants.KueueName,
Name: "cluster_queue_status",
Help: `Reports 'cluster_queue' with its 'status' (with possible values 'pending', 'active' or 'terminated').
For a ClusterQueue, the metric only reports a value of 1 for one of the statuses.`,
}, []string{"cluster_queue", "status"},
)
// Optional cluster queue metrics
ClusterQueueResourceReservations = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: constants.KueueName,
Name: "cluster_queue_resource_reservation",
Help: `Reports the cluster_queue's total resource reservation within all the flavors`,
}, []string{"cohort", "cluster_queue", "flavor", "resource"},
)
ClusterQueueResourceUsage = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: constants.KueueName,
Name: "cluster_queue_resource_usage",
Help: `Reports the cluster_queue's total resource usage within all the flavors`,
}, []string{"cohort", "cluster_queue", "flavor", "resource"},
)
ClusterQueueResourceNominalQuota = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: constants.KueueName,
Name: "cluster_queue_nominal_quota",
Help: `Reports the cluster_queue's resource nominal quota within all the flavors`,
}, []string{"cohort", "cluster_queue", "flavor", "resource"},
)
ClusterQueueResourceBorrowingLimit = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: constants.KueueName,
Name: "cluster_queue_borrowing_limit",
Help: `Reports the cluster_queue's resource borrowing limit within all the flavors`,
}, []string{"cohort", "cluster_queue", "flavor", "resource"},
)
ClusterQueueResourceLendingLimit = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: constants.KueueName,
Name: "cluster_queue_lending_limit",
Help: `Reports the cluster_queue's resource lending limit within all the flavors`,
}, []string{"cohort", "cluster_queue", "flavor", "resource"},
)
ClusterQueueWeightedShare = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: constants.KueueName,
Name: "cluster_queue_weighted_share",
Help: `Reports a value that representing the maximum of the ratios of usage above nominal
quota to the lendable resources in the cohort, among all the resources provided by
the ClusterQueue, and divided by the weight.
If zero, it means that the usage of the ClusterQueue is below the nominal quota.
If the ClusterQueue has a weight of zero, this will return 9223372036854775807,
the maximum possible share value.`,
}, []string{"cluster_queue"},
)
)
func generateExponentialBuckets(count int) []float64 {
return append([]float64{1}, prometheus.ExponentialBuckets(2.5, 2, count-1)...)
}
func AdmissionAttempt(result AdmissionResult, duration time.Duration) {
AdmissionAttemptsTotal.WithLabelValues(string(result)).Inc()
admissionAttemptDuration.WithLabelValues(string(result)).Observe(duration.Seconds())
}
func QuotaReservedWorkload(cqName kueue.ClusterQueueReference, waitTime time.Duration) {
QuotaReservedWorkloadsTotal.WithLabelValues(string(cqName)).Inc()
quotaReservedWaitTime.WithLabelValues(string(cqName)).Observe(waitTime.Seconds())
}
func AdmittedWorkload(cqName kueue.ClusterQueueReference, waitTime time.Duration) {
AdmittedWorkloadsTotal.WithLabelValues(string(cqName)).Inc()
admissionWaitTime.WithLabelValues(string(cqName)).Observe(waitTime.Seconds())
}
func AdmissionChecksWaitTime(cqName kueue.ClusterQueueReference, waitTime time.Duration) {
admissionChecksWaitTime.WithLabelValues(string(cqName)).Observe(waitTime.Seconds())
}
func ReportPendingWorkloads(cqName string, active, inadmissible int) {
PendingWorkloads.WithLabelValues(cqName, PendingStatusActive).Set(float64(active))
PendingWorkloads.WithLabelValues(cqName, PendingStatusInadmissible).Set(float64(inadmissible))
}
func ReportEvictedWorkloads(cqName, reason string) {
EvictedWorkloadsTotal.WithLabelValues(cqName, reason).Inc()
}
func ClearQueueSystemMetrics(cqName string) {
PendingWorkloads.DeleteLabelValues(cqName, PendingStatusActive)
PendingWorkloads.DeleteLabelValues(cqName, PendingStatusInadmissible)
QuotaReservedWorkloadsTotal.DeleteLabelValues(cqName)
quotaReservedWaitTime.DeleteLabelValues(cqName)
AdmittedWorkloadsTotal.DeleteLabelValues(cqName)
admissionWaitTime.DeleteLabelValues(cqName)
admissionChecksWaitTime.DeleteLabelValues(cqName)
EvictedWorkloadsTotal.DeletePartialMatch(prometheus.Labels{"cluster_queue": cqName})
}
func ReportClusterQueueStatus(cqName string, cqStatus ClusterQueueStatus) {
for _, status := range CQStatuses {
var v float64
if status == cqStatus {
v = 1
}
ClusterQueueByStatus.WithLabelValues(cqName, string(status)).Set(v)
}
}
func ClearCacheMetrics(cqName string) {
ReservingActiveWorkloads.DeleteLabelValues(cqName)
AdmittedActiveWorkloads.DeleteLabelValues(cqName)
for _, status := range CQStatuses {
ClusterQueueByStatus.DeleteLabelValues(cqName, string(status))
}
}
func ReportClusterQueueQuotas(cohort, queue, flavor, resource string, nominal, borrowing, lending float64) {
ClusterQueueResourceNominalQuota.WithLabelValues(cohort, queue, flavor, resource).Set(nominal)
ClusterQueueResourceBorrowingLimit.WithLabelValues(cohort, queue, flavor, resource).Set(borrowing)
if features.Enabled(features.LendingLimit) {
ClusterQueueResourceLendingLimit.WithLabelValues(cohort, queue, flavor, resource).Set(lending)
}
}
func ReportClusterQueueResourceReservations(cohort, queue, flavor, resource string, usage float64) {
ClusterQueueResourceReservations.WithLabelValues(cohort, queue, flavor, resource).Set(usage)
}
func ReportClusterQueueResourceUsage(cohort, queue, flavor, resource string, usage float64) {
ClusterQueueResourceUsage.WithLabelValues(cohort, queue, flavor, resource).Set(usage)
}
func ReportClusterQueueWeightedShare(cq string, weightedShare int64) {
ClusterQueueWeightedShare.WithLabelValues(cq).Set(float64(weightedShare))
}
func ClearClusterQueueResourceMetrics(cqName string) {
lbls := prometheus.Labels{
"cluster_queue": cqName,
}
ClusterQueueResourceNominalQuota.DeletePartialMatch(lbls)
ClusterQueueResourceBorrowingLimit.DeletePartialMatch(lbls)
if features.Enabled(features.LendingLimit) {
ClusterQueueResourceLendingLimit.DeletePartialMatch(lbls)
}
ClusterQueueResourceUsage.DeletePartialMatch(lbls)
ClusterQueueResourceReservations.DeletePartialMatch(lbls)
}
func ClearClusterQueueResourceQuotas(cqName, flavor, resource string) {
lbls := prometheus.Labels{
"cluster_queue": cqName,
"flavor": flavor,
}
if len(resource) != 0 {
lbls["resource"] = resource
}
ClusterQueueResourceNominalQuota.DeletePartialMatch(lbls)
ClusterQueueResourceBorrowingLimit.DeletePartialMatch(lbls)
if features.Enabled(features.LendingLimit) {
ClusterQueueResourceLendingLimit.DeletePartialMatch(lbls)
}
}
func ClearClusterQueueResourceUsage(cqName, flavor, resource string) {
lbls := prometheus.Labels{
"cluster_queue": cqName,
"flavor": flavor,
}
if len(resource) != 0 {
lbls["resource"] = resource
}
ClusterQueueResourceUsage.DeletePartialMatch(lbls)
}
func ClearClusterQueueResourceReservations(cqName, flavor, resource string) {
lbls := prometheus.Labels{
"cluster_queue": cqName,
"flavor": flavor,
}
if len(resource) != 0 {
lbls["resource"] = resource
}
ClusterQueueResourceReservations.DeletePartialMatch(lbls)
}
func Register() {
metrics.Registry.MustRegister(
AdmissionAttemptsTotal,
admissionAttemptDuration,
PendingWorkloads,
ReservingActiveWorkloads,
AdmittedActiveWorkloads,
QuotaReservedWorkloadsTotal,
quotaReservedWaitTime,
AdmittedWorkloadsTotal,
EvictedWorkloadsTotal,
admissionWaitTime,
admissionChecksWaitTime,
ClusterQueueResourceUsage,
ClusterQueueByStatus,
ClusterQueueResourceReservations,
ClusterQueueResourceNominalQuota,
ClusterQueueResourceBorrowingLimit,
ClusterQueueResourceLendingLimit,
ClusterQueueWeightedShare,
)
}