pvcmetrics.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. // KubePVCCollector
  10. //--------------------------------------------------------------------------
  11. // KubePVCCollector is a prometheus collector that generates pvc sourced metrics
  12. type KubePVCCollector struct {
  13. KubeClusterCache clustercache.ClusterCache
  14. metricsConfig MetricsConfig
  15. }
  16. // Describe sends the super-set of all possible descriptors of metrics collected by this Collector.
  17. func (kpvc KubePVCCollector) Describe(ch chan<- *prometheus.Desc) {
  18. disabledMetrics := kpvc.metricsConfig.GetDisabledMetricsMap()
  19. if _, disabled := disabledMetrics["kube_persistentvolumeclaim_resource_requests_storage_bytes"]; !disabled {
  20. ch <- prometheus.NewDesc("kube_persistentvolumeclaim_resource_requests_storage_bytes", "The pvc storage resource requests in bytes", []string{}, nil)
  21. }
  22. if _, disabled := disabledMetrics["kube_persistentvolumeclaim_info"]; !disabled {
  23. ch <- prometheus.NewDesc("kube_persistentvolumeclaim_info", "The pvc storage resource requests in bytes", []string{}, nil)
  24. }
  25. }
  26. // Collect is called by the Prometheus registry when collecting metrics.
  27. func (kpvc KubePVCCollector) Collect(ch chan<- prometheus.Metric) {
  28. pvcs := kpvc.KubeClusterCache.GetAllPersistentVolumeClaims()
  29. disabledMetrics := kpvc.metricsConfig.GetDisabledMetricsMap()
  30. for _, pvc := range pvcs {
  31. storageClass := getPersistentVolumeClaimClass(pvc)
  32. volume := pvc.Spec.VolumeName
  33. if _, disabled := disabledMetrics["kube_persistentvolumeclaim_info"]; !disabled {
  34. ch <- newKubePVCInfoMetric("kube_persistentvolumeclaim_info", pvc.Name, pvc.Namespace, storageClass, volume)
  35. }
  36. if storage, ok := pvc.Spec.Resources.Requests[v1.ResourceStorage]; ok {
  37. if _, disabled := disabledMetrics["kube_persistentvolumeclaim_resource_requests_storage_bytes"]; !disabled {
  38. ch <- newKubePVCResourceRequestsStorageBytesMetric("kube_persistentvolumeclaim_resource_requests_storage_bytes", pvc.Name, pvc.Namespace, float64(storage.Value()))
  39. }
  40. }
  41. }
  42. }
  43. //--------------------------------------------------------------------------
  44. // KubePVCResourceRequestsStorageBytesMetric
  45. //--------------------------------------------------------------------------
  46. // KubePVCResourceRequestsStorageBytesMetric is a prometheus.Metric
  47. type KubePVCResourceRequestsStorageBytesMetric struct {
  48. fqName string
  49. help string
  50. namespace string
  51. pvc string
  52. value float64
  53. }
  54. // Creates a new KubePVCResourceRequestsStorageBytesMetric, implementation of prometheus.Metric
  55. func newKubePVCResourceRequestsStorageBytesMetric(fqname, pvc, namespace string, value float64) KubePVCResourceRequestsStorageBytesMetric {
  56. return KubePVCResourceRequestsStorageBytesMetric{
  57. fqName: fqname,
  58. help: "kube_persistentvolumeclaim_resource_requests_storage_bytes pvc storage resource requests in bytes",
  59. pvc: pvc,
  60. namespace: namespace,
  61. value: value,
  62. }
  63. }
  64. // Desc returns the descriptor for the Metric. This method idempotently
  65. // returns the same descriptor throughout the lifetime of the Metric.
  66. func (kpvcrr KubePVCResourceRequestsStorageBytesMetric) Desc() *prometheus.Desc {
  67. l := prometheus.Labels{
  68. "persistentvolumeclaim": kpvcrr.pvc,
  69. "namespace": kpvcrr.namespace,
  70. }
  71. return prometheus.NewDesc(kpvcrr.fqName, kpvcrr.help, []string{}, l)
  72. }
  73. // Write encodes the Metric into a "Metric" Protocol Buffer data
  74. // transmission object.
  75. func (kpvcrr KubePVCResourceRequestsStorageBytesMetric) Write(m *dto.Metric) error {
  76. m.Gauge = &dto.Gauge{
  77. Value: &kpvcrr.value,
  78. }
  79. m.Label = []*dto.LabelPair{
  80. {
  81. Name: toStringPtr("persistentvolumeclaim"),
  82. Value: &kpvcrr.pvc,
  83. },
  84. {
  85. Name: toStringPtr("namespace"),
  86. Value: &kpvcrr.namespace,
  87. },
  88. }
  89. return nil
  90. }
  91. //--------------------------------------------------------------------------
  92. // KubePVCInfoMetric
  93. //--------------------------------------------------------------------------
  94. // KubePVCInfoMetric is a prometheus.Metric
  95. type KubePVCInfoMetric struct {
  96. fqName string
  97. help string
  98. namespace string
  99. pvc string
  100. storageclass string
  101. volume string
  102. }
  103. // Creates a new KubePVCInfoMetric, implementation of prometheus.Metric
  104. func newKubePVCInfoMetric(fqname, pvc, namespace, storageclass, volume string) KubePVCInfoMetric {
  105. return KubePVCInfoMetric{
  106. fqName: fqname,
  107. help: "kube_persistentvolumeclaim_info pvc storage resource requests in bytes",
  108. pvc: pvc,
  109. namespace: namespace,
  110. storageclass: storageclass,
  111. volume: volume,
  112. }
  113. }
  114. // Desc returns the descriptor for the Metric. This method idempotently
  115. // returns the same descriptor throughout the lifetime of the Metric.
  116. func (kpvcrr KubePVCInfoMetric) Desc() *prometheus.Desc {
  117. l := prometheus.Labels{
  118. "persistentvolumeclaim": kpvcrr.pvc,
  119. "namespace": kpvcrr.namespace,
  120. "storageclass": kpvcrr.storageclass,
  121. "volumename": kpvcrr.volume,
  122. }
  123. return prometheus.NewDesc(kpvcrr.fqName, kpvcrr.help, []string{}, l)
  124. }
  125. // Write encodes the Metric into a "Metric" Protocol Buffer data
  126. // transmission object.
  127. func (kpvci KubePVCInfoMetric) Write(m *dto.Metric) error {
  128. v := float64(1.0)
  129. m.Gauge = &dto.Gauge{
  130. Value: &v,
  131. }
  132. m.Label = []*dto.LabelPair{
  133. {
  134. Name: toStringPtr("namespace"),
  135. Value: &kpvci.namespace,
  136. },
  137. {
  138. Name: toStringPtr("persistentvolumeclaim"),
  139. Value: &kpvci.pvc,
  140. },
  141. {
  142. Name: toStringPtr("storageclass"),
  143. Value: &kpvci.storageclass,
  144. },
  145. {
  146. Name: toStringPtr("volumename"),
  147. Value: &kpvci.volume,
  148. },
  149. }
  150. return nil
  151. }