metrics.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package metrics
  14. import (
  15. "sync"
  16. "k8s.io/component-base/metrics"
  17. "k8s.io/component-base/metrics/legacyregistry"
  18. "k8s.io/kubernetes/pkg/kubelet/pluginmanager/cache"
  19. )
  20. const (
  21. // Metric keys for Plugin Manager.
  22. pluginManagerTotalPlugins = "plugin_manager_total_plugins"
  23. )
  24. var (
  25. registerMetrics sync.Once
  26. totalPluginsDesc = metrics.NewDesc(
  27. pluginManagerTotalPlugins,
  28. "Number of plugins in Plugin Manager",
  29. []string{"socket_path", "state"},
  30. nil,
  31. metrics.ALPHA,
  32. "",
  33. )
  34. )
  35. // pluginCount is a map of maps used as a counter.
  36. type pluginCount map[string]map[string]int64
  37. func (pc pluginCount) add(state, pluginName string) {
  38. count, ok := pc[state]
  39. if !ok {
  40. count = map[string]int64{}
  41. }
  42. count[pluginName]++
  43. pc[state] = count
  44. }
  45. // Register registers Plugin Manager metrics.
  46. func Register(asw cache.ActualStateOfWorld, dsw cache.DesiredStateOfWorld) {
  47. registerMetrics.Do(func() {
  48. legacyregistry.CustomMustRegister(&totalPluginsCollector{asw: asw, dsw: dsw})
  49. })
  50. }
  51. type totalPluginsCollector struct {
  52. metrics.BaseStableCollector
  53. asw cache.ActualStateOfWorld
  54. dsw cache.DesiredStateOfWorld
  55. }
  56. var _ metrics.StableCollector = &totalPluginsCollector{}
  57. // DescribeWithStability implements the metrics.StableCollector interface.
  58. func (c *totalPluginsCollector) DescribeWithStability(ch chan<- *metrics.Desc) {
  59. ch <- totalPluginsDesc
  60. }
  61. // CollectWithStability implements the metrics.StableCollector interface.
  62. func (c *totalPluginsCollector) CollectWithStability(ch chan<- metrics.Metric) {
  63. for stateName, pluginCount := range c.getPluginCount() {
  64. for socketPath, count := range pluginCount {
  65. ch <- metrics.NewLazyConstMetric(totalPluginsDesc,
  66. metrics.GaugeValue,
  67. float64(count),
  68. socketPath,
  69. stateName)
  70. }
  71. }
  72. }
  73. func (c *totalPluginsCollector) getPluginCount() pluginCount {
  74. counter := make(pluginCount)
  75. for _, registeredPlugin := range c.asw.GetRegisteredPlugins() {
  76. socketPath := registeredPlugin.SocketPath
  77. counter.add("actual_state_of_world", socketPath)
  78. }
  79. for _, pluginToRegister := range c.dsw.GetPluginsToRegister() {
  80. socketPath := pluginToRegister.SocketPath
  81. counter.add("desired_state_of_world", socketPath)
  82. }
  83. return counter
  84. }