Przeglądaj źródła

have opencost leverage promutil from core

Signed-off-by: Matt Bolt <mbolt35@gmail.com>
Matt Bolt 2 lat temu
rodzic
commit
6eaee79fd6

+ 1 - 1
pkg/prom/metrics_test.go → core/pkg/util/promutil/promutil_test.go

@@ -1,4 +1,4 @@
-package prom
+package promutil
 
 import (
 	"fmt"

+ 4 - 4
pkg/cloudcost/queryservice_helper.go

@@ -6,11 +6,11 @@ import (
 	"net/http"
 	"strings"
 
-	filter21 "github.com/opencost/opencost/core/pkg/filter"
+	"github.com/opencost/opencost/core/pkg/filter"
 	"github.com/opencost/opencost/core/pkg/filter/cloudcost"
 	"github.com/opencost/opencost/core/pkg/kubecost"
 	"github.com/opencost/opencost/core/pkg/util/httputil"
-	"github.com/opencost/opencost/pkg/prom"
+	"github.com/opencost/opencost/core/pkg/util/promutil"
 )
 
 func ParseCloudCostRequest(qp httputil.QueryParams) (*QueryRequest, error) {
@@ -45,7 +45,7 @@ func ParseCloudCostRequest(qp httputil.QueryParams) (*QueryRequest, error) {
 
 	accumulate := kubecost.ParseAccumulate(qp.Get("accumulate", ""))
 
-	var filter filter21.Filter
+	var filter filter.Filter
 	filterString := qp.Get("filter", "")
 	if filterString != "" {
 		parser := cloudcost.NewCloudCostFilterParser()
@@ -83,7 +83,7 @@ func ParseCloudCostProperty(text string) (string, error) {
 	}
 
 	if strings.HasPrefix(text, "label:") {
-		label := prom.SanitizeLabelName(strings.TrimSpace(strings.TrimPrefix(text, "label:")))
+		label := promutil.SanitizeLabelName(strings.TrimSpace(strings.TrimPrefix(text, "label:")))
 		return fmt.Sprintf("label:%s", label), nil
 	}
 

+ 2 - 1
pkg/costmodel/allocation_helpers.go

@@ -9,6 +9,7 @@ import (
 
 	"github.com/opencost/opencost/core/pkg/kubecost"
 	"github.com/opencost/opencost/core/pkg/log"
+	"github.com/opencost/opencost/core/pkg/util/promutil"
 	"github.com/opencost/opencost/core/pkg/util/timeutil"
 	"github.com/opencost/opencost/pkg/cloud/provider"
 	"github.com/opencost/opencost/pkg/env"
@@ -796,7 +797,7 @@ func resToNodeLabels(resNodeLabels []*prom.QueryResult) map[nodeKey]map[string]s
 
 			// Sanitize the given label name to match Prometheus formatting
 			// e.g. topology.kubernetes.io/zone => topology_kubernetes_io_zone
-			k := prom.SanitizeLabelName(rawK)
+			k := promutil.SanitizeLabelName(rawK)
 			if v, ok := labels[k]; ok {
 				nodeLabels[nodeKey][k] = v
 				continue

+ 3 - 2
pkg/costmodel/costmodel.go

@@ -13,6 +13,7 @@ import (
 	"github.com/opencost/opencost/core/pkg/kubecost"
 	"github.com/opencost/opencost/core/pkg/log"
 	"github.com/opencost/opencost/core/pkg/util"
+	"github.com/opencost/opencost/core/pkg/util/promutil"
 	costAnalyzerCloud "github.com/opencost/opencost/pkg/cloud/models"
 	"github.com/opencost/opencost/pkg/clustercache"
 	"github.com/opencost/opencost/pkg/env"
@@ -2306,7 +2307,7 @@ func getNamespaceLabels(cache clustercache.ClusterCache, clusterID string) (map[
 	for _, ns := range nss {
 		labels := make(map[string]string)
 		for k, v := range ns.Labels {
-			labels[prom.SanitizeLabelName(k)] = v
+			labels[promutil.SanitizeLabelName(k)] = v
 		}
 		nsToLabels[ns.Name+","+clusterID] = labels
 	}
@@ -2319,7 +2320,7 @@ func getNamespaceAnnotations(cache clustercache.ClusterCache, clusterID string)
 	for _, ns := range nss {
 		annotations := make(map[string]string)
 		for k, v := range ns.Annotations {
-			annotations[prom.SanitizeLabelName(k)] = v
+			annotations[promutil.SanitizeLabelName(k)] = v
 		}
 		nsToAnnotations[ns.Name+","+clusterID] = annotations
 	}

+ 3 - 2
pkg/costmodel/metrics.go

@@ -11,6 +11,7 @@ import (
 	"github.com/opencost/opencost/core/pkg/log"
 	"github.com/opencost/opencost/core/pkg/util"
 	"github.com/opencost/opencost/core/pkg/util/atomic"
+	"github.com/opencost/opencost/core/pkg/util/promutil"
 	"github.com/opencost/opencost/pkg/cloud/models"
 	"github.com/opencost/opencost/pkg/clustercache"
 	"github.com/opencost/opencost/pkg/env"
@@ -54,7 +55,7 @@ func (cic ClusterInfoCollector) Collect(ch chan<- prometheus.Metric) {
 	}
 
 	clusterInfo := cic.ClusterInfo.GetClusterInfo()
-	labels := prom.MapToLabels(clusterInfo)
+	labels := promutil.MapToLabels(clusterInfo)
 
 	m := newClusterInfoMetric("kubecost_cluster_info", labels)
 	ch <- m
@@ -85,7 +86,7 @@ func newClusterInfoMetric(fqName string, labels map[string]string) ClusterInfoMe
 // returns the same descriptor throughout the lifetime of the Metric.
 func (cim ClusterInfoMetric) Desc() *prometheus.Desc {
 	l := prometheus.Labels{}
-	return prometheus.NewDesc(cim.fqName, cim.help, prom.LabelNamesFrom(cim.labels), l)
+	return prometheus.NewDesc(cim.fqName, cim.help, promutil.LabelNamesFrom(cim.labels), l)
 }
 
 // Write encodes the Metric into a "Metric" Protocol Buffer data

+ 2 - 2
pkg/metrics/deploymentmetrics.go

@@ -1,8 +1,8 @@
 package metrics
 
 import (
+	"github.com/opencost/opencost/core/pkg/util/promutil"
 	"github.com/opencost/opencost/pkg/clustercache"
-	"github.com/opencost/opencost/pkg/prom"
 
 	"github.com/prometheus/client_golang/prometheus"
 	dto "github.com/prometheus/client_model/go"
@@ -42,7 +42,7 @@ func (kdc KubecostDeploymentCollector) Collect(ch chan<- prometheus.Metric) {
 		deploymentName := deployment.GetName()
 		deploymentNS := deployment.GetNamespace()
 
-		labels, values := prom.KubeLabelsToLabels(prom.SanitizeLabels(deployment.Spec.Selector.MatchLabels))
+		labels, values := promutil.KubeLabelsToLabels(promutil.SanitizeLabels(deployment.Spec.Selector.MatchLabels))
 		if len(labels) > 0 {
 			m := newDeploymentMatchLabelsMetric(deploymentName, deploymentNS, "deployment_match_labels", labels, values)
 			ch <- m

+ 2 - 2
pkg/metrics/kubemetrics.go

@@ -5,8 +5,8 @@ import (
 	"strings"
 	"sync"
 
+	"github.com/opencost/opencost/core/pkg/util/promutil"
 	"github.com/opencost/opencost/pkg/clustercache"
-	"github.com/opencost/opencost/pkg/prom"
 
 	"github.com/prometheus/client_golang/prometheus"
 	batchv1 "k8s.io/api/batch/v1"
@@ -162,7 +162,7 @@ func getPersistentVolumeClaimClass(claim *v1.PersistentVolumeClaim) string {
 // toResourceUnitValue accepts a resource name and quantity and returns the sanitized resource, the unit, and the value in the units.
 // Returns an empty string for resource and unit if there was a failure.
 func toResourceUnitValue(resourceName v1.ResourceName, quantity resource.Quantity) (resource string, unit string, value float64) {
-	resource = prom.SanitizeLabelName(string(resourceName))
+	resource = promutil.SanitizeLabelName(string(resourceName))
 
 	switch resourceName {
 	case v1.ResourceCPU:

+ 3 - 3
pkg/metrics/namespacemetrics.go

@@ -1,8 +1,8 @@
 package metrics
 
 import (
+	"github.com/opencost/opencost/core/pkg/util/promutil"
 	"github.com/opencost/opencost/pkg/clustercache"
-	"github.com/opencost/opencost/pkg/prom"
 
 	"github.com/prometheus/client_golang/prometheus"
 	dto "github.com/prometheus/client_model/go"
@@ -41,7 +41,7 @@ func (nsac KubecostNamespaceCollector) Collect(ch chan<- prometheus.Metric) {
 	for _, namespace := range namespaces {
 		nsName := namespace.GetName()
 
-		labels, values := prom.KubeAnnotationsToLabels(namespace.Annotations)
+		labels, values := promutil.KubeAnnotationsToLabels(namespace.Annotations)
 		if len(labels) > 0 {
 			m := newNamespaceAnnotationsMetric("kube_namespace_annotations", nsName, labels, values)
 			ch <- m
@@ -139,7 +139,7 @@ func (nsac KubeNamespaceCollector) Collect(ch chan<- prometheus.Metric) {
 	for _, namespace := range namespaces {
 		nsName := namespace.GetName()
 
-		labels, values := prom.KubeLabelsToLabels(prom.SanitizeLabels(namespace.Labels))
+		labels, values := promutil.KubeLabelsToLabels(promutil.SanitizeLabels(namespace.Labels))
 		if len(labels) > 0 {
 			m := newNamespaceAnnotationsMetric("kube_namespace_labels", nsName, labels, values)
 			ch <- m

+ 2 - 2
pkg/metrics/nodemetrics.go

@@ -4,8 +4,8 @@ import (
 	"strings"
 
 	"github.com/opencost/opencost/core/pkg/log"
+	"github.com/opencost/opencost/core/pkg/util/promutil"
 	"github.com/opencost/opencost/pkg/clustercache"
-	"github.com/opencost/opencost/pkg/prom"
 	"github.com/prometheus/client_golang/prometheus"
 	dto "github.com/prometheus/client_model/go"
 	v1 "k8s.io/api/core/v1"
@@ -120,7 +120,7 @@ func (nsac KubeNodeCollector) Collect(ch chan<- prometheus.Metric) {
 
 		// node labels
 		if _, disabled := disabledMetrics["kube_node_labels"]; !disabled {
-			labelNames, labelValues := prom.KubePrependQualifierToLabels(prom.SanitizeLabels(node.GetLabels()), "label_")
+			labelNames, labelValues := promutil.KubePrependQualifierToLabels(promutil.SanitizeLabels(node.GetLabels()), "label_")
 			ch <- newKubeNodeLabelsMetric(nodeName, "kube_node_labels", labelNames, labelValues)
 		}
 

+ 2 - 2
pkg/metrics/podlabelmetrics.go

@@ -1,8 +1,8 @@
 package metrics
 
 import (
+	"github.com/opencost/opencost/core/pkg/util/promutil"
 	"github.com/opencost/opencost/pkg/clustercache"
-	"github.com/opencost/opencost/pkg/prom"
 	"github.com/prometheus/client_golang/prometheus"
 )
 
@@ -41,7 +41,7 @@ func (kpmc KubePodLabelsCollector) Collect(ch chan<- prometheus.Metric) {
 
 		// Pod Labels
 		if _, disabled := disabledMetrics["kube_pod_labels"]; !disabled {
-			labelNames, labelValues := prom.KubePrependQualifierToLabels(prom.SanitizeLabels(pod.GetLabels()), "label_")
+			labelNames, labelValues := promutil.KubePrependQualifierToLabels(promutil.SanitizeLabels(pod.GetLabels()), "label_")
 			ch <- newKubePodLabelsMetric("kube_pod_labels", podNS, podName, podUID, labelNames, labelValues)
 		}
 

+ 3 - 3
pkg/metrics/podmetrics.go

@@ -4,8 +4,8 @@ import (
 	"fmt"
 
 	"github.com/opencost/opencost/core/pkg/log"
+	"github.com/opencost/opencost/core/pkg/util/promutil"
 	"github.com/opencost/opencost/pkg/clustercache"
-	"github.com/opencost/opencost/pkg/prom"
 	"github.com/prometheus/client_golang/prometheus"
 	dto "github.com/prometheus/client_model/go"
 	v1 "k8s.io/api/core/v1"
@@ -46,7 +46,7 @@ func (kpmc KubecostPodCollector) Collect(ch chan<- prometheus.Metric) {
 		podNS := pod.GetNamespace()
 
 		// Pod Annotations
-		labels, values := prom.KubeAnnotationsToLabels(pod.Annotations)
+		labels, values := promutil.KubeAnnotationsToLabels(pod.Annotations)
 		if len(labels) > 0 {
 			ch <- newPodAnnotationMetric("kube_pod_annotations", podNS, podName, labels, values)
 		}
@@ -135,7 +135,7 @@ func (kpmc KubePodCollector) Collect(ch chan<- prometheus.Metric) {
 
 		// Pod Labels
 		if _, disabled := disabledMetrics["kube_pod_labels"]; !disabled {
-			labelNames, labelValues := prom.KubePrependQualifierToLabels(prom.SanitizeLabels(pod.GetLabels()), "label_")
+			labelNames, labelValues := promutil.KubePrependQualifierToLabels(promutil.SanitizeLabels(pod.GetLabels()), "label_")
 			ch <- newKubePodLabelsMetric("kube_pod_labels", podNS, podName, podUID, labelNames, labelValues)
 		}
 

+ 2 - 2
pkg/metrics/servicemetrics.go

@@ -1,8 +1,8 @@
 package metrics
 
 import (
+	"github.com/opencost/opencost/core/pkg/util/promutil"
 	"github.com/opencost/opencost/pkg/clustercache"
-	"github.com/opencost/opencost/pkg/prom"
 
 	"github.com/prometheus/client_golang/prometheus"
 	dto "github.com/prometheus/client_model/go"
@@ -42,7 +42,7 @@ func (sc KubecostServiceCollector) Collect(ch chan<- prometheus.Metric) {
 		serviceName := svc.GetName()
 		serviceNS := svc.GetNamespace()
 
-		labels, values := prom.KubeLabelsToLabels(prom.SanitizeLabels(svc.Spec.Selector))
+		labels, values := promutil.KubeLabelsToLabels(promutil.SanitizeLabels(svc.Spec.Selector))
 		if len(labels) > 0 {
 			m := newServiceSelectorLabelsMetric(serviceName, serviceNS, "service_selector_labels", labels, values)
 			ch <- m

+ 2 - 2
pkg/metrics/statefulsetmetrics.go

@@ -1,8 +1,8 @@
 package metrics
 
 import (
+	"github.com/opencost/opencost/core/pkg/util/promutil"
 	"github.com/opencost/opencost/pkg/clustercache"
-	"github.com/opencost/opencost/pkg/prom"
 
 	"github.com/prometheus/client_golang/prometheus"
 	dto "github.com/prometheus/client_model/go"
@@ -41,7 +41,7 @@ func (sc KubecostStatefulsetCollector) Collect(ch chan<- prometheus.Metric) {
 		statefulsetName := statefulset.GetName()
 		statefulsetNS := statefulset.GetNamespace()
 
-		labels, values := prom.KubeLabelsToLabels(prom.SanitizeLabels(statefulset.Spec.Selector.MatchLabels))
+		labels, values := promutil.KubeLabelsToLabels(promutil.SanitizeLabels(statefulset.Spec.Selector.MatchLabels))
 		if len(labels) > 0 {
 			m := newStatefulsetMatchLabelsMetric(statefulsetName, statefulsetNS, "statefulSet_match_labels", labels, values)
 			ch <- m

+ 0 - 120
pkg/prom/metrics.go

@@ -1,120 +0,0 @@
-package prom
-
-import (
-	"fmt"
-	"reflect"
-	"regexp"
-	"sort"
-	"strings"
-
-	"github.com/opencost/opencost/core/pkg/util/json"
-)
-
-var invalidLabelCharRE = regexp.MustCompile(`[^a-zA-Z0-9_]`)
-
-// AnyToLabels will create prometheus labels based on the fields of the interface
-// passed. Note that this method is quite expensive and should only be used when absolutely
-// necessary.
-func AnyToLabels(a interface{}) (map[string]string, error) {
-	val := reflect.ValueOf(a)
-	if val.Kind() == reflect.Map {
-		return MapToLabels(a), nil
-	}
-
-	b, e := json.Marshal(a)
-	if e != nil {
-		return nil, e
-	}
-
-	var m map[string]interface{}
-	e = json.Unmarshal(b, &m)
-	if e != nil {
-		return nil, e
-	}
-
-	return MapToLabels(m), nil
-}
-
-// MapToLabels accepts a map type, and will return a new map containing all the nested
-// fields separated by _ with string versions of the values.
-func MapToLabels(m interface{}) map[string]string {
-	val := reflect.ValueOf(m)
-	if val.Kind() != reflect.Map {
-		return map[string]string{}
-	}
-
-	r := make(map[string]string)
-
-	for _, k := range val.MapKeys() {
-		key := strings.ToLower(k.String())
-		v := val.MapIndex(k).Interface()
-
-		switch v.(type) {
-		case uint, uint8, uint16, uint32, uint64, int, int8, int16, int32, int64, string, bool, float32, float64:
-			r[key] = fmt.Sprintf("%+v", v)
-
-		default:
-			mm := MapToLabels(v)
-			for kk, vv := range mm {
-				r[fmt.Sprintf("%s_%s", key, kk)] = vv
-			}
-		}
-	}
-
-	return r
-}
-
-// LabelNamesFrom accepts a mapping of labels to values and returns the label names.
-func LabelNamesFrom(labels map[string]string) []string {
-	keys := []string{}
-	for key := range labels {
-		keys = append(keys, key)
-	}
-	return keys
-}
-
-// Prepends a qualifier string to the keys provided in the m map and returns the new keys and values.
-func KubePrependQualifierToLabels(m map[string]string, qualifier string) ([]string, []string) {
-	keys := make([]string, 0, len(m))
-	for k := range m {
-		keys = append(keys, k)
-	}
-	sort.Strings(keys)
-
-	values := make([]string, 0, len(m))
-	for i, k := range keys {
-		keys[i] = qualifier + SanitizeLabelName(k)
-		values = append(values, m[k])
-	}
-
-	return keys, values
-}
-
-// Converts kubernetes labels into prometheus labels.
-func KubeLabelsToLabels(labels map[string]string) ([]string, []string) {
-	return KubePrependQualifierToLabels(labels, "label_")
-}
-
-// Converts kubernetes annotations into prometheus labels.
-func KubeAnnotationsToLabels(labels map[string]string) ([]string, []string) {
-	return KubePrependQualifierToLabels(labels, "annotation_")
-}
-
-// Replaces all illegal prometheus label characters with _
-func SanitizeLabelName(s string) string {
-	return invalidLabelCharRE.ReplaceAllString(s, "_")
-}
-
-// SanitizeLabels sanitizes all label names in the given map. This may cause
-// collisions, which is intentional as collisions that are not caught prior to
-// attempted emission will cause fatal errors. In the case of a collision, the
-// last value seen will be set, and all previous values will be overwritten.
-func SanitizeLabels(labels map[string]string) map[string]string {
-	response := make(map[string]string, len(labels))
-
-	for k, v := range labels {
-		response[SanitizeLabelName(k)] = v
-	}
-
-	return response
-}

+ 2 - 2
test/clusterinfo_test.go

@@ -4,7 +4,7 @@ import (
 	"testing"
 
 	"github.com/opencost/opencost/core/pkg/util/json"
-	"github.com/opencost/opencost/pkg/prom"
+	"github.com/opencost/opencost/core/pkg/util/promutil"
 )
 
 func TestClusterInfoLabels(t *testing.T) {
@@ -18,7 +18,7 @@ func TestClusterInfoLabels(t *testing.T) {
 		return
 	}
 
-	labels := prom.MapToLabels(m)
+	labels := promutil.MapToLabels(m)
 	for k := range expected {
 		if _, ok := labels[k]; !ok {
 			t.Errorf("Failed to locate key: \"%s\" in labels.", k)