2
0

pvcmetrics.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package metrics
  2. import (
  3. "github.com/opencost/opencost/core/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. pvcUID := string(pvc.UID)
  34. if _, disabled := disabledMetrics["kube_persistentvolumeclaim_info"]; !disabled {
  35. ch <- newKubePVCInfoMetric("kube_persistentvolumeclaim_info", pvc.Name, pvc.Namespace, pvcUID, storageClass, volume)
  36. }
  37. if storage, ok := pvc.Spec.Resources.Requests[v1.ResourceStorage]; ok {
  38. if _, disabled := disabledMetrics["kube_persistentvolumeclaim_resource_requests_storage_bytes"]; !disabled {
  39. ch <- newKubePVCResourceRequestsStorageBytesMetric("kube_persistentvolumeclaim_resource_requests_storage_bytes", pvc.Name, pvc.Namespace, pvcUID, float64(storage.Value()))
  40. }
  41. }
  42. }
  43. }
  44. //--------------------------------------------------------------------------
  45. // KubePVCResourceRequestsStorageBytesMetric
  46. //--------------------------------------------------------------------------
  47. // KubePVCResourceRequestsStorageBytesMetric is a prometheus.Metric
  48. type KubePVCResourceRequestsStorageBytesMetric struct {
  49. fqName string
  50. help string
  51. namespace string
  52. pvc string
  53. value float64
  54. uid string
  55. }
  56. // Creates a new KubePVCResourceRequestsStorageBytesMetric, implementation of prometheus.Metric
  57. func newKubePVCResourceRequestsStorageBytesMetric(fqname, pvc, namespace, uid string, value float64) KubePVCResourceRequestsStorageBytesMetric {
  58. return KubePVCResourceRequestsStorageBytesMetric{
  59. fqName: fqname,
  60. help: "kube_persistentvolumeclaim_resource_requests_storage_bytes pvc storage resource requests in bytes",
  61. pvc: pvc,
  62. namespace: namespace,
  63. value: value,
  64. uid: uid,
  65. }
  66. }
  67. // Desc returns the descriptor for the Metric. This method idempotently
  68. // returns the same descriptor throughout the lifetime of the Metric.
  69. func (kpvcrr KubePVCResourceRequestsStorageBytesMetric) Desc() *prometheus.Desc {
  70. l := prometheus.Labels{
  71. "persistentvolumeclaim": kpvcrr.pvc,
  72. "namespace": kpvcrr.namespace,
  73. "uid": kpvcrr.uid,
  74. }
  75. return prometheus.NewDesc(kpvcrr.fqName, kpvcrr.help, []string{}, l)
  76. }
  77. // Write encodes the Metric into a "Metric" Protocol Buffer data
  78. // transmission object.
  79. func (kpvcrr KubePVCResourceRequestsStorageBytesMetric) Write(m *dto.Metric) error {
  80. m.Gauge = &dto.Gauge{
  81. Value: &kpvcrr.value,
  82. }
  83. m.Label = []*dto.LabelPair{
  84. {
  85. Name: toStringPtr("persistentvolumeclaim"),
  86. Value: &kpvcrr.pvc,
  87. },
  88. {
  89. Name: toStringPtr("namespace"),
  90. Value: &kpvcrr.namespace,
  91. },
  92. {
  93. Name: toStringPtr("uid"),
  94. Value: &kpvcrr.uid,
  95. },
  96. }
  97. return nil
  98. }
  99. //--------------------------------------------------------------------------
  100. // KubePVCInfoMetric
  101. //--------------------------------------------------------------------------
  102. // KubePVCInfoMetric is a prometheus.Metric
  103. type KubePVCInfoMetric struct {
  104. fqName string
  105. help string
  106. namespace string
  107. pvc string
  108. storageclass string
  109. volume string
  110. uid string
  111. }
  112. // Creates a new KubePVCInfoMetric, implementation of prometheus.Metric
  113. func newKubePVCInfoMetric(fqname, pvc, namespace, uid, storageclass, volume string) KubePVCInfoMetric {
  114. return KubePVCInfoMetric{
  115. fqName: fqname,
  116. help: "kube_persistentvolumeclaim_info pvc storage resource requests in bytes",
  117. pvc: pvc,
  118. namespace: namespace,
  119. storageclass: storageclass,
  120. volume: volume,
  121. uid: uid,
  122. }
  123. }
  124. // Desc returns the descriptor for the Metric. This method idempotently
  125. // returns the same descriptor throughout the lifetime of the Metric.
  126. func (kpvcrr KubePVCInfoMetric) Desc() *prometheus.Desc {
  127. l := prometheus.Labels{
  128. "persistentvolumeclaim": kpvcrr.pvc,
  129. "namespace": kpvcrr.namespace,
  130. "storageclass": kpvcrr.storageclass,
  131. "volumename": kpvcrr.volume,
  132. "uid": kpvcrr.uid,
  133. }
  134. return prometheus.NewDesc(kpvcrr.fqName, kpvcrr.help, []string{}, l)
  135. }
  136. // Write encodes the Metric into a "Metric" Protocol Buffer data
  137. // transmission object.
  138. func (kpvci KubePVCInfoMetric) Write(m *dto.Metric) error {
  139. v := float64(1.0)
  140. m.Gauge = &dto.Gauge{
  141. Value: &v,
  142. }
  143. m.Label = []*dto.LabelPair{
  144. {
  145. Name: toStringPtr("namespace"),
  146. Value: &kpvci.namespace,
  147. },
  148. {
  149. Name: toStringPtr("persistentvolumeclaim"),
  150. Value: &kpvci.pvc,
  151. },
  152. {
  153. Name: toStringPtr("storageclass"),
  154. Value: &kpvci.storageclass,
  155. },
  156. {
  157. Name: toStringPtr("volumename"),
  158. Value: &kpvci.volume,
  159. },
  160. {
  161. Name: toStringPtr("uid"),
  162. Value: &kpvci.uid,
  163. },
  164. }
  165. return nil
  166. }