pvcmetrics.go 5.1 KB

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