123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- package metrics
- import (
- "sync"
- "k8s.io/component-base/metrics"
- "k8s.io/component-base/metrics/legacyregistry"
- "k8s.io/kubernetes/pkg/kubelet/pluginmanager/cache"
- )
- const (
-
- pluginManagerTotalPlugins = "plugin_manager_total_plugins"
- )
- var (
- registerMetrics sync.Once
- totalPluginsDesc = metrics.NewDesc(
- pluginManagerTotalPlugins,
- "Number of plugins in Plugin Manager",
- []string{"socket_path", "state"},
- nil,
- metrics.ALPHA,
- "",
- )
- )
- type pluginCount map[string]map[string]int64
- func (pc pluginCount) add(state, pluginName string) {
- count, ok := pc[state]
- if !ok {
- count = map[string]int64{}
- }
- count[pluginName]++
- pc[state] = count
- }
- func Register(asw cache.ActualStateOfWorld, dsw cache.DesiredStateOfWorld) {
- registerMetrics.Do(func() {
- legacyregistry.CustomMustRegister(&totalPluginsCollector{asw: asw, dsw: dsw})
- })
- }
- type totalPluginsCollector struct {
- metrics.BaseStableCollector
- asw cache.ActualStateOfWorld
- dsw cache.DesiredStateOfWorld
- }
- var _ metrics.StableCollector = &totalPluginsCollector{}
- func (c *totalPluginsCollector) DescribeWithStability(ch chan<- *metrics.Desc) {
- ch <- totalPluginsDesc
- }
- func (c *totalPluginsCollector) CollectWithStability(ch chan<- metrics.Metric) {
- for stateName, pluginCount := range c.getPluginCount() {
- for socketPath, count := range pluginCount {
- ch <- metrics.NewLazyConstMetric(totalPluginsDesc,
- metrics.GaugeValue,
- float64(count),
- socketPath,
- stateName)
- }
- }
- }
- func (c *totalPluginsCollector) getPluginCount() pluginCount {
- counter := make(pluginCount)
- for _, registeredPlugin := range c.asw.GetRegisteredPlugins() {
- socketPath := registeredPlugin.SocketPath
- counter.add("actual_state_of_world", socketPath)
- }
- for _, pluginToRegister := range c.dsw.GetPluginsToRegister() {
- socketPath := pluginToRegister.SocketPath
- counter.add("desired_state_of_world", socketPath)
- }
- return counter
- }
|