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