k8s.io/kubernetes@v1.29.3/pkg/kubelet/pluginmanager/metrics/metrics.go (about) 1 /* 2 Copyright 2019 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 22 "k8s.io/component-base/metrics" 23 "k8s.io/component-base/metrics/legacyregistry" 24 "k8s.io/kubernetes/pkg/kubelet/pluginmanager/cache" 25 ) 26 27 const ( 28 // Metric keys for Plugin Manager. 29 pluginManagerTotalPlugins = "plugin_manager_total_plugins" 30 ) 31 32 var ( 33 registerMetrics sync.Once 34 35 totalPluginsDesc = metrics.NewDesc( 36 pluginManagerTotalPlugins, 37 "Number of plugins in Plugin Manager", 38 []string{"socket_path", "state"}, 39 nil, 40 metrics.ALPHA, 41 "", 42 ) 43 ) 44 45 // pluginCount is a map of maps used as a counter. 46 type pluginCount map[string]map[string]int64 47 48 func (pc pluginCount) add(state, pluginName string) { 49 count, ok := pc[state] 50 if !ok { 51 count = map[string]int64{} 52 } 53 count[pluginName]++ 54 pc[state] = count 55 } 56 57 // Register registers Plugin Manager metrics. 58 func Register(asw cache.ActualStateOfWorld, dsw cache.DesiredStateOfWorld) { 59 registerMetrics.Do(func() { 60 legacyregistry.CustomMustRegister(&totalPluginsCollector{asw: asw, dsw: dsw}) 61 }) 62 } 63 64 type totalPluginsCollector struct { 65 metrics.BaseStableCollector 66 67 asw cache.ActualStateOfWorld 68 dsw cache.DesiredStateOfWorld 69 } 70 71 var _ metrics.StableCollector = &totalPluginsCollector{} 72 73 // DescribeWithStability implements the metrics.StableCollector interface. 74 func (c *totalPluginsCollector) DescribeWithStability(ch chan<- *metrics.Desc) { 75 ch <- totalPluginsDesc 76 } 77 78 // CollectWithStability implements the metrics.StableCollector interface. 79 func (c *totalPluginsCollector) CollectWithStability(ch chan<- metrics.Metric) { 80 for stateName, pluginCount := range c.getPluginCount() { 81 for socketPath, count := range pluginCount { 82 ch <- metrics.NewLazyConstMetric(totalPluginsDesc, 83 metrics.GaugeValue, 84 float64(count), 85 socketPath, 86 stateName) 87 } 88 } 89 } 90 91 func (c *totalPluginsCollector) getPluginCount() pluginCount { 92 counter := make(pluginCount) 93 for _, registeredPlugin := range c.asw.GetRegisteredPlugins() { 94 socketPath := registeredPlugin.SocketPath 95 counter.add("actual_state_of_world", socketPath) 96 } 97 98 for _, pluginToRegister := range c.dsw.GetPluginsToRegister() { 99 socketPath := pluginToRegister.SocketPath 100 counter.add("desired_state_of_world", socketPath) 101 } 102 return counter 103 }