statefulsetmetrics.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // KubecostStatefulsetCollector
  10. //--------------------------------------------------------------------------
  11. // StatefulsetCollector is a prometheus collector that generates StatefulsetMetrics
  12. type KubecostStatefulsetCollector 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 (sc KubecostStatefulsetCollector) Describe(ch chan<- *prometheus.Desc) {
  19. disabledMetrics := sc.metricsConfig.GetDisabledMetricsMap()
  20. if _, disabled := disabledMetrics["statefulSet_match_labels"]; disabled {
  21. return
  22. }
  23. ch <- prometheus.NewDesc("statefulSet_match_labels", "statfulSet match labels", []string{}, nil)
  24. }
  25. // Collect is called by the Prometheus registry when collecting metrics.
  26. func (sc KubecostStatefulsetCollector) Collect(ch chan<- prometheus.Metric) {
  27. disabledMetrics := sc.metricsConfig.GetDisabledMetricsMap()
  28. if _, disabled := disabledMetrics["statefulSet_match_labels"]; disabled {
  29. return
  30. }
  31. ds := sc.KubeClusterCache.GetAllStatefulSets()
  32. for _, statefulset := range ds {
  33. statefulsetName := statefulset.GetName()
  34. statefulsetNS := statefulset.GetNamespace()
  35. labels, values := prom.KubeLabelsToLabels(statefulset.Spec.Selector.MatchLabels)
  36. if len(labels) > 0 {
  37. m := newStatefulsetMatchLabelsMetric(statefulsetName, statefulsetNS, "statefulSet_match_labels", labels, values)
  38. ch <- m
  39. }
  40. }
  41. }
  42. //--------------------------------------------------------------------------
  43. // StatefulsetMatchLabelsMetric
  44. //--------------------------------------------------------------------------
  45. // StatefulsetMetric is a prometheus.Metric used to encode statefulset match labels
  46. type StatefulsetMatchLabelsMetric struct {
  47. fqName string
  48. help string
  49. labelNames []string
  50. labelValues []string
  51. statefulsetName string
  52. namespace string
  53. }
  54. // Creates a new StatefulsetMetric, implementation of prometheus.Metric
  55. func newStatefulsetMatchLabelsMetric(name, namespace, fqname string, labelNames, labelvalues []string) StatefulsetMatchLabelsMetric {
  56. return StatefulsetMatchLabelsMetric{
  57. fqName: fqname,
  58. labelNames: labelNames,
  59. labelValues: labelvalues,
  60. help: "statefulSet_match_labels StatefulSet Match Labels",
  61. statefulsetName: name,
  62. namespace: namespace,
  63. }
  64. }
  65. // Desc returns the descriptor for the Metric. This method idempotently
  66. // returns the same descriptor throughout the lifetime of the Metric.
  67. func (s StatefulsetMatchLabelsMetric) Desc() *prometheus.Desc {
  68. l := prometheus.Labels{
  69. "statefulSet": s.statefulsetName,
  70. "namespace": s.namespace,
  71. }
  72. return prometheus.NewDesc(s.fqName, s.help, s.labelNames, l)
  73. }
  74. // Write encodes the Metric into a "Metric" Protocol Buffer data
  75. // transmission object.
  76. func (s StatefulsetMatchLabelsMetric) Write(m *dto.Metric) error {
  77. h := float64(1)
  78. m.Gauge = &dto.Gauge{
  79. Value: &h,
  80. }
  81. var labels []*dto.LabelPair
  82. for i := range s.labelNames {
  83. labels = append(labels, &dto.LabelPair{
  84. Name: &s.labelNames[i],
  85. Value: &s.labelValues[i],
  86. })
  87. }
  88. labels = append(labels, &dto.LabelPair{
  89. Name: toStringPtr("namespace"),
  90. Value: &s.namespace,
  91. })
  92. labels = append(labels, &dto.LabelPair{
  93. Name: toStringPtr("statefulSet"),
  94. Value: &s.statefulsetName,
  95. })
  96. m.Label = labels
  97. return nil
  98. }