namespacemetrics.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package metrics
  2. import (
  3. "github.com/kubecost/cost-model/pkg/clustercache"
  4. "github.com/kubecost/cost-model/pkg/prom"
  5. "github.com/prometheus/client_golang/prometheus"
  6. dto "github.com/prometheus/client_model/go"
  7. )
  8. //--------------------------------------------------------------------------
  9. // KubecostNamespaceCollector
  10. //--------------------------------------------------------------------------
  11. // KubecostNamespaceCollector is a prometheus collector that generates namespace sourced metrics
  12. type KubecostNamespaceCollector struct {
  13. KubeClusterCache clustercache.ClusterCache
  14. }
  15. // Describe sends the super-set of all possible descriptors of metrics
  16. // collected by this Collector.
  17. func (nsac KubecostNamespaceCollector) Describe(ch chan<- *prometheus.Desc) {
  18. ch <- prometheus.NewDesc("kube_namespace_annotations", "namespace annotations", []string{}, nil)
  19. }
  20. // Collect is called by the Prometheus registry when collecting metrics.
  21. func (nsac KubecostNamespaceCollector) Collect(ch chan<- prometheus.Metric) {
  22. namespaces := nsac.KubeClusterCache.GetAllNamespaces()
  23. for _, namespace := range namespaces {
  24. nsName := namespace.GetName()
  25. labels, values := prom.KubeAnnotationsToLabels(namespace.Annotations)
  26. if len(labels) > 0 {
  27. m := newNamespaceAnnotationsMetric("kube_namespace_annotations", nsName, labels, values)
  28. ch <- m
  29. }
  30. }
  31. }
  32. //--------------------------------------------------------------------------
  33. // NamespaceAnnotationsMetric
  34. //--------------------------------------------------------------------------
  35. // NamespaceAnnotationsMetric is a prometheus.Metric used to encode namespace annotations
  36. type NamespaceAnnotationsMetric struct {
  37. fqName string
  38. help string
  39. namespace string
  40. labelNames []string
  41. labelValues []string
  42. }
  43. // Creates a new NamespaceAnnotationsMetric, implementation of prometheus.Metric
  44. func newNamespaceAnnotationsMetric(fqname, namespace string, labelNames []string, labelValues []string) NamespaceAnnotationsMetric {
  45. return NamespaceAnnotationsMetric{
  46. fqName: fqname,
  47. help: "kube_namespace_annotations Namespace Annotations",
  48. namespace: namespace,
  49. labelNames: labelNames,
  50. labelValues: labelValues,
  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 (nam NamespaceAnnotationsMetric) Desc() *prometheus.Desc {
  56. l := prometheus.Labels{
  57. "namespace": nam.namespace,
  58. }
  59. return prometheus.NewDesc(nam.fqName, nam.help, []string{}, l)
  60. }
  61. // Write encodes the Metric into a "Metric" Protocol Buffer data
  62. // transmission object.
  63. func (nam NamespaceAnnotationsMetric) Write(m *dto.Metric) error {
  64. h := float64(1)
  65. m.Gauge = &dto.Gauge{
  66. Value: &h,
  67. }
  68. var labels []*dto.LabelPair
  69. for i := range nam.labelNames {
  70. labels = append(labels, &dto.LabelPair{
  71. Name: &nam.labelNames[i],
  72. Value: &nam.labelValues[i],
  73. })
  74. }
  75. labels = append(labels, &dto.LabelPair{
  76. Name: toStringPtr("namespace"),
  77. Value: &nam.namespace,
  78. })
  79. m.Label = labels
  80. return nil
  81. }
  82. //--------------------------------------------------------------------------
  83. // KubeNamespaceCollector
  84. //--------------------------------------------------------------------------
  85. // KubeNamespaceCollector is a prometheus collector that generates namespace sourced metrics
  86. type KubeNamespaceCollector struct {
  87. KubeClusterCache clustercache.ClusterCache
  88. }
  89. // Describe sends the super-set of all possible descriptors of metrics
  90. // collected by this Collector.
  91. func (nsac KubeNamespaceCollector) Describe(ch chan<- *prometheus.Desc) {
  92. ch <- prometheus.NewDesc("kube_namespace_labels", "namespace labels", []string{}, nil)
  93. }
  94. // Collect is called by the Prometheus registry when collecting metrics.
  95. func (nsac KubeNamespaceCollector) Collect(ch chan<- prometheus.Metric) {
  96. namespaces := nsac.KubeClusterCache.GetAllNamespaces()
  97. for _, namespace := range namespaces {
  98. nsName := namespace.GetName()
  99. labels, values := prom.KubeLabelsToLabels(namespace.Labels)
  100. if len(labels) > 0 {
  101. m := newNamespaceAnnotationsMetric("kube_namespace_labels", nsName, labels, values)
  102. ch <- m
  103. }
  104. }
  105. }
  106. //--------------------------------------------------------------------------
  107. // NamespaceAnnotationsMetric
  108. //--------------------------------------------------------------------------
  109. // NamespaceAnnotationsMetric is a prometheus.Metric used to encode namespace annotations
  110. type KubeNamespaceLabelsMetric struct {
  111. fqName string
  112. help string
  113. namespace string
  114. labelNames []string
  115. labelValues []string
  116. }
  117. // Creates a new KubeNamespaceLabelsMetric, implementation of prometheus.Metric
  118. func newKubeNamespaceLabelsMetric(fqname, namespace string, labelNames []string, labelValues []string) KubeNamespaceLabelsMetric {
  119. return KubeNamespaceLabelsMetric{
  120. namespace: namespace,
  121. fqName: fqname,
  122. labelNames: labelNames,
  123. labelValues: labelValues,
  124. help: "kube_namespace_labels Namespace Labels",
  125. }
  126. }
  127. // Desc returns the descriptor for the Metric. This method idempotently
  128. // returns the same descriptor throughout the lifetime of the Metric.
  129. func (nam KubeNamespaceLabelsMetric) Desc() *prometheus.Desc {
  130. l := prometheus.Labels{
  131. "namespace": nam.namespace,
  132. }
  133. return prometheus.NewDesc(nam.fqName, nam.help, []string{}, l)
  134. }
  135. // Write encodes the Metric into a "Metric" Protocol Buffer data transmission object.
  136. func (nam KubeNamespaceLabelsMetric) Write(m *dto.Metric) error {
  137. h := float64(1)
  138. m.Gauge = &dto.Gauge{
  139. Value: &h,
  140. }
  141. var labels []*dto.LabelPair
  142. for i := range nam.labelNames {
  143. labels = append(labels, &dto.LabelPair{
  144. Name: &nam.labelNames[i],
  145. Value: &nam.labelValues[i],
  146. })
  147. }
  148. labels = append(labels, &dto.LabelPair{
  149. Name: toStringPtr("namespace"),
  150. Value: &nam.namespace,
  151. })
  152. m.Label = labels
  153. return nil
  154. }