namespacemetrics.go 6.4 KB

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