Răsfoiți Sursa

Implement early returns where possible + code clarity changes

Kaelan Patel 4 ani în urmă
părinte
comite
25c75712b0

+ 41 - 33
pkg/costmodel/metrics.go

@@ -40,23 +40,27 @@ type ClusterInfoCollector struct {
 // collected by this Collector.
 func (cic ClusterInfoCollector) Describe(ch chan<- *prometheus.Desc) {
 	disabledMetrics := cic.metricsConfig.GetDisabledMetricsMap()
-
-	if _, ok := disabledMetrics["kube_pod_annotations"]; !ok {
-		ch <- prometheus.NewDesc("kubecost_cluster_info", "Kubecost Cluster Info", []string{}, nil)
+	if _, disabled := disabledMetrics["kube_pod_annotations"]; disabled {
+		return
 	}
+
+	ch <- prometheus.NewDesc("kubecost_cluster_info", "Kubecost Cluster Info", []string{}, nil)
+
 }
 
 // Collect is called by the Prometheus registry when collecting metrics.
 func (cic ClusterInfoCollector) Collect(ch chan<- prometheus.Metric) {
 	disabledMetrics := cic.metricsConfig.GetDisabledMetricsMap()
+	if _, disabled := disabledMetrics["kube_pod_annotations"]; disabled {
+		return
+	}
 
-	if _, ok := disabledMetrics["kube_pod_annotations"]; !ok {
-		clusterInfo := cic.ClusterInfo.GetClusterInfo()
-		labels := prom.MapToLabels(clusterInfo)
+	clusterInfo := cic.ClusterInfo.GetClusterInfo()
+	labels := prom.MapToLabels(clusterInfo)
+
+	m := newClusterInfoMetric("kubecost_cluster_info", labels)
+	ch <- m
 
-		m := newClusterInfoMetric("kubecost_cluster_info", labels)
-		ch <- m
-	}
 }
 
 //--------------------------------------------------------------------------
@@ -138,11 +142,11 @@ func initCostModelMetrics(clusterCache clustercache.ClusterCache, provider cloud
 
 	disabledMetrics := metricsConfig.GetDisabledMetricsMap()
 	var toRegisterGV []*prometheus.GaugeVec
-	var toRegisterGage []prometheus.Gauge
+	var toRegisterGauge []prometheus.Gauge
 
 	metricsInit.Do(func() {
 
-		if _, ok := disabledMetrics["node_cpu_hourly_cost"]; !ok {
+		if _, disabled := disabledMetrics["node_cpu_hourly_cost"]; !disabled {
 			cpuGv = prometheus.NewGaugeVec(prometheus.GaugeOpts{
 				Name: "node_cpu_hourly_cost",
 				Help: "node_cpu_hourly_cost hourly cost for each cpu on this node",
@@ -150,7 +154,7 @@ func initCostModelMetrics(clusterCache clustercache.ClusterCache, provider cloud
 			toRegisterGV = append(toRegisterGV, cpuGv)
 		}
 
-		if _, ok := disabledMetrics["node_ram_hourly_cost"]; !ok {
+		if _, disabled := disabledMetrics["node_ram_hourly_cost"]; !disabled {
 			ramGv = prometheus.NewGaugeVec(prometheus.GaugeOpts{
 				Name: "node_ram_hourly_cost",
 				Help: "node_ram_hourly_cost hourly cost for each gb of ram on this node",
@@ -158,7 +162,7 @@ func initCostModelMetrics(clusterCache clustercache.ClusterCache, provider cloud
 			toRegisterGV = append(toRegisterGV, ramGv)
 		}
 
-		if _, ok := disabledMetrics["node_gpu_hourly_cost"]; !ok {
+		if _, disabled := disabledMetrics["node_gpu_hourly_cost"]; !disabled {
 			gpuGv = prometheus.NewGaugeVec(prometheus.GaugeOpts{
 				Name: "node_gpu_hourly_cost",
 				Help: "node_gpu_hourly_cost hourly cost for each gpu on this node",
@@ -166,7 +170,7 @@ func initCostModelMetrics(clusterCache clustercache.ClusterCache, provider cloud
 			toRegisterGV = append(toRegisterGV, gpuGv)
 		}
 
-		if _, ok := disabledMetrics["node_gpu_count"]; !ok {
+		if _, disabled := disabledMetrics["node_gpu_count"]; !disabled {
 			gpuCountGv = prometheus.NewGaugeVec(prometheus.GaugeOpts{
 				Name: "node_gpu_count",
 				Help: "node_gpu_count count of gpu on this node",
@@ -174,7 +178,7 @@ func initCostModelMetrics(clusterCache clustercache.ClusterCache, provider cloud
 			toRegisterGV = append(toRegisterGV, gpuCountGv)
 		}
 
-		if _, ok := disabledMetrics["pv_hourly_cost"]; !ok {
+		if _, disabled := disabledMetrics["pv_hourly_cost"]; !disabled {
 			pvGv = prometheus.NewGaugeVec(prometheus.GaugeOpts{
 				Name: "pv_hourly_cost",
 				Help: "pv_hourly_cost Cost per GB per hour on a persistent disk",
@@ -182,7 +186,7 @@ func initCostModelMetrics(clusterCache clustercache.ClusterCache, provider cloud
 			toRegisterGV = append(toRegisterGV, pvGv)
 		}
 
-		if _, ok := disabledMetrics["kubecost_node_is_spot"]; !ok {
+		if _, disabled := disabledMetrics["kubecost_node_is_spot"]; !disabled {
 			spotGv = prometheus.NewGaugeVec(prometheus.GaugeOpts{
 				Name: "kubecost_node_is_spot",
 				Help: "kubecost_node_is_spot Cloud provider info about node preemptibility",
@@ -190,7 +194,7 @@ func initCostModelMetrics(clusterCache clustercache.ClusterCache, provider cloud
 			toRegisterGV = append(toRegisterGV, spotGv)
 		}
 
-		if _, ok := disabledMetrics["node_total_hourly_cost"]; !ok {
+		if _, disabled := disabledMetrics["node_total_hourly_cost"]; !disabled {
 			totalGv = prometheus.NewGaugeVec(prometheus.GaugeOpts{
 				Name: "node_total_hourly_cost",
 				Help: "node_total_hourly_cost Total node cost per hour",
@@ -198,7 +202,7 @@ func initCostModelMetrics(clusterCache clustercache.ClusterCache, provider cloud
 			toRegisterGV = append(toRegisterGV, totalGv)
 		}
 
-		if _, ok := disabledMetrics["container_memory_allocation_bytes"]; !ok {
+		if _, disabled := disabledMetrics["container_memory_allocation_bytes"]; !disabled {
 			ramAllocGv = prometheus.NewGaugeVec(prometheus.GaugeOpts{
 				Name: "container_memory_allocation_bytes",
 				Help: "container_memory_allocation_bytes Bytes of RAM used",
@@ -206,7 +210,7 @@ func initCostModelMetrics(clusterCache clustercache.ClusterCache, provider cloud
 			toRegisterGV = append(toRegisterGV, ramAllocGv)
 		}
 
-		if _, ok := disabledMetrics["container_cpu_allocation"]; !ok {
+		if _, disabled := disabledMetrics["container_cpu_allocation"]; !disabled {
 			cpuAllocGv = prometheus.NewGaugeVec(prometheus.GaugeOpts{
 				Name: "container_cpu_allocation",
 				Help: "container_cpu_allocation Percent of a single CPU used in a minute",
@@ -214,7 +218,7 @@ func initCostModelMetrics(clusterCache clustercache.ClusterCache, provider cloud
 			toRegisterGV = append(toRegisterGV, cpuAllocGv)
 		}
 
-		if _, ok := disabledMetrics["container_gpu_allocation"]; !ok {
+		if _, disabled := disabledMetrics["container_gpu_allocation"]; !disabled {
 			gpuAllocGv = prometheus.NewGaugeVec(prometheus.GaugeOpts{
 				Name: "container_gpu_allocation",
 				Help: "container_gpu_allocation GPU used",
@@ -222,7 +226,7 @@ func initCostModelMetrics(clusterCache clustercache.ClusterCache, provider cloud
 			toRegisterGV = append(toRegisterGV, gpuAllocGv)
 		}
 
-		if _, ok := disabledMetrics["pod_pvc_allocation"]; !ok {
+		if _, disabled := disabledMetrics["pod_pvc_allocation"]; !disabled {
 			pvAllocGv = prometheus.NewGaugeVec(prometheus.GaugeOpts{
 				Name: "pod_pvc_allocation",
 				Help: "pod_pvc_allocation Bytes used by a PVC attached to a pod",
@@ -230,31 +234,31 @@ func initCostModelMetrics(clusterCache clustercache.ClusterCache, provider cloud
 			toRegisterGV = append(toRegisterGV, pvAllocGv)
 		}
 
-		if _, ok := disabledMetrics["kubecost_network_zone_egress_cost"]; !ok {
+		if _, disabled := disabledMetrics["kubecost_network_zone_egress_cost"]; !disabled {
 			networkZoneEgressCostG = prometheus.NewGauge(prometheus.GaugeOpts{
 				Name: "kubecost_network_zone_egress_cost",
 				Help: "kubecost_network_zone_egress_cost Total cost per GB egress across zones",
 			})
-			toRegisterGage = append(toRegisterGage, networkZoneEgressCostG)
+			toRegisterGauge = append(toRegisterGauge, networkZoneEgressCostG)
 		}
 
-		if _, ok := disabledMetrics["kubecost_network_region_egress_cost"]; !ok {
+		if _, disabled := disabledMetrics["kubecost_network_region_egress_cost"]; !disabled {
 			networkRegionEgressCostG = prometheus.NewGauge(prometheus.GaugeOpts{
 				Name: "kubecost_network_region_egress_cost",
 				Help: "kubecost_network_region_egress_cost Total cost per GB egress across regions",
 			})
-			toRegisterGage = append(toRegisterGage, networkRegionEgressCostG)
+			toRegisterGauge = append(toRegisterGauge, networkRegionEgressCostG)
 		}
 
-		if _, ok := disabledMetrics["kubecost_network_internet_egress_cost"]; !ok {
+		if _, disabled := disabledMetrics["kubecost_network_internet_egress_cost"]; !disabled {
 			networkInternetEgressCostG = prometheus.NewGauge(prometheus.GaugeOpts{
 				Name: "kubecost_network_internet_egress_cost",
 				Help: "kubecost_network_internet_egress_cost Total cost per GB of internet egress.",
 			})
-			toRegisterGage = append(toRegisterGage, networkInternetEgressCostG)
+			toRegisterGauge = append(toRegisterGauge, networkInternetEgressCostG)
 		}
 
-		if _, ok := disabledMetrics["kubecost_cluster_management_cost"]; !ok {
+		if _, disabled := disabledMetrics["kubecost_cluster_management_cost"]; !disabled {
 			clusterManagementCostGv = prometheus.NewGaugeVec(prometheus.GaugeOpts{
 				Name: "kubecost_cluster_management_cost",
 				Help: "kubecost_cluster_management_cost Hourly cost paid as a cluster management fee.",
@@ -262,7 +266,7 @@ func initCostModelMetrics(clusterCache clustercache.ClusterCache, provider cloud
 			toRegisterGV = append(toRegisterGV, clusterManagementCostGv)
 		}
 
-		if _, ok := disabledMetrics["kubecost_load_balancer_cost"]; !ok {
+		if _, disabled := disabledMetrics["kubecost_load_balancer_cost"]; !disabled {
 			lbCostGv = prometheus.NewGaugeVec(prometheus.GaugeOpts{ // no differentiation between ELB and ALB right now
 				Name: "kubecost_load_balancer_cost",
 				Help: "kubecost_load_balancer_cost Hourly cost of load balancer",
@@ -271,11 +275,11 @@ func initCostModelMetrics(clusterCache clustercache.ClusterCache, provider cloud
 		}
 
 		// Register cost-model metrics for emission
-		for i := range toRegisterGV {
-			prometheus.MustRegister(toRegisterGV[i])
+		for _, gv := range toRegisterGV {
+			prometheus.MustRegister(gv)
 		}
-		for i := range toRegisterGage {
-			prometheus.MustRegister(toRegisterGage[i])
+		for _, g := range toRegisterGauge {
+			prometheus.MustRegister(g)
 		}
 
 		// General Metric Collectors
@@ -329,6 +333,10 @@ func NewCostModelMetricsEmitter(promClient promclient.Client, clusterCache clust
 		log.Infof("Failed to get metrics config before init: %s", err)
 	}
 
+	if len(metricsConfig.DisabledMetrics) > 0 {
+		log.Infof("Starting metrics init with disabled metrics: %v", metricsConfig.DisabledMetrics)
+	}
+
 	// init will only actually execute once to register the custom gauges
 	initCostModelMetrics(clusterCache, provider, clusterInfo, metricsConfig)
 

+ 21 - 18
pkg/metrics/deploymentmetrics.go

@@ -23,29 +23,32 @@ type KubecostDeploymentCollector struct {
 // collected by this Collector.
 func (kdc KubecostDeploymentCollector) Describe(ch chan<- *prometheus.Desc) {
 	disabledMetrics := kdc.metricsConfig.GetDisabledMetricsMap()
-
-	if _, ok := disabledMetrics["deployment_match_labels"]; !ok {
-		ch <- prometheus.NewDesc("deployment_match_labels", "deployment match labels", []string{}, nil)
+	if _, disabled := disabledMetrics["deployment_match_labels"]; disabled {
+		return
 	}
+
+	ch <- prometheus.NewDesc("deployment_match_labels", "deployment match labels", []string{}, nil)
 }
 
 // Collect is called by the Prometheus registry when collecting metrics.
 func (kdc KubecostDeploymentCollector) Collect(ch chan<- prometheus.Metric) {
 	disabledMetrics := kdc.metricsConfig.GetDisabledMetricsMap()
+	if _, disabled := disabledMetrics["deployment_match_labels"]; disabled {
+		return
+	}
 
-	if _, ok := disabledMetrics["deployment_match_labels"]; !ok {
-		ds := kdc.KubeClusterCache.GetAllDeployments()
-		for _, deployment := range ds {
-			deploymentName := deployment.GetName()
-			deploymentNS := deployment.GetNamespace()
-
-			labels, values := prom.KubeLabelsToLabels(deployment.Spec.Selector.MatchLabels)
-			if len(labels) > 0 {
-				m := newDeploymentMatchLabelsMetric(deploymentName, deploymentNS, "deployment_match_labels", labels, values)
-				ch <- m
-			}
+	ds := kdc.KubeClusterCache.GetAllDeployments()
+	for _, deployment := range ds {
+		deploymentName := deployment.GetName()
+		deploymentNS := deployment.GetNamespace()
+
+		labels, values := prom.KubeLabelsToLabels(deployment.Spec.Selector.MatchLabels)
+		if len(labels) > 0 {
+			m := newDeploymentMatchLabelsMetric(deploymentName, deploymentNS, "deployment_match_labels", labels, values)
+			ch <- m
 		}
 	}
+
 }
 
 //--------------------------------------------------------------------------
@@ -125,10 +128,10 @@ type KubeDeploymentCollector struct {
 func (kdc KubeDeploymentCollector) Describe(ch chan<- *prometheus.Desc) {
 	disabledMetrics := kdc.metricsConfig.GetDisabledMetricsMap()
 
-	if _, ok := disabledMetrics["kube_deployment_spec_replicas"]; !ok {
+	if _, disabled := disabledMetrics["kube_deployment_spec_replicas"]; !disabled {
 		ch <- prometheus.NewDesc("kube_deployment_spec_replicas", "Number of desired pods for a deployment.", []string{}, nil)
 	}
-	if _, ok := disabledMetrics["kube_deployment_status_replicas_available"]; !ok {
+	if _, disabled := disabledMetrics["kube_deployment_status_replicas_available"]; !disabled {
 		ch <- prometheus.NewDesc("kube_deployment_status_replicas_available", "The number of available replicas per deployment.", []string{}, nil)
 	}
 
@@ -151,10 +154,10 @@ func (kdc KubeDeploymentCollector) Collect(ch chan<- prometheus.Metric) {
 			replicas = *deployment.Spec.Replicas
 		}
 
-		if _, ok := disabledMetrics["kube_deployment_spec_replicas"]; !ok {
+		if _, disabled := disabledMetrics["kube_deployment_spec_replicas"]; !disabled {
 			ch <- newKubeDeploymentReplicasMetric("kube_deployment_spec_replicas", deploymentName, deploymentNS, replicas)
 		}
-		if _, ok := disabledMetrics["kube_deployment_status_replicas_available"]; !ok {
+		if _, disabled := disabledMetrics["kube_deployment_status_replicas_available"]; !disabled {
 			// Replicas Available
 			ch <- newKubeDeploymentStatusAvailableReplicasMetric(
 				"kube_deployment_status_replicas_available",

+ 28 - 25
pkg/metrics/jobmetrics.go

@@ -25,43 +25,46 @@ type KubeJobCollector struct {
 // collected by this Collector.
 func (kjc KubeJobCollector) Describe(ch chan<- *prometheus.Desc) {
 	disabledMetrics := kjc.metricsConfig.GetDisabledMetricsMap()
-
-	if _, ok := disabledMetrics["kube_pod_annotations"]; !ok {
-		ch <- prometheus.NewDesc("kube_job_status_failed", "The number of pods which reached Phase Failed and the reason for failure.", []string{}, nil)
+	if _, disabled := disabledMetrics["kube_pod_annotations"]; disabled {
+		return
 	}
+
+	ch <- prometheus.NewDesc("kube_job_status_failed", "The number of pods which reached Phase Failed and the reason for failure.", []string{}, nil)
 }
 
 // Collect is called by the Prometheus registry when collecting metrics.
 func (kjc KubeJobCollector) Collect(ch chan<- prometheus.Metric) {
 	disabledMetrics := kjc.metricsConfig.GetDisabledMetricsMap()
+	if _, disabled := disabledMetrics["kube_pod_annotations"]; disabled {
+		return
+	}
 
-	if _, ok := disabledMetrics["kube_pod_annotations"]; !ok {
-		jobs := kjc.KubeClusterCache.GetAllJobs()
-		for _, job := range jobs {
-			jobName := job.GetName()
-			jobNS := job.GetNamespace()
-
-			if job.Status.Failed == 0 {
-				ch <- newKubeJobStatusFailedMetric(jobName, jobNS, "kube_job_status_failed", "", 0)
-			} else {
-				for _, condition := range job.Status.Conditions {
-					if condition.Type == batchv1.JobFailed {
-						reasonKnown := false
-						for _, reason := range jobFailureReasons {
-							reasonKnown = reasonKnown || failureReason(&condition, reason)
-
-							ch <- newKubeJobStatusFailedMetric(jobName, jobNS, "kube_job_status_failed", reason, boolFloat64(failureReason(&condition, reason)))
-						}
-
-						// for unknown reasons
-						if !reasonKnown {
-							ch <- newKubeJobStatusFailedMetric(jobName, jobNS, "kube_job_status_failed", "", float64(job.Status.Failed))
-						}
+	jobs := kjc.KubeClusterCache.GetAllJobs()
+	for _, job := range jobs {
+		jobName := job.GetName()
+		jobNS := job.GetNamespace()
+
+		if job.Status.Failed == 0 {
+			ch <- newKubeJobStatusFailedMetric(jobName, jobNS, "kube_job_status_failed", "", 0)
+		} else {
+			for _, condition := range job.Status.Conditions {
+				if condition.Type == batchv1.JobFailed {
+					reasonKnown := false
+					for _, reason := range jobFailureReasons {
+						reasonKnown = reasonKnown || failureReason(&condition, reason)
+
+						ch <- newKubeJobStatusFailedMetric(jobName, jobNS, "kube_job_status_failed", reason, boolFloat64(failureReason(&condition, reason)))
+					}
+
+					// for unknown reasons
+					if !reasonKnown {
+						ch <- newKubeJobStatusFailedMetric(jobName, jobNS, "kube_job_status_failed", "", float64(job.Status.Failed))
 					}
 				}
 			}
 		}
 	}
+
 }
 
 //--------------------------------------------------------------------------

+ 32 - 24
pkg/metrics/namespacemetrics.go

@@ -22,28 +22,32 @@ type KubecostNamespaceCollector struct {
 // collected by this Collector.
 func (nsac KubecostNamespaceCollector) Describe(ch chan<- *prometheus.Desc) {
 	disabledMetrics := nsac.metricsConfig.GetDisabledMetricsMap()
-
-	if _, ok := disabledMetrics["kube_namespace_labels"]; !ok {
-		ch <- prometheus.NewDesc("kube_namespace_annotations", "namespace annotations", []string{}, nil)
+	if _, disabled := disabledMetrics["kube_namespace_annotations"]; disabled {
+		return
 	}
+
+	ch <- prometheus.NewDesc("kube_namespace_annotations", "namespace annotations", []string{}, nil)
+
 }
 
 // Collect is called by the Prometheus registry when collecting metrics.
 func (nsac KubecostNamespaceCollector) Collect(ch chan<- prometheus.Metric) {
 	disabledMetrics := nsac.metricsConfig.GetDisabledMetricsMap()
+	if _, disabled := disabledMetrics["kube_namespace_annotations"]; disabled {
+		return
+	}
 
-	if _, ok := disabledMetrics["kube_namespace_labels"]; !ok {
-		namespaces := nsac.KubeClusterCache.GetAllNamespaces()
-		for _, namespace := range namespaces {
-			nsName := namespace.GetName()
+	namespaces := nsac.KubeClusterCache.GetAllNamespaces()
+	for _, namespace := range namespaces {
+		nsName := namespace.GetName()
 
-			labels, values := prom.KubeAnnotationsToLabels(namespace.Annotations)
-			if len(labels) > 0 {
-				m := newNamespaceAnnotationsMetric("kube_namespace_annotations", nsName, labels, values)
-				ch <- m
-			}
+		labels, values := prom.KubeAnnotationsToLabels(namespace.Annotations)
+		if len(labels) > 0 {
+			m := newNamespaceAnnotationsMetric("kube_namespace_annotations", nsName, labels, values)
+			ch <- m
 		}
 	}
+
 }
 
 //--------------------------------------------------------------------------
@@ -116,28 +120,32 @@ type KubeNamespaceCollector struct {
 // collected by this Collector.
 func (nsac KubeNamespaceCollector) Describe(ch chan<- *prometheus.Desc) {
 	disabledMetrics := nsac.metricsConfig.GetDisabledMetricsMap()
-
-	if _, ok := disabledMetrics["kube_namespace_labels"]; !ok {
-		ch <- prometheus.NewDesc("kube_namespace_labels", "namespace labels", []string{}, nil)
+	if _, disabled := disabledMetrics["kube_namespace_labels"]; disabled {
+		return
 	}
+
+	ch <- prometheus.NewDesc("kube_namespace_labels", "namespace labels", []string{}, nil)
+
 }
 
 // Collect is called by the Prometheus registry when collecting metrics.
 func (nsac KubeNamespaceCollector) Collect(ch chan<- prometheus.Metric) {
 	disabledMetrics := nsac.metricsConfig.GetDisabledMetricsMap()
+	if _, disabled := disabledMetrics["kube_namespace_labels"]; disabled {
+		return
+	}
 
-	if _, ok := disabledMetrics["kube_namespace_labels"]; !ok {
-		namespaces := nsac.KubeClusterCache.GetAllNamespaces()
-		for _, namespace := range namespaces {
-			nsName := namespace.GetName()
+	namespaces := nsac.KubeClusterCache.GetAllNamespaces()
+	for _, namespace := range namespaces {
+		nsName := namespace.GetName()
 
-			labels, values := prom.KubeLabelsToLabels(namespace.Labels)
-			if len(labels) > 0 {
-				m := newNamespaceAnnotationsMetric("kube_namespace_labels", nsName, labels, values)
-				ch <- m
-			}
+		labels, values := prom.KubeLabelsToLabels(namespace.Labels)
+		if len(labels) > 0 {
+			m := newNamespaceAnnotationsMetric("kube_namespace_labels", nsName, labels, values)
+			ch <- m
 		}
 	}
+
 }
 
 //--------------------------------------------------------------------------

+ 16 - 16
pkg/metrics/nodemetrics.go

@@ -30,28 +30,28 @@ type KubeNodeCollector struct {
 func (nsac KubeNodeCollector) Describe(ch chan<- *prometheus.Desc) {
 	disabledMetrics := nsac.metricsConfig.GetDisabledMetricsMap()
 
-	if _, ok := disabledMetrics["kube_node_status_capacity"]; !ok {
+	if _, disabled := disabledMetrics["kube_node_status_capacity"]; !disabled {
 		ch <- prometheus.NewDesc("kube_node_status_capacity", "Node resource capacity.", []string{}, nil)
 	}
-	if _, ok := disabledMetrics["kube_node_status_capacity_memory_bytes"]; !ok {
+	if _, disabled := disabledMetrics["kube_node_status_capacity_memory_bytes"]; !disabled {
 		ch <- prometheus.NewDesc("kube_node_status_capacity_memory_bytes", "node capacity memory bytes", []string{}, nil)
 	}
-	if _, ok := disabledMetrics["kube_node_status_capacity_cpu_cores"]; !ok {
+	if _, disabled := disabledMetrics["kube_node_status_capacity_cpu_cores"]; !disabled {
 		ch <- prometheus.NewDesc("kube_node_status_capacity_cpu_cores", "node capacity cpu cores", []string{}, nil)
 	}
-	if _, ok := disabledMetrics["kube_node_status_allocatable"]; !ok {
+	if _, disabled := disabledMetrics["kube_node_status_allocatable"]; !disabled {
 		ch <- prometheus.NewDesc("kube_node_status_allocatable", "The allocatable for different resources of a node that are available for scheduling.", []string{}, nil)
 	}
-	if _, ok := disabledMetrics["kube_node_status_allocatable_cpu_cores"]; !ok {
+	if _, disabled := disabledMetrics["kube_node_status_allocatable_cpu_cores"]; !disabled {
 		ch <- prometheus.NewDesc("kube_node_status_allocatable_cpu_cores", "The allocatable cpu cores.", []string{}, nil)
 	}
-	if _, ok := disabledMetrics["kube_node_status_allocatable_memory_bytes"]; !ok {
+	if _, disabled := disabledMetrics["kube_node_status_allocatable_memory_bytes"]; !disabled {
 		ch <- prometheus.NewDesc("kube_node_status_allocatable_memory_bytes", "The allocatable memory in bytes.", []string{}, nil)
 	}
-	if _, ok := disabledMetrics["kube_node_labels"]; !ok {
+	if _, disabled := disabledMetrics["kube_node_labels"]; !disabled {
 		ch <- prometheus.NewDesc("kube_node_labels", "all labels for each node prefixed with label_", []string{}, nil)
 	}
-	if _, ok := disabledMetrics["kube_node_status_condition"]; !ok {
+	if _, disabled := disabledMetrics["kube_node_status_condition"]; !disabled {
 		ch <- prometheus.NewDesc("kube_node_status_condition", "The condition of a cluster node.", []string{}, nil)
 	}
 }
@@ -75,19 +75,19 @@ func (nsac KubeNodeCollector) Collect(ch chan<- prometheus.Metric) {
 			}
 
 			// KSM v1 Emission
-			if _, ok := disabledMetrics["kube_node_status_capacity_cpu_cores"]; !ok {
+			if _, disabled := disabledMetrics["kube_node_status_capacity_cpu_cores"]; !disabled {
 				if resource == "cpu" {
 					ch <- newKubeNodeStatusCapacityCPUCoresMetric("kube_node_status_capacity_cpu_cores", nodeName, value)
 
 				}
 			}
-			if _, ok := disabledMetrics["kube_node_status_capacity_memory_bytes"]; !ok {
+			if _, disabled := disabledMetrics["kube_node_status_capacity_memory_bytes"]; !disabled {
 				if resource == "memory" {
 					ch <- newKubeNodeStatusCapacityMemoryBytesMetric("kube_node_status_capacity_memory_bytes", nodeName, value)
 				}
 			}
 
-			if _, ok := disabledMetrics["kube_node_status_capacity"]; !ok {
+			if _, disabled := disabledMetrics["kube_node_status_capacity"]; !disabled {
 				ch <- newKubeNodeStatusCapacityMetric("kube_node_status_capacity", nodeName, resource, unit, value)
 			}
 		}
@@ -103,30 +103,30 @@ func (nsac KubeNodeCollector) Collect(ch chan<- prometheus.Metric) {
 			}
 
 			// KSM v1 Emission
-			if _, ok := disabledMetrics["kube_node_status_allocatable_cpu_cores"]; !ok {
+			if _, disabled := disabledMetrics["kube_node_status_allocatable_cpu_cores"]; !disabled {
 				if resource == "cpu" {
 					ch <- newKubeNodeStatusAllocatableCPUCoresMetric("kube_node_status_allocatable_cpu_cores", nodeName, value)
 				}
 			}
-			if _, ok := disabledMetrics["kube_node_status_allocatable_memory_bytes"]; !ok {
+			if _, disabled := disabledMetrics["kube_node_status_allocatable_memory_bytes"]; !disabled {
 				if resource == "memory" {
 					ch <- newKubeNodeStatusAllocatableMemoryBytesMetric("kube_node_status_allocatable_memory_bytes", nodeName, value)
 				}
 			}
-			if _, ok := disabledMetrics["kube_node_status_allocatable"]; !ok {
+			if _, disabled := disabledMetrics["kube_node_status_allocatable"]; !disabled {
 				ch <- newKubeNodeStatusAllocatableMetric("kube_node_status_allocatable", nodeName, resource, unit, value)
 			}
 		}
 
 		// node labels
-		if _, ok := disabledMetrics["kube_node_labels"]; !ok {
+		if _, disabled := disabledMetrics["kube_node_labels"]; !disabled {
 			labelNames, labelValues := prom.KubePrependQualifierToLabels(node.GetLabels(), "label_")
 			ch <- newKubeNodeLabelsMetric(nodeName, "kube_node_labels", labelNames, labelValues)
 		}
 
 		// kube_node_status_condition
 		// Collect node conditions and while default to false.
-		if _, ok := disabledMetrics["kube_node_status_condition"]; !ok {
+		if _, disabled := disabledMetrics["kube_node_status_condition"]; !disabled {
 			for _, c := range node.Status.Conditions {
 				conditions := getConditions(c.Status)
 

+ 4 - 4
pkg/metrics/podlabelmetrics.go

@@ -51,10 +51,10 @@ type KubePodLabelsCollector struct {
 func (kpmc KubePodLabelsCollector) Describe(ch chan<- *prometheus.Desc) {
 	disabledMetrics := kpmc.metricsConfig.GetDisabledMetricsMap()
 
-	if _, ok := disabledMetrics["kube_pod_labels"]; !ok {
+	if _, disabled := disabledMetrics["kube_pod_labels"]; !disabled {
 		ch <- prometheus.NewDesc("kube_pod_labels", "All labels for each pod prefixed with label_", []string{}, nil)
 	}
-	if _, ok := disabledMetrics["kube_pod_owner"]; !ok {
+	if _, disabled := disabledMetrics["kube_pod_owner"]; !disabled {
 		ch <- prometheus.NewDesc("kube_pod_owner", "Information about the Pod's owner", []string{}, nil)
 	}
 }
@@ -70,13 +70,13 @@ func (kpmc KubePodLabelsCollector) Collect(ch chan<- prometheus.Metric) {
 		podUID := string(pod.GetUID())
 
 		// Pod Labels
-		if _, ok := disabledMetrics["kube_pod_labels"]; !ok {
+		if _, disabled := disabledMetrics["kube_pod_labels"]; !disabled {
 			labelNames, labelValues := prom.KubePrependQualifierToLabels(pod.GetLabels(), "label_")
 			ch <- newKubePodLabelsMetric("kube_pod_labels", podNS, podName, podUID, labelNames, labelValues)
 		}
 
 		// Owner References
-		if _, ok := disabledMetrics["kube_pod_owner"]; !ok {
+		if _, disabled := disabledMetrics["kube_pod_owner"]; !disabled {
 			for _, owner := range pod.OwnerReferences {
 				ch <- newKubePodOwnerMetric("kube_pod_owner", podNS, podName, owner.Name, owner.Kind, owner.Controller != nil)
 			}

+ 37 - 33
pkg/metrics/podmetrics.go

@@ -25,29 +25,33 @@ type KubecostPodCollector struct {
 // collected by this Collector.
 func (kpmc KubecostPodCollector) Describe(ch chan<- *prometheus.Desc) {
 	disabledMetrics := kpmc.metricsConfig.GetDisabledMetricsMap()
-
-	if _, ok := disabledMetrics["kube_pod_annotations"]; !ok {
-		ch <- prometheus.NewDesc("kube_pod_annotations", "All annotations for each pod prefix with annotation_", []string{}, nil)
+	if _, disabled := disabledMetrics["kube_pod_annotations"]; disabled {
+		return
 	}
+
+	ch <- prometheus.NewDesc("kube_pod_annotations", "All annotations for each pod prefix with annotation_", []string{}, nil)
+
 }
 
 // Collect is called by the Prometheus registry when collecting metrics.
 func (kpmc KubecostPodCollector) Collect(ch chan<- prometheus.Metric) {
 	disabledMetrics := kpmc.metricsConfig.GetDisabledMetricsMap()
+	if _, disabled := disabledMetrics["kube_pod_annotations"]; disabled {
+		return
+	}
 
-	if _, ok := disabledMetrics["kube_pod_annotations"]; !ok {
-		pods := kpmc.KubeClusterCache.GetAllPods()
-		for _, pod := range pods {
-			podName := pod.GetName()
-			podNS := pod.GetNamespace()
+	pods := kpmc.KubeClusterCache.GetAllPods()
+	for _, pod := range pods {
+		podName := pod.GetName()
+		podNS := pod.GetNamespace()
 
-			// Pod Annotations
-			labels, values := prom.KubeAnnotationsToLabels(pod.Annotations)
-			if len(labels) > 0 {
-				ch <- newPodAnnotationMetric("kube_pod_annotations", podNS, podName, labels, values)
-			}
+		// Pod Annotations
+		labels, values := prom.KubeAnnotationsToLabels(pod.Annotations)
+		if len(labels) > 0 {
+			ch <- newPodAnnotationMetric("kube_pod_annotations", podNS, podName, labels, values)
 		}
 	}
+
 }
 
 //--------------------------------------------------------------------------
@@ -65,34 +69,34 @@ type KubePodCollector struct {
 func (kpmc KubePodCollector) Describe(ch chan<- *prometheus.Desc) {
 	disabledMetrics := kpmc.metricsConfig.GetDisabledMetricsMap()
 
-	if _, ok := disabledMetrics["kube_pod_labels"]; !ok {
+	if _, disabled := disabledMetrics["kube_pod_labels"]; !disabled {
 		ch <- prometheus.NewDesc("kube_pod_labels", "All labels for each pod prefixed with label_", []string{}, nil)
 	}
-	if _, ok := disabledMetrics["kube_pod_owner"]; !ok {
+	if _, disabled := disabledMetrics["kube_pod_owner"]; !disabled {
 		ch <- prometheus.NewDesc("kube_pod_owner", "Information about the Pod's owner", []string{}, nil)
 	}
-	if _, ok := disabledMetrics["kube_pod_container_status_running"]; !ok {
+	if _, disabled := disabledMetrics["kube_pod_container_status_running"]; !disabled {
 		ch <- prometheus.NewDesc("kube_pod_container_status_running", "Describes whether the container is currently in running state", []string{}, nil)
 	}
-	if _, ok := disabledMetrics["kube_pod_container_status_terminated_reason"]; !ok {
+	if _, disabled := disabledMetrics["kube_pod_container_status_terminated_reason"]; !disabled {
 		ch <- prometheus.NewDesc("kube_pod_container_status_terminated_reason", "Describes the reason the container is currently in terminated state.", []string{}, nil)
 	}
-	if _, ok := disabledMetrics["kube_pod_container_status_restarts_total"]; !ok {
+	if _, disabled := disabledMetrics["kube_pod_container_status_restarts_total"]; !disabled {
 		ch <- prometheus.NewDesc("kube_pod_container_status_restarts_total", "The number of container restarts per container.", []string{}, nil)
 	}
-	if _, ok := disabledMetrics["kube_pod_container_resource_requests"]; !ok {
+	if _, disabled := disabledMetrics["kube_pod_container_resource_requests"]; !disabled {
 		ch <- prometheus.NewDesc("kube_pod_container_resource_requests", "The number of requested resource by a container", []string{}, nil)
 	}
-	if _, ok := disabledMetrics["kube_pod_container_resource_limits"]; !ok {
+	if _, disabled := disabledMetrics["kube_pod_container_resource_limits"]; !disabled {
 		ch <- prometheus.NewDesc("kube_pod_container_resource_limits", "The number of requested limit resource by a container.", []string{}, nil)
 	}
-	if _, ok := disabledMetrics["kube_pod_container_resource_limits_cpu_cores"]; !ok {
+	if _, disabled := disabledMetrics["kube_pod_container_resource_limits_cpu_cores"]; !disabled {
 		ch <- prometheus.NewDesc("kube_pod_container_resource_limits_cpu_cores", "The number of requested limit cpu core resource by a container.", []string{}, nil)
 	}
-	if _, ok := disabledMetrics["kube_pod_container_resource_limits_memory_bytes"]; !ok {
+	if _, disabled := disabledMetrics["kube_pod_container_resource_limits_memory_bytes"]; !disabled {
 		ch <- prometheus.NewDesc("kube_pod_container_resource_limits_memory_bytes", "The number of requested limit memory resource by a container.", []string{}, nil)
 	}
-	if _, ok := disabledMetrics["kube_pod_status_phase"]; !ok {
+	if _, disabled := disabledMetrics["kube_pod_status_phase"]; !disabled {
 		ch <- prometheus.NewDesc("kube_pod_status_phase", "The pods current phase.", []string{}, nil)
 	}
 }
@@ -110,7 +114,7 @@ func (kpmc KubePodCollector) Collect(ch chan<- prometheus.Metric) {
 		phase := pod.Status.Phase
 
 		// Pod Status Phase
-		if _, ok := disabledMetrics["kube_pod_status_phase"]; !ok {
+		if _, disabled := disabledMetrics["kube_pod_status_phase"]; !disabled {
 			if phase != "" {
 				phases := []struct {
 					v bool
@@ -130,13 +134,13 @@ func (kpmc KubePodCollector) Collect(ch chan<- prometheus.Metric) {
 		}
 
 		// Pod Labels
-		if _, ok := disabledMetrics["kube_pod_labels"]; !ok {
+		if _, disabled := disabledMetrics["kube_pod_labels"]; !disabled {
 			labelNames, labelValues := prom.KubePrependQualifierToLabels(pod.GetLabels(), "label_")
 			ch <- newKubePodLabelsMetric("kube_pod_labels", podNS, podName, podUID, labelNames, labelValues)
 		}
 
 		// Owner References
-		if _, ok := disabledMetrics["kube_pod_owner"]; !ok {
+		if _, disabled := disabledMetrics["kube_pod_owner"]; !disabled {
 			for _, owner := range pod.OwnerReferences {
 				ch <- newKubePodOwnerMetric("kube_pod_owner", podNS, podName, owner.Name, owner.Kind, owner.Controller != nil)
 			}
@@ -144,17 +148,17 @@ func (kpmc KubePodCollector) Collect(ch chan<- prometheus.Metric) {
 
 		// Container Status
 		for _, status := range pod.Status.ContainerStatuses {
-			if _, ok := disabledMetrics["kube_pod_container_status_restarts_total"]; !ok {
+			if _, disabled := disabledMetrics["kube_pod_container_status_restarts_total"]; !disabled {
 				ch <- newKubePodContainerStatusRestartsTotalMetric("kube_pod_container_status_restarts_total", podNS, podName, podUID, status.Name, float64(status.RestartCount))
 			}
 			if status.State.Running != nil {
-				if _, ok := disabledMetrics["kube_pod_container_status_running"]; !ok {
+				if _, disabled := disabledMetrics["kube_pod_container_status_running"]; !disabled {
 					ch <- newKubePodContainerStatusRunningMetric("kube_pod_container_status_running", podNS, podName, podUID, status.Name)
 				}
 			}
 
 			if status.State.Terminated != nil {
-				if _, ok := disabledMetrics["kube_pod_container_status_terminated_reason"]; !ok {
+				if _, disabled := disabledMetrics["kube_pod_container_status_terminated_reason"]; !disabled {
 					ch <- newKubePodContainerStatusTerminatedReasonMetric(
 						"kube_pod_container_status_terminated_reason",
 						podNS,
@@ -169,7 +173,7 @@ func (kpmc KubePodCollector) Collect(ch chan<- prometheus.Metric) {
 		for _, container := range pod.Spec.Containers {
 
 			// Requests
-			if _, ok := disabledMetrics["kube_pod_container_resource_requests"]; !ok {
+			if _, disabled := disabledMetrics["kube_pod_container_resource_requests"]; !disabled {
 				for resourceName, quantity := range container.Resources.Requests {
 					resource, unit, value := toResourceUnitValue(resourceName, quantity)
 
@@ -203,7 +207,7 @@ func (kpmc KubePodCollector) Collect(ch chan<- prometheus.Metric) {
 				}
 
 				// KSM v1 Emission
-				if _, ok := disabledMetrics["kube_pod_container_resource_limits_cpu_cores"]; !ok {
+				if _, disabled := disabledMetrics["kube_pod_container_resource_limits_cpu_cores"]; !disabled {
 					if resource == "cpu" {
 						ch <- newKubePodContainerResourceLimitsCPUCoresMetric(
 							"kube_pod_container_resource_limits_cpu_cores",
@@ -215,7 +219,7 @@ func (kpmc KubePodCollector) Collect(ch chan<- prometheus.Metric) {
 							value)
 					}
 				}
-				if _, ok := disabledMetrics["kube_pod_container_resource_limits_memory_bytes"]; !ok {
+				if _, disabled := disabledMetrics["kube_pod_container_resource_limits_memory_bytes"]; !disabled {
 					if resource == "memory" {
 						ch <- newKubePodContainerResourceLimitsMemoryBytesMetric(
 							"kube_pod_container_resource_limits_memory_bytes",
@@ -227,7 +231,7 @@ func (kpmc KubePodCollector) Collect(ch chan<- prometheus.Metric) {
 							value)
 					}
 				}
-				if _, ok := disabledMetrics["kube_pod_container_resource_limits"]; !ok {
+				if _, disabled := disabledMetrics["kube_pod_container_resource_limits"]; !disabled {
 					ch <- newKubePodContainerResourceLimitsMetric(
 						"kube_pod_container_resource_limits",
 						podNS,

+ 4 - 4
pkg/metrics/pvcmetrics.go

@@ -21,10 +21,10 @@ type KubePVCCollector struct {
 func (kpvc KubePVCCollector) Describe(ch chan<- *prometheus.Desc) {
 	disabledMetrics := kpvc.metricsConfig.GetDisabledMetricsMap()
 
-	if _, ok := disabledMetrics["kube_persistentvolumeclaim_resource_requests_storage_bytes"]; !ok {
+	if _, disabled := disabledMetrics["kube_persistentvolumeclaim_resource_requests_storage_bytes"]; !disabled {
 		ch <- prometheus.NewDesc("kube_persistentvolumeclaim_resource_requests_storage_bytes", "The pvc storage resource requests in bytes", []string{}, nil)
 	}
-	if _, ok := disabledMetrics["kube_persistentvolumeclaim_info"]; !ok {
+	if _, disabled := disabledMetrics["kube_persistentvolumeclaim_info"]; !disabled {
 		ch <- prometheus.NewDesc("kube_persistentvolumeclaim_info", "The pvc storage resource requests in bytes", []string{}, nil)
 	}
 }
@@ -38,12 +38,12 @@ func (kpvc KubePVCCollector) Collect(ch chan<- prometheus.Metric) {
 		storageClass := getPersistentVolumeClaimClass(pvc)
 		volume := pvc.Spec.VolumeName
 
-		if _, ok := disabledMetrics["kube_persistentvolumeclaim_info"]; !ok {
+		if _, disabled := disabledMetrics["kube_persistentvolumeclaim_info"]; !disabled {
 			ch <- newKubePVCInfoMetric("kube_persistentvolumeclaim_info", pvc.Name, pvc.Namespace, storageClass, volume)
 		}
 
 		if storage, ok := pvc.Spec.Resources.Requests[v1.ResourceStorage]; ok {
-			if _, ok := disabledMetrics["kube_persistentvolumeclaim_resource_requests_storage_bytes"]; !ok {
+			if _, disabled := disabledMetrics["kube_persistentvolumeclaim_resource_requests_storage_bytes"]; !disabled {
 				ch <- newKubePVCResourceRequestsStorageBytesMetric("kube_persistentvolumeclaim_resource_requests_storage_bytes", pvc.Name, pvc.Namespace, float64(storage.Value()))
 			}
 		}

+ 4 - 4
pkg/metrics/pvmetrics.go

@@ -22,10 +22,10 @@ type KubePVCollector struct {
 func (kpvcb KubePVCollector) Describe(ch chan<- *prometheus.Desc) {
 	disabledMetrics := kpvcb.metricsConfig.GetDisabledMetricsMap()
 
-	if _, ok := disabledMetrics["kube_persistentvolume_capacity_bytes"]; !ok {
+	if _, disabled := disabledMetrics["kube_persistentvolume_capacity_bytes"]; !disabled {
 		ch <- prometheus.NewDesc("kube_persistentvolume_capacity_bytes", "The pv storage capacity in bytes", []string{}, nil)
 	}
-	if _, ok := disabledMetrics["kube_persistentvolume_status_phase"]; !ok {
+	if _, disabled := disabledMetrics["kube_persistentvolume_status_phase"]; !disabled {
 		ch <- prometheus.NewDesc("kube_persistentvolume_status_phase", "The phase indicates if a volume is available, bound to a claim, or released by a claim.", []string{}, nil)
 	}
 }
@@ -36,7 +36,7 @@ func (kpvcb KubePVCollector) Collect(ch chan<- prometheus.Metric) {
 	disabledMetrics := kpvcb.metricsConfig.GetDisabledMetricsMap()
 
 	for _, pv := range pvs {
-		if _, ok := disabledMetrics["kube_persistentvolume_status_phase"]; !ok {
+		if _, disabled := disabledMetrics["kube_persistentvolume_status_phase"]; !disabled {
 			phase := pv.Status.Phase
 			if phase != "" {
 				phases := []struct {
@@ -56,7 +56,7 @@ func (kpvcb KubePVCollector) Collect(ch chan<- prometheus.Metric) {
 			}
 		}
 
-		if _, ok := disabledMetrics["kube_persistentvolume_capacity_bytes"]; !ok {
+		if _, disabled := disabledMetrics["kube_persistentvolume_capacity_bytes"]; !disabled {
 			storage := pv.Spec.Capacity[v1.ResourceStorage]
 			m := newKubePVCapacityBytesMetric("kube_persistentvolume_capacity_bytes", pv.Name, float64(storage.Value()))
 

+ 18 - 14
pkg/metrics/servicemetrics.go

@@ -22,29 +22,33 @@ type KubecostServiceCollector struct {
 // collected by this Collector.
 func (sc KubecostServiceCollector) Describe(ch chan<- *prometheus.Desc) {
 	disabledMetrics := sc.metricsConfig.GetDisabledMetricsMap()
-
-	if _, ok := disabledMetrics["service_selector_labels"]; !ok {
-		ch <- prometheus.NewDesc("service_selector_labels", "service selector labels", []string{}, nil)
+	if _, disabled := disabledMetrics["service_selector_labels"]; disabled {
+		return
 	}
+
+	ch <- prometheus.NewDesc("service_selector_labels", "service selector labels", []string{}, nil)
+
 }
 
 // Collect is called by the Prometheus registry when collecting metrics.
 func (sc KubecostServiceCollector) Collect(ch chan<- prometheus.Metric) {
 	disabledMetrics := sc.metricsConfig.GetDisabledMetricsMap()
+	if _, disabled := disabledMetrics["service_selector_labels"]; disabled {
+		return
+	}
+
+	svcs := sc.KubeClusterCache.GetAllServices()
+	for _, svc := range svcs {
+		serviceName := svc.GetName()
+		serviceNS := svc.GetNamespace()
 
-	if _, ok := disabledMetrics["service_selector_labels"]; !ok {
-		svcs := sc.KubeClusterCache.GetAllServices()
-		for _, svc := range svcs {
-			serviceName := svc.GetName()
-			serviceNS := svc.GetNamespace()
-
-			labels, values := prom.KubeLabelsToLabels(svc.Spec.Selector)
-			if len(labels) > 0 {
-				m := newServiceSelectorLabelsMetric(serviceName, serviceNS, "service_selector_labels", labels, values)
-				ch <- m
-			}
+		labels, values := prom.KubeLabelsToLabels(svc.Spec.Selector)
+		if len(labels) > 0 {
+			m := newServiceSelectorLabelsMetric(serviceName, serviceNS, "service_selector_labels", labels, values)
+			ch <- m
 		}
 	}
+
 }
 
 //--------------------------------------------------------------------------

+ 16 - 13
pkg/metrics/statefulsetmetrics.go

@@ -22,29 +22,32 @@ type KubecostStatefulsetCollector struct {
 // collected by this Collector.
 func (sc KubecostStatefulsetCollector) Describe(ch chan<- *prometheus.Desc) {
 	disabledMetrics := sc.metricsConfig.GetDisabledMetricsMap()
-
-	if _, ok := disabledMetrics["statefulSet_match_labels"]; !ok {
-		ch <- prometheus.NewDesc("statefulSet_match_labels", "statfulSet match labels", []string{}, nil)
+	if _, disabled := disabledMetrics["statefulSet_match_labels"]; disabled {
+		return
 	}
+
+	ch <- prometheus.NewDesc("statefulSet_match_labels", "statfulSet match labels", []string{}, nil)
 }
 
 // Collect is called by the Prometheus registry when collecting metrics.
 func (sc KubecostStatefulsetCollector) Collect(ch chan<- prometheus.Metric) {
 	disabledMetrics := sc.metricsConfig.GetDisabledMetricsMap()
+	if _, disabled := disabledMetrics["statefulSet_match_labels"]; disabled {
+		return
+	}
 
-	if _, ok := disabledMetrics["statefulSet_match_labels"]; !ok {
-		ds := sc.KubeClusterCache.GetAllStatefulSets()
-		for _, statefulset := range ds {
-			statefulsetName := statefulset.GetName()
-			statefulsetNS := statefulset.GetNamespace()
+	ds := sc.KubeClusterCache.GetAllStatefulSets()
+	for _, statefulset := range ds {
+		statefulsetName := statefulset.GetName()
+		statefulsetNS := statefulset.GetNamespace()
 
-			labels, values := prom.KubeLabelsToLabels(statefulset.Spec.Selector.MatchLabels)
-			if len(labels) > 0 {
-				m := newStatefulsetMatchLabelsMetric(statefulsetName, statefulsetNS, "statefulSet_match_labels", labels, values)
-				ch <- m
-			}
+		labels, values := prom.KubeLabelsToLabels(statefulset.Spec.Selector.MatchLabels)
+		if len(labels) > 0 {
+			m := newStatefulsetMatchLabelsMetric(statefulsetName, statefulsetNS, "statefulSet_match_labels", labels, values)
+			ch <- m
 		}
 	}
+
 }
 
 //--------------------------------------------------------------------------