podlabelmetrics.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. )
  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. metricsConfig MetricsConfig
  39. }
  40. // Describe sends the super-set of all possible descriptors of pod labels only
  41. // collected by this Collector.
  42. func (kpmc KubePodLabelsCollector) Describe(ch chan<- *prometheus.Desc) {
  43. disabledMetrics := kpmc.metricsConfig.GetDisabledMetricsMap()
  44. if _, disabled := disabledMetrics["kube_pod_labels"]; !disabled {
  45. ch <- prometheus.NewDesc("kube_pod_labels", "All labels for each pod prefixed with label_", []string{}, nil)
  46. }
  47. if _, disabled := disabledMetrics["kube_pod_owner"]; !disabled {
  48. ch <- prometheus.NewDesc("kube_pod_owner", "Information about the Pod's owner", []string{}, nil)
  49. }
  50. }
  51. // Collect is called by the Prometheus registry when collecting metrics.
  52. func (kpmc KubePodLabelsCollector) Collect(ch chan<- prometheus.Metric) {
  53. pods := kpmc.KubeClusterCache.GetAllPods()
  54. disabledMetrics := kpmc.metricsConfig.GetDisabledMetricsMap()
  55. for _, pod := range pods {
  56. podName := pod.GetName()
  57. podNS := pod.GetNamespace()
  58. podUID := string(pod.GetUID())
  59. // Pod Labels
  60. if _, disabled := disabledMetrics["kube_pod_labels"]; !disabled {
  61. labelNames, labelValues := prom.KubePrependQualifierToLabels(pod.GetLabels(), "label_")
  62. ch <- newKubePodLabelsMetric("kube_pod_labels", podNS, podName, podUID, labelNames, labelValues)
  63. }
  64. // Owner References
  65. if _, disabled := disabledMetrics["kube_pod_owner"]; !disabled {
  66. for _, owner := range pod.OwnerReferences {
  67. ch <- newKubePodOwnerMetric("kube_pod_owner", podNS, podName, owner.Name, owner.Kind, owner.Controller != nil)
  68. }
  69. }
  70. }
  71. }