...
1
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
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
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
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
74 func (c *totalPluginsCollector) DescribeWithStability(ch chan<- *metrics.Desc) {
75 ch <- totalPluginsDesc
76 }
77
78
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 }
104
View as plain text