pkg/metrics/metrics.go (125 lines of code) (raw):

/* 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" ) 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. // 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"}, ) 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 until it was admitted, per 'cluster_queue'", }, []string{"cluster_queue"}, ) // Metrics tied to the cache. 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"}, ) ) func AdmissionAttempt(result AdmissionResult, duration time.Duration) { admissionAttemptsTotal.WithLabelValues(string(result)).Inc() admissionAttemptDuration.WithLabelValues(string(result)).Observe(duration.Seconds()) } func AdmittedWorkload(cqName kueue.ClusterQueueReference, waitTime time.Duration) { AdmittedWorkloadsTotal.WithLabelValues(string(cqName)).Inc() admissionWaitTime.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 ClearQueueSystemMetrics(cqName string) { PendingWorkloads.DeleteLabelValues(cqName, PendingStatusActive) PendingWorkloads.DeleteLabelValues(cqName, PendingStatusInadmissible) AdmittedWorkloadsTotal.DeleteLabelValues(cqName) admissionWaitTime.DeleteLabelValues(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) { AdmittedActiveWorkloads.DeleteLabelValues(cqName) for _, status := range CQStatuses { ClusterQueueByStatus.DeleteLabelValues(cqName, string(status)) } } func Register() { metrics.Registry.MustRegister( admissionAttemptsTotal, admissionAttemptDuration, PendingWorkloads, AdmittedActiveWorkloads, AdmittedWorkloadsTotal, admissionWaitTime, ) }