k8s.io/kubernetes@v1.29.3/pkg/proxy/metrics/metrics.go (about) 1 /* 2 Copyright 2017 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 "sync" 21 "time" 22 23 "k8s.io/component-base/metrics" 24 "k8s.io/component-base/metrics/legacyregistry" 25 ) 26 27 const kubeProxySubsystem = "kubeproxy" 28 29 var ( 30 // SyncProxyRulesLatency is the latency of one round of kube-proxy syncing proxy 31 // rules. (With the iptables proxy, this includes both full and partial syncs.) 32 SyncProxyRulesLatency = metrics.NewHistogram( 33 &metrics.HistogramOpts{ 34 Subsystem: kubeProxySubsystem, 35 Name: "sync_proxy_rules_duration_seconds", 36 Help: "SyncProxyRules latency in seconds", 37 Buckets: metrics.ExponentialBuckets(0.001, 2, 15), 38 StabilityLevel: metrics.ALPHA, 39 }, 40 ) 41 42 // SyncFullProxyRulesLatency is the latency of one round of full rule syncing. 43 SyncFullProxyRulesLatency = metrics.NewHistogram( 44 &metrics.HistogramOpts{ 45 Subsystem: kubeProxySubsystem, 46 Name: "sync_full_proxy_rules_duration_seconds", 47 Help: "SyncProxyRules latency in seconds for full resyncs", 48 Buckets: metrics.ExponentialBuckets(0.001, 2, 15), 49 StabilityLevel: metrics.ALPHA, 50 }, 51 ) 52 53 // SyncPartialProxyRulesLatency is the latency of one round of partial rule syncing. 54 SyncPartialProxyRulesLatency = metrics.NewHistogram( 55 &metrics.HistogramOpts{ 56 Subsystem: kubeProxySubsystem, 57 Name: "sync_partial_proxy_rules_duration_seconds", 58 Help: "SyncProxyRules latency in seconds for partial resyncs", 59 Buckets: metrics.ExponentialBuckets(0.001, 2, 15), 60 StabilityLevel: metrics.ALPHA, 61 }, 62 ) 63 64 // SyncProxyRulesLastTimestamp is the timestamp proxy rules were last 65 // successfully synced. 66 SyncProxyRulesLastTimestamp = metrics.NewGauge( 67 &metrics.GaugeOpts{ 68 Subsystem: kubeProxySubsystem, 69 Name: "sync_proxy_rules_last_timestamp_seconds", 70 Help: "The last time proxy rules were successfully synced", 71 StabilityLevel: metrics.ALPHA, 72 }, 73 ) 74 75 // NetworkProgrammingLatency is defined as the time it took to program the network - from the time 76 // the service or pod has changed to the time the change was propagated and the proper kube-proxy 77 // rules were synced. Exported for each endpoints object that were part of the rules sync. 78 // See https://github.com/kubernetes/community/blob/master/sig-scalability/slos/network_programming_latency.md 79 // Note that the metrics is partially based on the time exported by the endpoints controller on 80 // the master machine. The measurement may be inaccurate if there is a clock drift between the 81 // node and master machine. 82 NetworkProgrammingLatency = metrics.NewHistogram( 83 &metrics.HistogramOpts{ 84 Subsystem: kubeProxySubsystem, 85 Name: "network_programming_duration_seconds", 86 Help: "In Cluster Network Programming Latency in seconds", 87 Buckets: metrics.MergeBuckets( 88 metrics.LinearBuckets(0.25, 0.25, 2), // 0.25s, 0.50s 89 metrics.LinearBuckets(1, 1, 59), // 1s, 2s, 3s, ... 59s 90 metrics.LinearBuckets(60, 5, 12), // 60s, 65s, 70s, ... 115s 91 metrics.LinearBuckets(120, 30, 7), // 2min, 2.5min, 3min, ..., 5min 92 ), 93 StabilityLevel: metrics.ALPHA, 94 }, 95 ) 96 97 // EndpointChangesPending is the number of pending endpoint changes that 98 // have not yet been synced to the proxy. 99 EndpointChangesPending = metrics.NewGauge( 100 &metrics.GaugeOpts{ 101 Subsystem: kubeProxySubsystem, 102 Name: "sync_proxy_rules_endpoint_changes_pending", 103 Help: "Pending proxy rules Endpoint changes", 104 StabilityLevel: metrics.ALPHA, 105 }, 106 ) 107 108 // EndpointChangesTotal is the number of endpoint changes that the proxy 109 // has seen. 110 EndpointChangesTotal = metrics.NewCounter( 111 &metrics.CounterOpts{ 112 Subsystem: kubeProxySubsystem, 113 Name: "sync_proxy_rules_endpoint_changes_total", 114 Help: "Cumulative proxy rules Endpoint changes", 115 StabilityLevel: metrics.ALPHA, 116 }, 117 ) 118 119 // ServiceChangesPending is the number of pending service changes that 120 // have not yet been synced to the proxy. 121 ServiceChangesPending = metrics.NewGauge( 122 &metrics.GaugeOpts{ 123 Subsystem: kubeProxySubsystem, 124 Name: "sync_proxy_rules_service_changes_pending", 125 Help: "Pending proxy rules Service changes", 126 StabilityLevel: metrics.ALPHA, 127 }, 128 ) 129 130 // ServiceChangesTotal is the number of service changes that the proxy has 131 // seen. 132 ServiceChangesTotal = metrics.NewCounter( 133 &metrics.CounterOpts{ 134 Subsystem: kubeProxySubsystem, 135 Name: "sync_proxy_rules_service_changes_total", 136 Help: "Cumulative proxy rules Service changes", 137 StabilityLevel: metrics.ALPHA, 138 }, 139 ) 140 141 // IptablesRestoreFailuresTotal is the number of iptables restore failures that the proxy has 142 // seen. 143 IptablesRestoreFailuresTotal = metrics.NewCounter( 144 &metrics.CounterOpts{ 145 Subsystem: kubeProxySubsystem, 146 Name: "sync_proxy_rules_iptables_restore_failures_total", 147 Help: "Cumulative proxy iptables restore failures", 148 StabilityLevel: metrics.ALPHA, 149 }, 150 ) 151 152 // IptablesPartialRestoreFailuresTotal is the number of iptables *partial* restore 153 // failures (resulting in a fall back to a full restore) that the proxy has seen. 154 IptablesPartialRestoreFailuresTotal = metrics.NewCounter( 155 &metrics.CounterOpts{ 156 Subsystem: kubeProxySubsystem, 157 Name: "sync_proxy_rules_iptables_partial_restore_failures_total", 158 Help: "Cumulative proxy iptables partial restore failures", 159 StabilityLevel: metrics.ALPHA, 160 }, 161 ) 162 163 // IptablesRulesTotal is the total number of iptables rules that the iptables 164 // proxy has installed. 165 IptablesRulesTotal = metrics.NewGaugeVec( 166 &metrics.GaugeOpts{ 167 Subsystem: kubeProxySubsystem, 168 Name: "sync_proxy_rules_iptables_total", 169 Help: "Total number of iptables rules owned by kube-proxy", 170 StabilityLevel: metrics.ALPHA, 171 }, 172 []string{"table"}, 173 ) 174 175 // IptablesRulesLastSync is the number of iptables rules that the iptables proxy 176 // updated in the last sync. 177 IptablesRulesLastSync = metrics.NewGaugeVec( 178 &metrics.GaugeOpts{ 179 Subsystem: kubeProxySubsystem, 180 Name: "sync_proxy_rules_iptables_last", 181 Help: "Number of iptables rules written by kube-proxy in last sync", 182 StabilityLevel: metrics.ALPHA, 183 }, 184 []string{"table"}, 185 ) 186 187 // ProxyHealthzTotal is the number of returned HTTP Status for each 188 // healthz probe. 189 ProxyHealthzTotal = metrics.NewCounterVec( 190 &metrics.CounterOpts{ 191 Subsystem: kubeProxySubsystem, 192 Name: "proxy_healthz_total", 193 Help: "Cumulative proxy healthz HTTP status", 194 StabilityLevel: metrics.ALPHA, 195 }, 196 []string{"code"}, 197 ) 198 199 // ProxyLivezTotal is the number of returned HTTP Status for each 200 // livez probe. 201 ProxyLivezTotal = metrics.NewCounterVec( 202 &metrics.CounterOpts{ 203 Subsystem: kubeProxySubsystem, 204 Name: "proxy_livez_total", 205 Help: "Cumulative proxy livez HTTP status", 206 StabilityLevel: metrics.ALPHA, 207 }, 208 []string{"code"}, 209 ) 210 211 // SyncProxyRulesLastQueuedTimestamp is the last time a proxy sync was 212 // requested. If this is much larger than 213 // kubeproxy_sync_proxy_rules_last_timestamp_seconds, then something is hung. 214 SyncProxyRulesLastQueuedTimestamp = metrics.NewGauge( 215 &metrics.GaugeOpts{ 216 Subsystem: kubeProxySubsystem, 217 Name: "sync_proxy_rules_last_queued_timestamp_seconds", 218 Help: "The last time a sync of proxy rules was queued", 219 StabilityLevel: metrics.ALPHA, 220 }, 221 ) 222 223 // SyncProxyRulesNoLocalEndpointsTotal is the total number of rules that do 224 // not have an available endpoint. This can be caused by an internal 225 // traffic policy with no available local workload. 226 SyncProxyRulesNoLocalEndpointsTotal = metrics.NewGaugeVec( 227 &metrics.GaugeOpts{ 228 Subsystem: kubeProxySubsystem, 229 Name: "sync_proxy_rules_no_local_endpoints_total", 230 Help: "Number of services with a Local traffic policy and no endpoints", 231 StabilityLevel: metrics.ALPHA, 232 }, 233 []string{"traffic_policy"}, 234 ) 235 ) 236 237 var registerMetricsOnce sync.Once 238 239 // RegisterMetrics registers kube-proxy metrics. 240 func RegisterMetrics() { 241 registerMetricsOnce.Do(func() { 242 legacyregistry.MustRegister(SyncProxyRulesLatency) 243 legacyregistry.MustRegister(SyncFullProxyRulesLatency) 244 legacyregistry.MustRegister(SyncPartialProxyRulesLatency) 245 legacyregistry.MustRegister(SyncProxyRulesLastTimestamp) 246 legacyregistry.MustRegister(NetworkProgrammingLatency) 247 legacyregistry.MustRegister(EndpointChangesPending) 248 legacyregistry.MustRegister(EndpointChangesTotal) 249 legacyregistry.MustRegister(ServiceChangesPending) 250 legacyregistry.MustRegister(ServiceChangesTotal) 251 legacyregistry.MustRegister(IptablesRulesTotal) 252 legacyregistry.MustRegister(IptablesRulesLastSync) 253 legacyregistry.MustRegister(IptablesRestoreFailuresTotal) 254 legacyregistry.MustRegister(IptablesPartialRestoreFailuresTotal) 255 legacyregistry.MustRegister(SyncProxyRulesLastQueuedTimestamp) 256 legacyregistry.MustRegister(SyncProxyRulesNoLocalEndpointsTotal) 257 legacyregistry.MustRegister(ProxyHealthzTotal) 258 legacyregistry.MustRegister(ProxyLivezTotal) 259 260 }) 261 } 262 263 // SinceInSeconds gets the time since the specified start in seconds. 264 func SinceInSeconds(start time.Time) float64 { 265 return time.Since(start).Seconds() 266 }