|
| 1 | +/* |
| 2 | +Copyright 2018 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package metrics |
| 18 | + |
| 19 | +import ( |
| 20 | + "time" |
| 21 | + |
| 22 | + "github.com/prometheus/client_golang/prometheus" |
| 23 | + "github.com/prometheus/client_golang/prometheus/promauto" // auto-registry collectors in default registry |
| 24 | +) |
| 25 | + |
| 26 | +const ( |
| 27 | + // KubeBatchNamespace - namespace in prometheus used by kube-batch |
| 28 | + KubeBatchNamespace = "kube_batch" |
| 29 | + |
| 30 | + // OnSessionOpen label |
| 31 | + OnSessionOpen = "OnSessionOpen" |
| 32 | + |
| 33 | + // OnSessionClose label |
| 34 | + OnSessionClose = "OnSessionClose" |
| 35 | +) |
| 36 | + |
| 37 | +var ( |
| 38 | + e2eSchedulingLatency = promauto.NewHistogram( |
| 39 | + prometheus.HistogramOpts{ |
| 40 | + Subsystem: KubeBatchNamespace, |
| 41 | + Name: "e2e_scheduling_latency_milliseconds", |
| 42 | + Help: "E2e scheduling latency in milliseconds (scheduling algorithm + binding)", |
| 43 | + Buckets: prometheus.ExponentialBuckets(5, 2, 10), |
| 44 | + }, |
| 45 | + ) |
| 46 | + |
| 47 | + pluginSchedulingLatency = promauto.NewHistogramVec( |
| 48 | + prometheus.HistogramOpts{ |
| 49 | + Subsystem: KubeBatchNamespace, |
| 50 | + Name: "plugin_scheduling_latency_microseconds", |
| 51 | + Help: "Plugin scheduling latency in microseconds", |
| 52 | + Buckets: prometheus.ExponentialBuckets(5, 2, 10), |
| 53 | + }, []string{"plugin", "OnSession"}, |
| 54 | + ) |
| 55 | + |
| 56 | + actionSchedulingLatency = promauto.NewHistogramVec( |
| 57 | + prometheus.HistogramOpts{ |
| 58 | + Subsystem: KubeBatchNamespace, |
| 59 | + Name: "action_scheduling_latency_microseconds", |
| 60 | + Help: "Action scheduling latency in microseconds", |
| 61 | + Buckets: prometheus.ExponentialBuckets(5, 2, 10), |
| 62 | + }, []string{"action"}, |
| 63 | + ) |
| 64 | + |
| 65 | + taskSchedulingLatency = promauto.NewHistogram( |
| 66 | + prometheus.HistogramOpts{ |
| 67 | + Subsystem: KubeBatchNamespace, |
| 68 | + Name: "task_scheduling_latency_microseconds", |
| 69 | + Help: "Task scheduling latency in microseconds", |
| 70 | + Buckets: prometheus.ExponentialBuckets(5, 2, 10), |
| 71 | + }, |
| 72 | + ) |
| 73 | + |
| 74 | + scheduleAttempts = promauto.NewCounterVec( |
| 75 | + prometheus.CounterOpts{ |
| 76 | + Subsystem: KubeBatchNamespace, |
| 77 | + Name: "schedule_attempts_total", |
| 78 | + Help: "Number of attempts to schedule pods, by the result. 'unschedulable' means a pod could not be scheduled, while 'error' means an internal scheduler problem.", |
| 79 | + }, []string{"result"}, |
| 80 | + ) |
| 81 | + |
| 82 | + preemptionVictims = promauto.NewGauge( |
| 83 | + prometheus.GaugeOpts{ |
| 84 | + Subsystem: KubeBatchNamespace, |
| 85 | + Name: "pod_preemption_victims", |
| 86 | + Help: "Number of selected preemption victims", |
| 87 | + }, |
| 88 | + ) |
| 89 | + |
| 90 | + preemptionAttempts = promauto.NewCounter( |
| 91 | + prometheus.CounterOpts{ |
| 92 | + Subsystem: KubeBatchNamespace, |
| 93 | + Name: "total_preemption_attempts", |
| 94 | + Help: "Total preemption attempts in the cluster till now", |
| 95 | + }, |
| 96 | + ) |
| 97 | + |
| 98 | + unscheduleTaskCount = promauto.NewGaugeVec( |
| 99 | + prometheus.GaugeOpts{ |
| 100 | + Subsystem: KubeBatchNamespace, |
| 101 | + Name: "unschedule_task_count", |
| 102 | + Help: "Number of tasks could not be scheduled", |
| 103 | + }, []string{"job_id"}, |
| 104 | + ) |
| 105 | + |
| 106 | + unscheduleJobCount = promauto.NewGauge( |
| 107 | + prometheus.GaugeOpts{ |
| 108 | + Subsystem: KubeBatchNamespace, |
| 109 | + Name: "unschedule_job_count", |
| 110 | + Help: "Number of jobs could not be scheduled", |
| 111 | + }, |
| 112 | + ) |
| 113 | + |
| 114 | + jobRetryCount = promauto.NewCounterVec( |
| 115 | + prometheus.CounterOpts{ |
| 116 | + Subsystem: KubeBatchNamespace, |
| 117 | + Name: "job_retry_counts", |
| 118 | + Help: "Number of retry counts for one job", |
| 119 | + }, []string{"job_id"}, |
| 120 | + ) |
| 121 | +) |
| 122 | + |
| 123 | +// UpdatePluginDuration updates latency for every plugin |
| 124 | +func UpdatePluginDuration(pluginName, OnSessionStatus string, duration time.Duration) { |
| 125 | + pluginSchedulingLatency.WithLabelValues(pluginName, OnSessionStatus).Observe(DurationInMicroseconds(duration)) |
| 126 | +} |
| 127 | + |
| 128 | +// UpdateActionDuration updates latency for every action |
| 129 | +func UpdateActionDuration(actionName string, duration time.Duration) { |
| 130 | + actionSchedulingLatency.WithLabelValues(actionName).Observe(DurationInMicroseconds(duration)) |
| 131 | +} |
| 132 | + |
| 133 | +// UpdateE2eDuration updates entire end to end scheduling latency |
| 134 | +func UpdateE2eDuration(duration time.Duration) { |
| 135 | + e2eSchedulingLatency.Observe(DurationInMilliseconds(duration)) |
| 136 | +} |
| 137 | + |
| 138 | +// UpdateTaskScheduleDuration updates single task scheduling latency |
| 139 | +func UpdateTaskScheduleDuration(duration time.Duration) { |
| 140 | + taskSchedulingLatency.Observe(DurationInMicroseconds(duration)) |
| 141 | +} |
| 142 | + |
| 143 | +// UpdatePodScheduleStatus update pod schedule decision, could be Success, Failure, Error |
| 144 | +func UpdatePodScheduleStatus(label string, count int) { |
| 145 | + scheduleAttempts.WithLabelValues(label).Add(float64(count)) |
| 146 | +} |
| 147 | + |
| 148 | +// UpdatePreemptionVictimsCount updates count of preemption victims |
| 149 | +func UpdatePreemptionVictimsCount(victimsCount int) { |
| 150 | + preemptionVictims.Set(float64(victimsCount)) |
| 151 | +} |
| 152 | + |
| 153 | +// RegisterPreemptionAttempts records number of attempts for preemtion |
| 154 | +func RegisterPreemptionAttempts() { |
| 155 | + preemptionAttempts.Inc() |
| 156 | +} |
| 157 | + |
| 158 | +// UpdateUnscheduleTaskCount records total number of unscheduleable tasks |
| 159 | +func UpdateUnscheduleTaskCount(jobID string, taskCount int) { |
| 160 | + unscheduleTaskCount.WithLabelValues(jobID).Set(float64(taskCount)) |
| 161 | +} |
| 162 | + |
| 163 | +// UpdateUnscheduleJobCount records total number of unscheduleable jobs |
| 164 | +func UpdateUnscheduleJobCount(jobCount int) { |
| 165 | + unscheduleJobCount.Set(float64(jobCount)) |
| 166 | +} |
| 167 | + |
| 168 | +// RegisterJobRetries total number of job retries. |
| 169 | +func RegisterJobRetries(jobID string) { |
| 170 | + jobRetryCount.WithLabelValues(jobID).Inc() |
| 171 | +} |
| 172 | + |
| 173 | +// DurationInMicroseconds gets the time in microseconds. |
| 174 | +func DurationInMicroseconds(duration time.Duration) float64 { |
| 175 | + return float64(duration.Nanoseconds()) / float64(time.Microsecond.Nanoseconds()) |
| 176 | +} |
| 177 | + |
| 178 | +// DurationInMilliseconds gets the time in milliseconds. |
| 179 | +func DurationInMilliseconds(duration time.Duration) float64 { |
| 180 | + return float64(duration.Nanoseconds()) / float64(time.Millisecond.Nanoseconds()) |
| 181 | +} |
| 182 | + |
| 183 | +// DurationInSeconds gets the time in seconds. |
| 184 | +func DurationInSeconds(duration time.Duration) float64 { |
| 185 | + return duration.Seconds() |
| 186 | +} |
| 187 | + |
| 188 | +// Duration get the time since specified start |
| 189 | +func Duration(start time.Time) time.Duration { |
| 190 | + return time.Since(start) |
| 191 | +} |
0 commit comments