metrics.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. Copyright 2016 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 workqueue
  14. import (
  15. "sync"
  16. "time"
  17. "k8s.io/utils/clock"
  18. )
  19. // This file provides abstractions for setting the provider (e.g., prometheus)
  20. // of metrics.
  21. type queueMetrics interface {
  22. add(item t)
  23. get(item t)
  24. done(item t)
  25. updateUnfinishedWork()
  26. }
  27. // GaugeMetric represents a single numerical value that can arbitrarily go up
  28. // and down.
  29. type GaugeMetric interface {
  30. Inc()
  31. Dec()
  32. }
  33. // SettableGaugeMetric represents a single numerical value that can arbitrarily go up
  34. // and down. (Separate from GaugeMetric to preserve backwards compatibility.)
  35. type SettableGaugeMetric interface {
  36. Set(float64)
  37. }
  38. // CounterMetric represents a single numerical value that only ever
  39. // goes up.
  40. type CounterMetric interface {
  41. Inc()
  42. }
  43. // SummaryMetric captures individual observations.
  44. type SummaryMetric interface {
  45. Observe(float64)
  46. }
  47. // HistogramMetric counts individual observations.
  48. type HistogramMetric interface {
  49. Observe(float64)
  50. }
  51. type noopMetric struct{}
  52. func (noopMetric) Inc() {}
  53. func (noopMetric) Dec() {}
  54. func (noopMetric) Set(float64) {}
  55. func (noopMetric) Observe(float64) {}
  56. // defaultQueueMetrics expects the caller to lock before setting any metrics.
  57. type defaultQueueMetrics struct {
  58. clock clock.Clock
  59. // current depth of a workqueue
  60. depth GaugeMetric
  61. // total number of adds handled by a workqueue
  62. adds CounterMetric
  63. // how long an item stays in a workqueue
  64. latency HistogramMetric
  65. // how long processing an item from a workqueue takes
  66. workDuration HistogramMetric
  67. addTimes map[t]time.Time
  68. processingStartTimes map[t]time.Time
  69. // how long have current threads been working?
  70. unfinishedWorkSeconds SettableGaugeMetric
  71. longestRunningProcessor SettableGaugeMetric
  72. }
  73. func (m *defaultQueueMetrics) add(item t) {
  74. if m == nil {
  75. return
  76. }
  77. m.adds.Inc()
  78. m.depth.Inc()
  79. if _, exists := m.addTimes[item]; !exists {
  80. m.addTimes[item] = m.clock.Now()
  81. }
  82. }
  83. func (m *defaultQueueMetrics) get(item t) {
  84. if m == nil {
  85. return
  86. }
  87. m.depth.Dec()
  88. m.processingStartTimes[item] = m.clock.Now()
  89. if startTime, exists := m.addTimes[item]; exists {
  90. m.latency.Observe(m.sinceInSeconds(startTime))
  91. delete(m.addTimes, item)
  92. }
  93. }
  94. func (m *defaultQueueMetrics) done(item t) {
  95. if m == nil {
  96. return
  97. }
  98. if startTime, exists := m.processingStartTimes[item]; exists {
  99. m.workDuration.Observe(m.sinceInSeconds(startTime))
  100. delete(m.processingStartTimes, item)
  101. }
  102. }
  103. func (m *defaultQueueMetrics) updateUnfinishedWork() {
  104. // Note that a summary metric would be better for this, but prometheus
  105. // doesn't seem to have non-hacky ways to reset the summary metrics.
  106. var total float64
  107. var oldest float64
  108. for _, t := range m.processingStartTimes {
  109. age := m.sinceInSeconds(t)
  110. total += age
  111. if age > oldest {
  112. oldest = age
  113. }
  114. }
  115. m.unfinishedWorkSeconds.Set(total)
  116. m.longestRunningProcessor.Set(oldest)
  117. }
  118. type noMetrics struct{}
  119. func (noMetrics) add(item t) {}
  120. func (noMetrics) get(item t) {}
  121. func (noMetrics) done(item t) {}
  122. func (noMetrics) updateUnfinishedWork() {}
  123. // Gets the time since the specified start in seconds.
  124. func (m *defaultQueueMetrics) sinceInSeconds(start time.Time) float64 {
  125. return m.clock.Since(start).Seconds()
  126. }
  127. type retryMetrics interface {
  128. retry()
  129. }
  130. type defaultRetryMetrics struct {
  131. retries CounterMetric
  132. }
  133. func (m *defaultRetryMetrics) retry() {
  134. if m == nil {
  135. return
  136. }
  137. m.retries.Inc()
  138. }
  139. // MetricsProvider generates various metrics used by the queue.
  140. type MetricsProvider interface {
  141. NewDepthMetric(name string) GaugeMetric
  142. NewAddsMetric(name string) CounterMetric
  143. NewLatencyMetric(name string) HistogramMetric
  144. NewWorkDurationMetric(name string) HistogramMetric
  145. NewUnfinishedWorkSecondsMetric(name string) SettableGaugeMetric
  146. NewLongestRunningProcessorSecondsMetric(name string) SettableGaugeMetric
  147. NewRetriesMetric(name string) CounterMetric
  148. }
  149. type noopMetricsProvider struct{}
  150. func (_ noopMetricsProvider) NewDepthMetric(name string) GaugeMetric {
  151. return noopMetric{}
  152. }
  153. func (_ noopMetricsProvider) NewAddsMetric(name string) CounterMetric {
  154. return noopMetric{}
  155. }
  156. func (_ noopMetricsProvider) NewLatencyMetric(name string) HistogramMetric {
  157. return noopMetric{}
  158. }
  159. func (_ noopMetricsProvider) NewWorkDurationMetric(name string) HistogramMetric {
  160. return noopMetric{}
  161. }
  162. func (_ noopMetricsProvider) NewUnfinishedWorkSecondsMetric(name string) SettableGaugeMetric {
  163. return noopMetric{}
  164. }
  165. func (_ noopMetricsProvider) NewLongestRunningProcessorSecondsMetric(name string) SettableGaugeMetric {
  166. return noopMetric{}
  167. }
  168. func (_ noopMetricsProvider) NewRetriesMetric(name string) CounterMetric {
  169. return noopMetric{}
  170. }
  171. var globalMetricsFactory = queueMetricsFactory{
  172. metricsProvider: noopMetricsProvider{},
  173. }
  174. type queueMetricsFactory struct {
  175. metricsProvider MetricsProvider
  176. onlyOnce sync.Once
  177. }
  178. func (f *queueMetricsFactory) setProvider(mp MetricsProvider) {
  179. f.onlyOnce.Do(func() {
  180. f.metricsProvider = mp
  181. })
  182. }
  183. func (f *queueMetricsFactory) newQueueMetrics(name string, clock clock.Clock) queueMetrics {
  184. mp := f.metricsProvider
  185. if len(name) == 0 || mp == (noopMetricsProvider{}) {
  186. return noMetrics{}
  187. }
  188. return &defaultQueueMetrics{
  189. clock: clock,
  190. depth: mp.NewDepthMetric(name),
  191. adds: mp.NewAddsMetric(name),
  192. latency: mp.NewLatencyMetric(name),
  193. workDuration: mp.NewWorkDurationMetric(name),
  194. unfinishedWorkSeconds: mp.NewUnfinishedWorkSecondsMetric(name),
  195. longestRunningProcessor: mp.NewLongestRunningProcessorSecondsMetric(name),
  196. addTimes: map[t]time.Time{},
  197. processingStartTimes: map[t]time.Time{},
  198. }
  199. }
  200. func newRetryMetrics(name string, provider MetricsProvider) retryMetrics {
  201. var ret *defaultRetryMetrics
  202. if len(name) == 0 {
  203. return ret
  204. }
  205. if provider == nil {
  206. provider = globalMetricsFactory.metricsProvider
  207. }
  208. return &defaultRetryMetrics{
  209. retries: provider.NewRetriesMetric(name),
  210. }
  211. }
  212. // SetProvider sets the metrics provider for all subsequently created work
  213. // queues. Only the first call has an effect.
  214. func SetProvider(metricsProvider MetricsProvider) {
  215. globalMetricsFactory.setProvider(metricsProvider)
  216. }