metrics.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. "github.com/prometheus/client_golang/prometheus"
  17. "k8s.io/klog"
  18. "k8s.io/kubernetes/pkg/kubelet/pluginmanager/cache"
  19. )
  20. const (
  21. pluginNameNotAvailable = "N/A"
  22. // Metric keys for Plugin Manager.
  23. pluginManagerTotalPlugins = "plugin_manager_total_plugins"
  24. )
  25. var (
  26. registerMetrics sync.Once
  27. totalPluginsDesc = prometheus.NewDesc(
  28. pluginManagerTotalPlugins,
  29. "Number of plugins in Plugin Manager",
  30. []string{"socket_path", "state"},
  31. nil,
  32. )
  33. )
  34. // pluginCount is a map of maps used as a counter.
  35. type pluginCount map[string]map[string]int64
  36. func (pc pluginCount) add(state, pluginName string) {
  37. count, ok := pc[state]
  38. if !ok {
  39. count = map[string]int64{}
  40. }
  41. count[pluginName]++
  42. pc[state] = count
  43. }
  44. // Register registers Plugin Manager metrics.
  45. func Register(asw cache.ActualStateOfWorld, dsw cache.DesiredStateOfWorld) {
  46. registerMetrics.Do(func() {
  47. prometheus.MustRegister(&totalPluginsCollector{asw, dsw})
  48. })
  49. }
  50. type totalPluginsCollector struct {
  51. asw cache.ActualStateOfWorld
  52. dsw cache.DesiredStateOfWorld
  53. }
  54. var _ prometheus.Collector = &totalPluginsCollector{}
  55. // Describe implements the prometheus.Collector interface.
  56. func (c *totalPluginsCollector) Describe(ch chan<- *prometheus.Desc) {
  57. ch <- totalPluginsDesc
  58. }
  59. // Collect implements the prometheus.Collector interface.
  60. func (c *totalPluginsCollector) Collect(ch chan<- prometheus.Metric) {
  61. for stateName, pluginCount := range c.getPluginCount() {
  62. for socketPath, count := range pluginCount {
  63. metric, err := prometheus.NewConstMetric(totalPluginsDesc,
  64. prometheus.GaugeValue,
  65. float64(count),
  66. socketPath,
  67. stateName)
  68. if err != nil {
  69. klog.Warningf("Failed to create metric : %v", err)
  70. }
  71. ch <- metric
  72. }
  73. }
  74. }
  75. func (c *totalPluginsCollector) getPluginCount() pluginCount {
  76. counter := make(pluginCount)
  77. for _, registeredPlugin := range c.asw.GetRegisteredPlugins() {
  78. socketPath := registeredPlugin.SocketPath
  79. counter.add("actual_state_of_world", socketPath)
  80. }
  81. for _, pluginToRegister := range c.dsw.GetPluginsToRegister() {
  82. socketPath := pluginToRegister.SocketPath
  83. counter.add("desired_state_of_world", socketPath)
  84. }
  85. return counter
  86. }