statefulsetmetrics.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package metrics
  2. import (
  3. "github.com/opencost/opencost/core/pkg/clustercache"
  4. "github.com/opencost/opencost/core/pkg/util/promutil"
  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.Name
  34. statefulsetNS := statefulset.Namespace
  35. statefulsetUID := string(statefulset.UID)
  36. if statefulset.SpecSelector != nil {
  37. labels, values := promutil.KubeLabelsToLabels(promutil.SanitizeLabels(statefulset.SpecSelector.MatchLabels))
  38. if len(labels) > 0 {
  39. m := newStatefulsetMatchLabelsMetric(statefulsetName, statefulsetNS, "statefulSet_match_labels", labels, values, statefulsetUID)
  40. ch <- m
  41. }
  42. }
  43. }
  44. }
  45. //--------------------------------------------------------------------------
  46. // StatefulsetMatchLabelsMetric
  47. //--------------------------------------------------------------------------
  48. // StatefulsetMetric is a prometheus.Metric used to encode statefulset match labels
  49. type StatefulsetMatchLabelsMetric struct {
  50. fqName string
  51. help string
  52. labelNames []string
  53. labelValues []string
  54. statefulsetName string
  55. namespace string
  56. uid string
  57. }
  58. // Creates a new StatefulsetMetric, implementation of prometheus.Metric
  59. func newStatefulsetMatchLabelsMetric(name, namespace, fqname string, labelNames, labelvalues []string, uid string) StatefulsetMatchLabelsMetric {
  60. return StatefulsetMatchLabelsMetric{
  61. fqName: fqname,
  62. labelNames: labelNames,
  63. labelValues: labelvalues,
  64. help: "statefulSet_match_labels StatefulSet Match Labels",
  65. statefulsetName: name,
  66. namespace: namespace,
  67. uid: uid,
  68. }
  69. }
  70. // Desc returns the descriptor for the Metric. This method idempotently
  71. // returns the same descriptor throughout the lifetime of the Metric.
  72. func (s StatefulsetMatchLabelsMetric) Desc() *prometheus.Desc {
  73. l := prometheus.Labels{
  74. "statefulSet": s.statefulsetName,
  75. "namespace": s.namespace,
  76. "uid": s.uid,
  77. }
  78. return prometheus.NewDesc(s.fqName, s.help, s.labelNames, l)
  79. }
  80. // Write encodes the Metric into a "Metric" Protocol Buffer data
  81. // transmission object.
  82. func (s StatefulsetMatchLabelsMetric) Write(m *dto.Metric) error {
  83. h := float64(1)
  84. m.Gauge = &dto.Gauge{
  85. Value: &h,
  86. }
  87. var labels []*dto.LabelPair
  88. for i := range s.labelNames {
  89. labels = append(labels, &dto.LabelPair{
  90. Name: &s.labelNames[i],
  91. Value: &s.labelValues[i],
  92. })
  93. }
  94. labels = append(labels, &dto.LabelPair{
  95. Name: toStringPtr("namespace"),
  96. Value: &s.namespace,
  97. })
  98. labels = append(labels, &dto.LabelPair{
  99. Name: toStringPtr("statefulSet"),
  100. Value: &s.statefulsetName,
  101. })
  102. labels = append(labels, &dto.LabelPair{
  103. Name: toStringPtr("uid"),
  104. Value: &s.uid,
  105. })
  106. m.Label = labels
  107. return nil
  108. }