statefulsetmetrics.go 3.3 KB

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