metrics.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. "k8s.io/component-base/metrics"
  18. "k8s.io/component-base/metrics/legacyregistry"
  19. )
  20. const kubeProxySubsystem = "kubeproxy"
  21. var (
  22. // SyncProxyRulesLatency is the latency of one round of kube-proxy syncing proxy rules.
  23. SyncProxyRulesLatency = metrics.NewHistogram(
  24. &metrics.HistogramOpts{
  25. Subsystem: kubeProxySubsystem,
  26. Name: "sync_proxy_rules_duration_seconds",
  27. Help: "SyncProxyRules latency in seconds",
  28. Buckets: metrics.ExponentialBuckets(0.001, 2, 15),
  29. StabilityLevel: metrics.ALPHA,
  30. },
  31. )
  32. // SyncProxyRulesLastTimestamp is the timestamp proxy rules were last
  33. // successfully synced.
  34. SyncProxyRulesLastTimestamp = metrics.NewGauge(
  35. &metrics.GaugeOpts{
  36. Subsystem: kubeProxySubsystem,
  37. Name: "sync_proxy_rules_last_timestamp_seconds",
  38. Help: "The last time proxy rules were successfully synced",
  39. StabilityLevel: metrics.ALPHA,
  40. },
  41. )
  42. // NetworkProgrammingLatency is defined as the time it took to program the network - from the time
  43. // the service or pod has changed to the time the change was propagated and the proper kube-proxy
  44. // rules were synced. Exported for each endpoints object that were part of the rules sync.
  45. // See https://github.com/kubernetes/community/blob/master/sig-scalability/slos/network_programming_latency.md
  46. // Note that the metrics is partially based on the time exported by the endpoints controller on
  47. // the master machine. The measurement may be inaccurate if there is a clock drift between the
  48. // node and master machine.
  49. NetworkProgrammingLatency = metrics.NewHistogram(
  50. &metrics.HistogramOpts{
  51. Subsystem: kubeProxySubsystem,
  52. Name: "network_programming_duration_seconds",
  53. Help: "In Cluster Network Programming Latency in seconds",
  54. Buckets: merge(
  55. metrics.LinearBuckets(0.25, 0.25, 2), // 0.25s, 0.50s
  56. metrics.LinearBuckets(1, 1, 59), // 1s, 2s, 3s, ... 59s
  57. metrics.LinearBuckets(60, 5, 12), // 60s, 65s, 70s, ... 115s
  58. metrics.LinearBuckets(120, 30, 7), // 2min, 2.5min, 3min, ..., 5min
  59. ),
  60. StabilityLevel: metrics.ALPHA,
  61. },
  62. )
  63. // EndpointChangesPending is the number of pending endpoint changes that
  64. // have not yet been synced to the proxy.
  65. EndpointChangesPending = metrics.NewGauge(
  66. &metrics.GaugeOpts{
  67. Subsystem: kubeProxySubsystem,
  68. Name: "sync_proxy_rules_endpoint_changes_pending",
  69. Help: "Pending proxy rules Endpoint changes",
  70. StabilityLevel: metrics.ALPHA,
  71. },
  72. )
  73. // EndpointChangesTotal is the number of endpoint changes that the proxy
  74. // has seen.
  75. EndpointChangesTotal = metrics.NewCounter(
  76. &metrics.CounterOpts{
  77. Subsystem: kubeProxySubsystem,
  78. Name: "sync_proxy_rules_endpoint_changes_total",
  79. Help: "Cumulative proxy rules Endpoint changes",
  80. StabilityLevel: metrics.ALPHA,
  81. },
  82. )
  83. // ServiceChangesPending is the number of pending service changes that
  84. // have not yet been synced to the proxy.
  85. ServiceChangesPending = metrics.NewGauge(
  86. &metrics.GaugeOpts{
  87. Subsystem: kubeProxySubsystem,
  88. Name: "sync_proxy_rules_service_changes_pending",
  89. Help: "Pending proxy rules Service changes",
  90. StabilityLevel: metrics.ALPHA,
  91. },
  92. )
  93. // ServiceChangesTotal is the number of service changes that the proxy has
  94. // seen.
  95. ServiceChangesTotal = metrics.NewCounter(
  96. &metrics.CounterOpts{
  97. Subsystem: kubeProxySubsystem,
  98. Name: "sync_proxy_rules_service_changes_total",
  99. Help: "Cumulative proxy rules Service changes",
  100. StabilityLevel: metrics.ALPHA,
  101. },
  102. )
  103. // IptablesRestoreFailuresTotal is the number of iptables restore failures that the proxy has
  104. // seen.
  105. IptablesRestoreFailuresTotal = metrics.NewCounter(
  106. &metrics.CounterOpts{
  107. Subsystem: kubeProxySubsystem,
  108. Name: "sync_proxy_rules_iptables_restore_failures_total",
  109. Help: "Cumulative proxy iptables restore failures",
  110. StabilityLevel: metrics.ALPHA,
  111. },
  112. )
  113. )
  114. var registerMetricsOnce sync.Once
  115. // RegisterMetrics registers kube-proxy metrics.
  116. func RegisterMetrics() {
  117. registerMetricsOnce.Do(func() {
  118. legacyregistry.MustRegister(SyncProxyRulesLatency)
  119. legacyregistry.MustRegister(SyncProxyRulesLastTimestamp)
  120. legacyregistry.MustRegister(NetworkProgrammingLatency)
  121. legacyregistry.MustRegister(EndpointChangesPending)
  122. legacyregistry.MustRegister(EndpointChangesTotal)
  123. legacyregistry.MustRegister(ServiceChangesPending)
  124. legacyregistry.MustRegister(ServiceChangesTotal)
  125. legacyregistry.MustRegister(IptablesRestoreFailuresTotal)
  126. })
  127. }
  128. // SinceInSeconds gets the time since the specified start in seconds.
  129. func SinceInSeconds(start time.Time) float64 {
  130. return time.Since(start).Seconds()
  131. }
  132. func merge(slices ...[]float64) []float64 {
  133. result := make([]float64, 1)
  134. for _, s := range slices {
  135. result = append(result, s...)
  136. }
  137. return result
  138. }