pvmetrics.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package metrics
  2. import (
  3. "github.com/opencost/opencost/pkg/clustercache"
  4. "github.com/prometheus/client_golang/prometheus"
  5. dto "github.com/prometheus/client_model/go"
  6. v1 "k8s.io/api/core/v1"
  7. )
  8. //--------------------------------------------------------------------------
  9. // KubePVCollector
  10. //--------------------------------------------------------------------------
  11. // KubePVCollector is a prometheus collector that generates PV metrics
  12. type KubePVCollector struct {
  13. KubeClusterCache clustercache.ClusterCache
  14. metricsConfig MetricsConfig
  15. }
  16. // Describe sends the super-set of all possible descriptors of metrics
  17. // collected by this Collector.
  18. func (kpvcb KubePVCollector) Describe(ch chan<- *prometheus.Desc) {
  19. disabledMetrics := kpvcb.metricsConfig.GetDisabledMetricsMap()
  20. if _, disabled := disabledMetrics["kube_persistentvolume_capacity_bytes"]; !disabled {
  21. ch <- prometheus.NewDesc("kube_persistentvolume_capacity_bytes", "The pv storage capacity in bytes", []string{}, nil)
  22. }
  23. if _, disabled := disabledMetrics["kube_persistentvolume_status_phase"]; !disabled {
  24. ch <- prometheus.NewDesc("kube_persistentvolume_status_phase", "The phase indicates if a volume is available, bound to a claim, or released by a claim.", []string{}, nil)
  25. }
  26. }
  27. // Collect is called by the Prometheus registry when collecting metrics.
  28. func (kpvcb KubePVCollector) Collect(ch chan<- prometheus.Metric) {
  29. pvs := kpvcb.KubeClusterCache.GetAllPersistentVolumes()
  30. disabledMetrics := kpvcb.metricsConfig.GetDisabledMetricsMap()
  31. for _, pv := range pvs {
  32. if _, disabled := disabledMetrics["kube_persistentvolume_status_phase"]; !disabled {
  33. phase := pv.Status.Phase
  34. if phase != "" {
  35. phases := []struct {
  36. v bool
  37. n string
  38. }{
  39. {phase == v1.VolumePending, string(v1.VolumePending)},
  40. {phase == v1.VolumeAvailable, string(v1.VolumeAvailable)},
  41. {phase == v1.VolumeBound, string(v1.VolumeBound)},
  42. {phase == v1.VolumeReleased, string(v1.VolumeReleased)},
  43. {phase == v1.VolumeFailed, string(v1.VolumeFailed)},
  44. }
  45. for _, p := range phases {
  46. ch <- newKubePVStatusPhaseMetric("kube_persistentvolume_status_phase", pv.Name, p.n, boolFloat64(p.v))
  47. }
  48. }
  49. }
  50. if _, disabled := disabledMetrics["kube_persistentvolume_capacity_bytes"]; !disabled {
  51. storage := pv.Spec.Capacity[v1.ResourceStorage]
  52. m := newKubePVCapacityBytesMetric("kube_persistentvolume_capacity_bytes", pv.Name, float64(storage.Value()))
  53. ch <- m
  54. }
  55. }
  56. }
  57. //--------------------------------------------------------------------------
  58. // KubePVCapacityBytesMetric
  59. //--------------------------------------------------------------------------
  60. // KubePVCapacityBytesMetric is a prometheus.Metric
  61. type KubePVCapacityBytesMetric struct {
  62. fqName string
  63. help string
  64. pv string
  65. value float64
  66. }
  67. // Creates a new KubePVCapacityBytesMetric, implementation of prometheus.Metric
  68. func newKubePVCapacityBytesMetric(fqname, pv string, value float64) KubePVCapacityBytesMetric {
  69. return KubePVCapacityBytesMetric{
  70. fqName: fqname,
  71. help: "kube_persistentvolume_capacity_bytes pv storage capacity in bytes",
  72. pv: pv,
  73. value: value,
  74. }
  75. }
  76. // Desc returns the descriptor for the Metric. This method idempotently
  77. // returns the same descriptor throughout the lifetime of the Metric.
  78. func (kpcrr KubePVCapacityBytesMetric) Desc() *prometheus.Desc {
  79. l := prometheus.Labels{
  80. "persistentvolume": kpcrr.pv,
  81. }
  82. return prometheus.NewDesc(kpcrr.fqName, kpcrr.help, []string{}, l)
  83. }
  84. // Write encodes the Metric into a "Metric" Protocol Buffer data
  85. // transmission object.
  86. func (kpcrr KubePVCapacityBytesMetric) Write(m *dto.Metric) error {
  87. m.Gauge = &dto.Gauge{
  88. Value: &kpcrr.value,
  89. }
  90. m.Label = []*dto.LabelPair{
  91. {
  92. Name: toStringPtr("persistentvolume"),
  93. Value: &kpcrr.pv,
  94. },
  95. }
  96. return nil
  97. }
  98. //--------------------------------------------------------------------------
  99. // KubePVStatusPhaseMetric
  100. //--------------------------------------------------------------------------
  101. // KubePVStatusPhaseMetric is a prometheus.Metric
  102. type KubePVStatusPhaseMetric struct {
  103. fqName string
  104. help string
  105. pv string
  106. phase string
  107. value float64
  108. }
  109. // Creates a new KubePVCapacityBytesMetric, implementation of prometheus.Metric
  110. func newKubePVStatusPhaseMetric(fqname, pv, phase string, value float64) KubePVStatusPhaseMetric {
  111. return KubePVStatusPhaseMetric{
  112. fqName: fqname,
  113. help: "kube_persistentvolume_status_phase pv status phase",
  114. pv: pv,
  115. phase: phase,
  116. value: value,
  117. }
  118. }
  119. // Desc returns the descriptor for the Metric. This method idempotently
  120. // returns the same descriptor throughout the lifetime of the Metric.
  121. func (kpcrr KubePVStatusPhaseMetric) Desc() *prometheus.Desc {
  122. l := prometheus.Labels{
  123. "persistentvolume": kpcrr.pv,
  124. "phase": kpcrr.phase,
  125. }
  126. return prometheus.NewDesc(kpcrr.fqName, kpcrr.help, []string{}, l)
  127. }
  128. // Write encodes the Metric into a "Metric" Protocol Buffer data
  129. // transmission object.
  130. func (kpcrr KubePVStatusPhaseMetric) Write(m *dto.Metric) error {
  131. m.Gauge = &dto.Gauge{
  132. Value: &kpcrr.value,
  133. }
  134. m.Label = []*dto.LabelPair{
  135. {
  136. Name: toStringPtr("persistentvolume"),
  137. Value: &kpcrr.pv,
  138. },
  139. {
  140. Name: toStringPtr("phase"),
  141. Value: &kpcrr.phase,
  142. },
  143. }
  144. return nil
  145. }