podlabelmetrics.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. )
  7. //--------------------------------------------------------------------------
  8. // KubecostPodCollector
  9. //--------------------------------------------------------------------------
  10. // KubecostPodCollector is a prometheus collector that emits pod metrics
  11. type KubecostPodLabelsCollector struct {
  12. KubeClusterCache clustercache.ClusterCache
  13. }
  14. // Describe sends the super-set of all possible descriptors of metrics
  15. // collected by this Collector.
  16. func (kpmc KubecostPodLabelsCollector) Describe(ch chan<- *prometheus.Desc) {
  17. ch <- prometheus.NewDesc("kube_pod_annotations", "All annotations for each pod prefix with annotation_", []string{}, nil)
  18. }
  19. // Collect is called by the Prometheus registry when collecting metrics.
  20. func (kpmc KubecostPodLabelsCollector) Collect(ch chan<- prometheus.Metric) {
  21. pods := kpmc.KubeClusterCache.GetAllPods()
  22. for _, pod := range pods {
  23. podName := pod.GetName()
  24. podNS := pod.GetNamespace()
  25. // Pod Annotations
  26. labels, values := prom.KubeAnnotationsToLabels(pod.Annotations)
  27. if len(labels) > 0 {
  28. ch <- newPodAnnotationMetric("kube_pod_annotations", podNS, podName, labels, values)
  29. }
  30. }
  31. }
  32. //--------------------------------------------------------------------------
  33. // KubePodLabelsCollector
  34. //--------------------------------------------------------------------------
  35. // KubePodLabelsCollector is a prometheus collector that emits pod labels only
  36. type KubePodLabelsCollector struct {
  37. KubeClusterCache clustercache.ClusterCache
  38. }
  39. // Describe sends the super-set of all possible descriptors of pod labels only
  40. // collected by this Collector.
  41. func (kpmc KubePodLabelsCollector) Describe(ch chan<- *prometheus.Desc) {
  42. ch <- prometheus.NewDesc("kube_pod_labels", "All labels for each pod prefixed with label_", []string{}, nil)
  43. ch <- prometheus.NewDesc("kube_pod_owner", "Information about the Pod's owner", []string{}, nil)
  44. }
  45. // Collect is called by the Prometheus registry when collecting metrics.
  46. func (kpmc KubePodLabelsCollector) Collect(ch chan<- prometheus.Metric) {
  47. pods := kpmc.KubeClusterCache.GetAllPods()
  48. for _, pod := range pods {
  49. podName := pod.GetName()
  50. podNS := pod.GetNamespace()
  51. podUID := string(pod.GetUID())
  52. // Pod Labels
  53. labelNames, labelValues := prom.KubePrependQualifierToLabels(pod.GetLabels(), "label_")
  54. ch <- newKubePodLabelsMetric("kube_pod_labels", podNS, podName, podUID, labelNames, labelValues)
  55. // Owner References
  56. for _, owner := range pod.OwnerReferences {
  57. ch <- newKubePodOwnerMetric("kube_pod_owner", podNS, podName, owner.Name, owner.Kind, owner.Controller != nil)
  58. }
  59. }
  60. }