metrics.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package metrics
  14. import (
  15. "sync"
  16. "time"
  17. "github.com/prometheus/client_golang/prometheus"
  18. volumescheduling "k8s.io/kubernetes/pkg/controller/volume/scheduling"
  19. )
  20. const (
  21. // SchedulerSubsystem - subsystem name used by scheduler
  22. SchedulerSubsystem = "scheduler"
  23. // SchedulingLatencyName - scheduler latency metric name
  24. SchedulingLatencyName = "scheduling_duration_seconds"
  25. // DeprecatedSchedulingLatencyName - scheduler latency metric name which is deprecated
  26. DeprecatedSchedulingLatencyName = "scheduling_latency_seconds"
  27. // OperationLabel - operation label name
  28. OperationLabel = "operation"
  29. // Below are possible values for the operation label. Each represents a substep of e2e scheduling:
  30. // PredicateEvaluation - predicate evaluation operation label value
  31. PredicateEvaluation = "predicate_evaluation"
  32. // PriorityEvaluation - priority evaluation operation label value
  33. PriorityEvaluation = "priority_evaluation"
  34. // PreemptionEvaluation - preemption evaluation operation label value (occurs in case of scheduling fitError).
  35. PreemptionEvaluation = "preemption_evaluation"
  36. // Binding - binding operation label value
  37. Binding = "binding"
  38. // E2eScheduling - e2e scheduling operation label value
  39. )
  40. // All the histogram based metrics have 1ms as size for the smallest bucket.
  41. var (
  42. scheduleAttempts = prometheus.NewCounterVec(
  43. prometheus.CounterOpts{
  44. Subsystem: SchedulerSubsystem,
  45. Name: "schedule_attempts_total",
  46. 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.",
  47. }, []string{"result"})
  48. // PodScheduleSuccesses counts how many pods were scheduled.
  49. PodScheduleSuccesses = scheduleAttempts.With(prometheus.Labels{"result": "scheduled"})
  50. // PodScheduleFailures counts how many pods could not be scheduled.
  51. PodScheduleFailures = scheduleAttempts.With(prometheus.Labels{"result": "unschedulable"})
  52. // PodScheduleErrors counts how many pods could not be scheduled due to a scheduler error.
  53. PodScheduleErrors = scheduleAttempts.With(prometheus.Labels{"result": "error"})
  54. SchedulingLatency = prometheus.NewSummaryVec(
  55. prometheus.SummaryOpts{
  56. Subsystem: SchedulerSubsystem,
  57. Name: SchedulingLatencyName,
  58. Help: "Scheduling latency in seconds split by sub-parts of the scheduling operation",
  59. // Make the sliding window of 5h.
  60. // TODO: The value for this should be based on some SLI definition (long term).
  61. MaxAge: 5 * time.Hour,
  62. },
  63. []string{OperationLabel},
  64. )
  65. DeprecatedSchedulingLatency = prometheus.NewSummaryVec(
  66. prometheus.SummaryOpts{
  67. Subsystem: SchedulerSubsystem,
  68. Name: DeprecatedSchedulingLatencyName,
  69. Help: "(Deprecated) Scheduling latency in seconds split by sub-parts of the scheduling operation",
  70. // Make the sliding window of 5h.
  71. // TODO: The value for this should be based on some SLI definition (long term).
  72. MaxAge: 5 * time.Hour,
  73. },
  74. []string{OperationLabel},
  75. )
  76. E2eSchedulingLatency = prometheus.NewHistogram(
  77. prometheus.HistogramOpts{
  78. Subsystem: SchedulerSubsystem,
  79. Name: "e2e_scheduling_duration_seconds",
  80. Help: "E2e scheduling latency in seconds (scheduling algorithm + binding)",
  81. Buckets: prometheus.ExponentialBuckets(0.001, 2, 15),
  82. },
  83. )
  84. DeprecatedE2eSchedulingLatency = prometheus.NewHistogram(
  85. prometheus.HistogramOpts{
  86. Subsystem: SchedulerSubsystem,
  87. Name: "e2e_scheduling_latency_microseconds",
  88. Help: "(Deprecated) E2e scheduling latency in microseconds (scheduling algorithm + binding)",
  89. Buckets: prometheus.ExponentialBuckets(1000, 2, 15),
  90. },
  91. )
  92. SchedulingAlgorithmLatency = prometheus.NewHistogram(
  93. prometheus.HistogramOpts{
  94. Subsystem: SchedulerSubsystem,
  95. Name: "scheduling_algorithm_duration_seconds",
  96. Help: "Scheduling algorithm latency in seconds",
  97. Buckets: prometheus.ExponentialBuckets(0.001, 2, 15),
  98. },
  99. )
  100. DeprecatedSchedulingAlgorithmLatency = prometheus.NewHistogram(
  101. prometheus.HistogramOpts{
  102. Subsystem: SchedulerSubsystem,
  103. Name: "scheduling_algorithm_latency_microseconds",
  104. Help: "(Deprecated) Scheduling algorithm latency in microseconds",
  105. Buckets: prometheus.ExponentialBuckets(1000, 2, 15),
  106. },
  107. )
  108. SchedulingAlgorithmPredicateEvaluationDuration = prometheus.NewHistogram(
  109. prometheus.HistogramOpts{
  110. Subsystem: SchedulerSubsystem,
  111. Name: "scheduling_algorithm_predicate_evaluation_seconds",
  112. Help: "Scheduling algorithm predicate evaluation duration in seconds",
  113. Buckets: prometheus.ExponentialBuckets(0.001, 2, 15),
  114. },
  115. )
  116. DeprecatedSchedulingAlgorithmPredicateEvaluationDuration = prometheus.NewHistogram(
  117. prometheus.HistogramOpts{
  118. Subsystem: SchedulerSubsystem,
  119. Name: "scheduling_algorithm_predicate_evaluation",
  120. Help: "(Deprecated) Scheduling algorithm predicate evaluation duration in microseconds",
  121. Buckets: prometheus.ExponentialBuckets(1000, 2, 15),
  122. },
  123. )
  124. SchedulingAlgorithmPriorityEvaluationDuration = prometheus.NewHistogram(
  125. prometheus.HistogramOpts{
  126. Subsystem: SchedulerSubsystem,
  127. Name: "scheduling_algorithm_priority_evaluation_seconds",
  128. Help: "Scheduling algorithm priority evaluation duration in seconds",
  129. Buckets: prometheus.ExponentialBuckets(0.001, 2, 15),
  130. },
  131. )
  132. DeprecatedSchedulingAlgorithmPriorityEvaluationDuration = prometheus.NewHistogram(
  133. prometheus.HistogramOpts{
  134. Subsystem: SchedulerSubsystem,
  135. Name: "scheduling_algorithm_priority_evaluation",
  136. Help: "(Deprecated) Scheduling algorithm priority evaluation duration in microseconds",
  137. Buckets: prometheus.ExponentialBuckets(1000, 2, 15),
  138. },
  139. )
  140. SchedulingAlgorithmPremptionEvaluationDuration = prometheus.NewHistogram(
  141. prometheus.HistogramOpts{
  142. Subsystem: SchedulerSubsystem,
  143. Name: "scheduling_algorithm_preemption_evaluation_seconds",
  144. Help: "Scheduling algorithm preemption evaluation duration in seconds",
  145. Buckets: prometheus.ExponentialBuckets(0.001, 2, 15),
  146. },
  147. )
  148. DeprecatedSchedulingAlgorithmPremptionEvaluationDuration = prometheus.NewHistogram(
  149. prometheus.HistogramOpts{
  150. Subsystem: SchedulerSubsystem,
  151. Name: "scheduling_algorithm_preemption_evaluation",
  152. Help: "(Deprecated) Scheduling algorithm preemption evaluation duration in microseconds",
  153. Buckets: prometheus.ExponentialBuckets(1000, 2, 15),
  154. },
  155. )
  156. BindingLatency = prometheus.NewHistogram(
  157. prometheus.HistogramOpts{
  158. Subsystem: SchedulerSubsystem,
  159. Name: "binding_duration_seconds",
  160. Help: "Binding latency in seconds",
  161. Buckets: prometheus.ExponentialBuckets(0.001, 2, 15),
  162. },
  163. )
  164. DeprecatedBindingLatency = prometheus.NewHistogram(
  165. prometheus.HistogramOpts{
  166. Subsystem: SchedulerSubsystem,
  167. Name: "binding_latency_microseconds",
  168. Help: "(Deprecated) Binding latency in microseconds",
  169. Buckets: prometheus.ExponentialBuckets(1000, 2, 15),
  170. },
  171. )
  172. PreemptionVictims = prometheus.NewGauge(
  173. prometheus.GaugeOpts{
  174. Subsystem: SchedulerSubsystem,
  175. Name: "pod_preemption_victims",
  176. Help: "Number of selected preemption victims",
  177. })
  178. PreemptionAttempts = prometheus.NewCounter(
  179. prometheus.CounterOpts{
  180. Subsystem: SchedulerSubsystem,
  181. Name: "total_preemption_attempts",
  182. Help: "Total preemption attempts in the cluster till now",
  183. })
  184. pendingPods = prometheus.NewGaugeVec(
  185. prometheus.GaugeOpts{
  186. Subsystem: SchedulerSubsystem,
  187. Name: "pending_pods",
  188. Help: "Number of pending pods, by the queue type. 'active' means number of pods in activeQ; 'backoff' means number of pods in backoffQ; 'unschedulable' means number of pods in unschedulableQ.",
  189. }, []string{"queue"})
  190. ActivePods = pendingPods.With(prometheus.Labels{"queue": "active"})
  191. BackoffPods = pendingPods.With(prometheus.Labels{"queue": "backoff"})
  192. UnschedulablePods = pendingPods.With(prometheus.Labels{"queue": "unschedulable"})
  193. metricsList = []prometheus.Collector{
  194. scheduleAttempts,
  195. SchedulingLatency,
  196. DeprecatedSchedulingLatency,
  197. E2eSchedulingLatency,
  198. DeprecatedE2eSchedulingLatency,
  199. SchedulingAlgorithmLatency,
  200. DeprecatedSchedulingAlgorithmLatency,
  201. BindingLatency,
  202. DeprecatedBindingLatency,
  203. SchedulingAlgorithmPredicateEvaluationDuration,
  204. DeprecatedSchedulingAlgorithmPredicateEvaluationDuration,
  205. SchedulingAlgorithmPriorityEvaluationDuration,
  206. DeprecatedSchedulingAlgorithmPriorityEvaluationDuration,
  207. SchedulingAlgorithmPremptionEvaluationDuration,
  208. DeprecatedSchedulingAlgorithmPremptionEvaluationDuration,
  209. PreemptionVictims,
  210. PreemptionAttempts,
  211. pendingPods,
  212. }
  213. )
  214. var registerMetrics sync.Once
  215. // Register all metrics.
  216. func Register() {
  217. // Register the metrics.
  218. registerMetrics.Do(func() {
  219. for _, metric := range metricsList {
  220. prometheus.MustRegister(metric)
  221. }
  222. volumescheduling.RegisterVolumeSchedulingMetrics()
  223. })
  224. }
  225. // Reset resets metrics
  226. func Reset() {
  227. SchedulingLatency.Reset()
  228. DeprecatedSchedulingLatency.Reset()
  229. }
  230. // SinceInMicroseconds gets the time since the specified start in microseconds.
  231. func SinceInMicroseconds(start time.Time) float64 {
  232. return float64(time.Since(start).Nanoseconds() / time.Microsecond.Nanoseconds())
  233. }
  234. // SinceInSeconds gets the time since the specified start in seconds.
  235. func SinceInSeconds(start time.Time) float64 {
  236. return time.Since(start).Seconds()
  237. }