metrics.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. Copyright 2018 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/volumemanager/cache"
  19. "k8s.io/kubernetes/pkg/volume"
  20. volumeutil "k8s.io/kubernetes/pkg/volume/util"
  21. )
  22. const (
  23. pluginNameNotAvailable = "N/A"
  24. // Metric keys for Volume Manager.
  25. volumeManagerTotalVolumes = "volume_manager_total_volumes"
  26. )
  27. var (
  28. registerMetrics sync.Once
  29. totalVolumesDesc = prometheus.NewDesc(
  30. volumeManagerTotalVolumes,
  31. "Number of volumes in Volume Manager",
  32. []string{"plugin_name", "state"},
  33. nil,
  34. )
  35. )
  36. // volumeCount is a map of maps used as a counter.
  37. type volumeCount map[string]map[string]int64
  38. func (v volumeCount) add(state, plugin string) {
  39. count, ok := v[state]
  40. if !ok {
  41. count = map[string]int64{}
  42. }
  43. count[plugin]++
  44. v[state] = count
  45. }
  46. // Register registers Volume Manager metrics.
  47. func Register(asw cache.ActualStateOfWorld, dsw cache.DesiredStateOfWorld, pluginMgr *volume.VolumePluginMgr) {
  48. registerMetrics.Do(func() {
  49. prometheus.MustRegister(&totalVolumesCollector{asw, dsw, pluginMgr})
  50. })
  51. }
  52. type totalVolumesCollector struct {
  53. asw cache.ActualStateOfWorld
  54. dsw cache.DesiredStateOfWorld
  55. pluginMgr *volume.VolumePluginMgr
  56. }
  57. var _ prometheus.Collector = &totalVolumesCollector{}
  58. // Describe implements the prometheus.Collector interface.
  59. func (c *totalVolumesCollector) Describe(ch chan<- *prometheus.Desc) {
  60. ch <- totalVolumesDesc
  61. }
  62. // Collect implements the prometheus.Collector interface.
  63. func (c *totalVolumesCollector) Collect(ch chan<- prometheus.Metric) {
  64. for stateName, pluginCount := range c.getVolumeCount() {
  65. for pluginName, count := range pluginCount {
  66. metric, err := prometheus.NewConstMetric(totalVolumesDesc,
  67. prometheus.GaugeValue,
  68. float64(count),
  69. pluginName,
  70. stateName)
  71. if err != nil {
  72. klog.Warningf("Failed to create metric : %v", err)
  73. }
  74. ch <- metric
  75. }
  76. }
  77. }
  78. func (c *totalVolumesCollector) getVolumeCount() volumeCount {
  79. counter := make(volumeCount)
  80. for _, mountedVolume := range c.asw.GetMountedVolumes() {
  81. pluginName := volumeutil.GetFullQualifiedPluginNameForVolume(mountedVolume.PluginName, mountedVolume.VolumeSpec)
  82. if pluginName == "" {
  83. pluginName = pluginNameNotAvailable
  84. }
  85. counter.add("actual_state_of_world", pluginName)
  86. }
  87. for _, volumeToMount := range c.dsw.GetVolumesToMount() {
  88. pluginName := pluginNameNotAvailable
  89. if plugin, err := c.pluginMgr.FindPluginBySpec(volumeToMount.VolumeSpec); err == nil {
  90. pluginName = volumeutil.GetFullQualifiedPluginNameForVolume(plugin.GetPluginName(), volumeToMount.VolumeSpec)
  91. }
  92. counter.add("desired_state_of_world", pluginName)
  93. }
  94. return counter
  95. }