فهرست منبع

added metric send to prometheus in metrics.go

Calvin Wang 5 سال پیش
والد
کامیت
f967ab315e
3فایلهای تغییر یافته به همراه35 افزوده شده و 4 حذف شده
  1. 13 3
      pkg/costmodel/costmodel.go
  2. 21 0
      pkg/costmodel/metrics.go
  3. 1 1
      pkg/costmodel/router.go

+ 13 - 3
pkg/costmodel/costmodel.go

@@ -355,6 +355,13 @@ func (cm *CostModel) ComputeCostData(cli prometheusClient.Client, clientset kube
 		return nil, err
 	}
 
+	// TODO: is this necessary?
+	// loadBalancers, err := cm.GetLBCost(cp)
+	// if err != nil {
+	// 	log.Warningf("GetLBCost: no load balancer cost model available: " + err.Error())
+	// 	return nil, err
+	// }
+
 	// Unmounted PVs represent the PVs that are not mounted or tied to a volume on a container
 	unmountedPVs := make(map[string][]*PersistentVolumeClaimData)
 	pvClaimMapping, err := GetPVInfo(resPVRequests, clusterID)
@@ -1184,7 +1191,7 @@ func (cm *CostModel) GetNodeCost(cp costAnalyzerCloud.Provider) (map[string]*cos
 }
 
 // TODO: drop some logs
-func (cm *CostModel) getLBCost(cp costAnalyzerCloud.Provider) (map[string]*costAnalyzerCloud.LoadBalancer, error) {
+func (cm *CostModel) GetLBCost(cp costAnalyzerCloud.Provider) (map[string]*costAnalyzerCloud.LoadBalancer, error) {
 	// for fetching prices from cloud provider
 	// cfg, err := cp.GetConfig()
 	// if err != nil {
@@ -1199,8 +1206,10 @@ func (cm *CostModel) getLBCost(cp costAnalyzerCloud.Provider) (map[string]*costA
 	// 3. need to check if key exists in servicesList and then populate loadBalancers with it
 
 	for _, service := range servicesList {
-		// namespace := service.GetObjectMeta().GetNamespace() // do I need this?
+		namespace := service.GetObjectMeta().GetNamespace()
 		name := service.GetObjectMeta().GetName()
+		// TODO: add clusterID to key? possible if called in ComputeCostData(), but what about metrics.go?
+		key := namespace + "," + name
 
 		// Does this identify ELB vs. ILB? Need to test the /api/allServices call with an ALB. Current work is on ELBs.
 		if service.Spec.Type == "LoadBalancer" {
@@ -1216,7 +1225,8 @@ func (cm *CostModel) getLBCost(cp costAnalyzerCloud.Provider) (map[string]*costA
 					newLoadBalancer.IngressIPAddresses = append(newLoadBalancer.IngressIPAddresses, loadBalancerIngress.IP)
 				}
 			}
-			loadBalancerMap[name] = &newLoadBalancer
+			newLoadBalancer.Cost = 88.88
+			loadBalancerMap[key] = &newLoadBalancer
 		}
 	}
 	return loadBalancerMap, nil

+ 21 - 0
pkg/costmodel/metrics.go

@@ -408,6 +408,7 @@ func StartCostModelMetricRecording(a *Accesses) bool {
 
 		containerSeen := make(map[string]bool)
 		nodeSeen := make(map[string]bool)
+		loadBalancerSeen := make(map[string]bool)
 		pvSeen := make(map[string]bool)
 		pvcSeen := make(map[string]bool)
 
@@ -511,6 +512,18 @@ func StartCostModelMetricRecording(a *Accesses) bool {
 				nodeSeen[labelKey] = true
 			}
 
+			loadBalancers, err := a.Model.GetLBCost(a.Cloud)
+			for lbKey, lb := range loadBalancers {
+				// TODO: parse (if necessary) and calculate cost associated with loadBalancer based on dynamic cloud prices fetched into each lb struct on GetLBCost() call
+				keyParts := getLabelStringsFromKey(lbKey)
+				namespace := keyParts[0]
+				serviceName := keyParts[1]
+				a.LBCostRecorder.WithLabelValues(namespace, serviceName).Set(lb.Cost)
+
+				labelKey := getKeyFromLabelStrings(namespace, serviceName)
+				loadBalancerSeen[labelKey] = true
+			}
+
 			for _, costs := range data {
 				nodeName := costs.NodeName
 
@@ -623,6 +636,14 @@ func StartCostModelMetricRecording(a *Accesses) bool {
 					nodeSeen[labelString] = false
 				}
 			}
+			for labelString, seen := range loadBalancerSeen {
+				if !seen {
+					labels := getLabelStringsFromKey(labelString)
+					a.LBCostRecorder.DeleteLabelValues(labels...)
+				} else {
+					loadBalancerSeen[labelString] = false
+				}
+			}
 			for labelString, seen := range containerSeen {
 				if !seen {
 					labels := getLabelStringsFromKey(labelString)

+ 1 - 1
pkg/costmodel/router.go

@@ -902,7 +902,7 @@ func Initialize(additionalConfigWatchers ...ConfigWatchers) {
 	LBCostRecorder := prometheus.NewGaugeVec(prometheus.GaugeOpts{ // don't know if necessary to differentiate ELB vs. ALB cost
 		Name: "load_balancer_cost",
 		Help: "load_balancer_cost Hourly cost of load balancer",
-	}, []string{"ip_address", "namespace", "service_name"}) // will likely need some adjustments
+	}, []string{"namespace", "service_name"}) // will likely need some adjustments. could have multiple IPs per LB, so ignore for now
 
 	prometheus.MustRegister(cpuGv)
 	prometheus.MustRegister(ramGv)