metrics.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. "k8s.io/component-base/metrics"
  17. "k8s.io/component-base/metrics/legacyregistry"
  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 = metrics.NewDesc(
  30. volumeManagerTotalVolumes,
  31. "Number of volumes in Volume Manager",
  32. []string{"plugin_name", "state"},
  33. nil,
  34. metrics.ALPHA, "",
  35. )
  36. )
  37. // volumeCount is a map of maps used as a counter.
  38. type volumeCount map[string]map[string]int64
  39. func (v volumeCount) add(state, plugin string) {
  40. count, ok := v[state]
  41. if !ok {
  42. count = map[string]int64{}
  43. }
  44. count[plugin]++
  45. v[state] = count
  46. }
  47. // Register registers Volume Manager metrics.
  48. func Register(asw cache.ActualStateOfWorld, dsw cache.DesiredStateOfWorld, pluginMgr *volume.VolumePluginMgr) {
  49. registerMetrics.Do(func() {
  50. legacyregistry.CustomMustRegister(&totalVolumesCollector{asw: asw, dsw: dsw, pluginMgr: pluginMgr})
  51. })
  52. }
  53. type totalVolumesCollector struct {
  54. metrics.BaseStableCollector
  55. asw cache.ActualStateOfWorld
  56. dsw cache.DesiredStateOfWorld
  57. pluginMgr *volume.VolumePluginMgr
  58. }
  59. var _ metrics.StableCollector = &totalVolumesCollector{}
  60. // DescribeWithStability implements the metrics.StableCollector interface.
  61. func (c *totalVolumesCollector) DescribeWithStability(ch chan<- *metrics.Desc) {
  62. ch <- totalVolumesDesc
  63. }
  64. // CollectWithStability implements the metrics.StableCollector interface.
  65. func (c *totalVolumesCollector) CollectWithStability(ch chan<- metrics.Metric) {
  66. for stateName, pluginCount := range c.getVolumeCount() {
  67. for pluginName, count := range pluginCount {
  68. ch <- metrics.NewLazyConstMetric(totalVolumesDesc,
  69. metrics.GaugeValue,
  70. float64(count),
  71. pluginName,
  72. stateName)
  73. }
  74. }
  75. }
  76. func (c *totalVolumesCollector) getVolumeCount() volumeCount {
  77. counter := make(volumeCount)
  78. for _, mountedVolume := range c.asw.GetMountedVolumes() {
  79. pluginName := volumeutil.GetFullQualifiedPluginNameForVolume(mountedVolume.PluginName, mountedVolume.VolumeSpec)
  80. if pluginName == "" {
  81. pluginName = pluginNameNotAvailable
  82. }
  83. counter.add("actual_state_of_world", pluginName)
  84. }
  85. for _, volumeToMount := range c.dsw.GetVolumesToMount() {
  86. pluginName := pluginNameNotAvailable
  87. if plugin, err := c.pluginMgr.FindPluginBySpec(volumeToMount.VolumeSpec); err == nil {
  88. pluginName = volumeutil.GetFullQualifiedPluginNameForVolume(plugin.GetPluginName(), volumeToMount.VolumeSpec)
  89. }
  90. counter.add("desired_state_of_world", pluginName)
  91. }
  92. return counter
  93. }