k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/registry/core/service/ipallocator/metrics.go (about) 1 /* 2 Copyright 2021 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 ipallocator 18 19 import ( 20 "sync" 21 22 "k8s.io/component-base/metrics" 23 "k8s.io/component-base/metrics/legacyregistry" 24 ) 25 26 const ( 27 namespace = "kube_apiserver" 28 subsystem = "clusterip_allocator" 29 ) 30 31 var ( 32 // clusterIPAllocated indicates the amount of cluster IP allocated by Service CIDR. 33 clusterIPAllocated = metrics.NewGaugeVec( 34 &metrics.GaugeOpts{ 35 Namespace: namespace, 36 Subsystem: subsystem, 37 Name: "allocated_ips", 38 Help: "Gauge measuring the number of allocated IPs for Services", 39 StabilityLevel: metrics.ALPHA, 40 }, 41 []string{"cidr"}, 42 ) 43 // clusterIPAvailable indicates the amount of cluster IP available by Service CIDR. 44 clusterIPAvailable = metrics.NewGaugeVec( 45 &metrics.GaugeOpts{ 46 Namespace: namespace, 47 Subsystem: subsystem, 48 Name: "available_ips", 49 Help: "Gauge measuring the number of available IPs for Services", 50 StabilityLevel: metrics.ALPHA, 51 }, 52 []string{"cidr"}, 53 ) 54 // clusterIPAllocation counts the total number of ClusterIP allocation and allocation mode: static or dynamic. 55 clusterIPAllocations = metrics.NewCounterVec( 56 &metrics.CounterOpts{ 57 Namespace: namespace, 58 Subsystem: subsystem, 59 Name: "allocation_total", 60 Help: "Number of Cluster IPs allocations", 61 StabilityLevel: metrics.ALPHA, 62 }, 63 []string{"cidr", "scope"}, 64 ) 65 // clusterIPAllocationErrors counts the number of error trying to allocate a ClusterIP and allocation mode: static or dynamic. 66 clusterIPAllocationErrors = metrics.NewCounterVec( 67 &metrics.CounterOpts{ 68 Namespace: namespace, 69 Subsystem: subsystem, 70 Name: "allocation_errors_total", 71 Help: "Number of errors trying to allocate Cluster IPs", 72 StabilityLevel: metrics.ALPHA, 73 }, 74 []string{"cidr", "scope"}, 75 ) 76 ) 77 78 var registerMetricsOnce sync.Once 79 80 func registerMetrics() { 81 registerMetricsOnce.Do(func() { 82 legacyregistry.MustRegister(clusterIPAllocated) 83 legacyregistry.MustRegister(clusterIPAvailable) 84 legacyregistry.MustRegister(clusterIPAllocations) 85 legacyregistry.MustRegister(clusterIPAllocationErrors) 86 }) 87 } 88 89 // metricsRecorderInterface is the interface to record metrics. 90 type metricsRecorderInterface interface { 91 setAllocated(cidr string, allocated int) 92 setAvailable(cidr string, available int) 93 incrementAllocations(cidr, scope string) 94 incrementAllocationErrors(cidr, scope string) 95 } 96 97 // metricsRecorder implements metricsRecorderInterface. 98 type metricsRecorder struct{} 99 100 func (m *metricsRecorder) setAllocated(cidr string, allocated int) { 101 clusterIPAllocated.WithLabelValues(cidr).Set(float64(allocated)) 102 } 103 104 func (m *metricsRecorder) setAvailable(cidr string, available int) { 105 clusterIPAvailable.WithLabelValues(cidr).Set(float64(available)) 106 } 107 108 func (m *metricsRecorder) incrementAllocations(cidr, scope string) { 109 clusterIPAllocations.WithLabelValues(cidr, scope).Inc() 110 } 111 112 func (m *metricsRecorder) incrementAllocationErrors(cidr, scope string) { 113 clusterIPAllocationErrors.WithLabelValues(cidr, scope).Inc() 114 } 115 116 // emptyMetricsRecorder is a null object implements metricsRecorderInterface. 117 type emptyMetricsRecorder struct{} 118 119 func (*emptyMetricsRecorder) setAllocated(cidr string, allocated int) {} 120 func (*emptyMetricsRecorder) setAvailable(cidr string, available int) {} 121 func (*emptyMetricsRecorder) incrementAllocations(cidr, scope string) {} 122 func (*emptyMetricsRecorder) incrementAllocationErrors(cidr, scope string) {}