metrics.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. Copyright 2017 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. )
  19. const kubeProxySubsystem = "kubeproxy"
  20. var (
  21. // SyncProxyRulesLatency is the latency of one round of kube-proxy syncing proxy rules.
  22. SyncProxyRulesLatency = prometheus.NewHistogram(
  23. prometheus.HistogramOpts{
  24. Subsystem: kubeProxySubsystem,
  25. Name: "sync_proxy_rules_duration_seconds",
  26. Help: "SyncProxyRules latency in seconds",
  27. Buckets: prometheus.ExponentialBuckets(0.001, 2, 15),
  28. },
  29. )
  30. // DeprecatedSyncProxyRulesLatency is the latency of one round of kube-proxy syncing proxy rules.
  31. DeprecatedSyncProxyRulesLatency = prometheus.NewHistogram(
  32. prometheus.HistogramOpts{
  33. Subsystem: kubeProxySubsystem,
  34. Name: "sync_proxy_rules_latency_microseconds",
  35. Help: "(Deprecated) SyncProxyRules latency in microseconds",
  36. Buckets: prometheus.ExponentialBuckets(1000, 2, 15),
  37. },
  38. )
  39. // SyncProxyRulesLastTimestamp is the timestamp proxy rules were last
  40. // successfully synced.
  41. SyncProxyRulesLastTimestamp = prometheus.NewGauge(
  42. prometheus.GaugeOpts{
  43. Subsystem: kubeProxySubsystem,
  44. Name: "sync_proxy_rules_last_timestamp_seconds",
  45. Help: "The last time proxy rules were successfully synced",
  46. },
  47. )
  48. // NetworkProgrammingLatency is defined as the time it took to program the network - from the time
  49. // the service or pod has changed to the time the change was propagated and the proper kube-proxy
  50. // rules were synced. Exported for each endpoints object that were part of the rules sync.
  51. // See https://github.com/kubernetes/community/blob/master/sig-scalability/slos/network_programming_latency.md
  52. // Note that the metrics is partially based on the time exported by the endpoints controller on
  53. // the master machine. The measurement may be inaccurate if there is a clock drift between the
  54. // node and master machine.
  55. NetworkProgrammingLatency = prometheus.NewHistogram(
  56. prometheus.HistogramOpts{
  57. Subsystem: kubeProxySubsystem,
  58. Name: "network_programming_duration_seconds",
  59. Help: "In Cluster Network Programming Latency in seconds",
  60. // TODO(mm4tt): Reevaluate buckets before 1.14 release.
  61. // The last bucket will be [0.001s*2^20 ~= 17min, +inf)
  62. Buckets: prometheus.ExponentialBuckets(0.001, 2, 20),
  63. },
  64. )
  65. // EndpointChangesPending is the number of pending endpoint changes that
  66. // have not yet been synced to the proxy.
  67. EndpointChangesPending = prometheus.NewGauge(
  68. prometheus.GaugeOpts{
  69. Subsystem: kubeProxySubsystem,
  70. Name: "sync_proxy_rules_endpoint_changes_pending",
  71. Help: "Pending proxy rules Endpoint changes",
  72. },
  73. )
  74. // EndpointChangesTotal is the number of endpoint changes that the proxy
  75. // has seen.
  76. EndpointChangesTotal = prometheus.NewCounter(
  77. prometheus.CounterOpts{
  78. Subsystem: kubeProxySubsystem,
  79. Name: "sync_proxy_rules_endpoint_changes_total",
  80. Help: "Cumulative proxy rules Endpoint changes",
  81. },
  82. )
  83. // ServiceChangesPending is the number of pending service changes that
  84. // have not yet been synced to the proxy.
  85. ServiceChangesPending = prometheus.NewGauge(
  86. prometheus.GaugeOpts{
  87. Subsystem: kubeProxySubsystem,
  88. Name: "sync_proxy_rules_service_changes_pending",
  89. Help: "Pending proxy rules Service changes",
  90. },
  91. )
  92. // ServiceChangesTotal is the number of service changes that the proxy has
  93. // seen.
  94. ServiceChangesTotal = prometheus.NewCounter(
  95. prometheus.CounterOpts{
  96. Subsystem: kubeProxySubsystem,
  97. Name: "sync_proxy_rules_service_changes_total",
  98. Help: "Cumulative proxy rules Service changes",
  99. },
  100. )
  101. )
  102. var registerMetricsOnce sync.Once
  103. // RegisterMetrics registers kube-proxy metrics.
  104. func RegisterMetrics() {
  105. registerMetricsOnce.Do(func() {
  106. prometheus.MustRegister(SyncProxyRulesLatency)
  107. prometheus.MustRegister(DeprecatedSyncProxyRulesLatency)
  108. prometheus.MustRegister(SyncProxyRulesLastTimestamp)
  109. prometheus.MustRegister(NetworkProgrammingLatency)
  110. prometheus.MustRegister(EndpointChangesPending)
  111. prometheus.MustRegister(EndpointChangesTotal)
  112. prometheus.MustRegister(ServiceChangesPending)
  113. prometheus.MustRegister(ServiceChangesTotal)
  114. })
  115. }
  116. // SinceInMicroseconds gets the time since the specified start in microseconds.
  117. func SinceInMicroseconds(start time.Time) float64 {
  118. return float64(time.Since(start).Nanoseconds() / time.Microsecond.Nanoseconds())
  119. }
  120. // SinceInSeconds gets the time since the specified start in seconds.
  121. func SinceInSeconds(start time.Time) float64 {
  122. return time.Since(start).Seconds()
  123. }