Переглянути джерело

fix unit tests, separators for prom implementation

Signed-off-by: Matt Bolt <mbolt35@gmail.com>
Matt Bolt 1 рік тому
батько
коміт
701b7a1f44

+ 13 - 1
modules/prometheus-source/pkg/prom/datasource.go

@@ -572,6 +572,10 @@ func (pds *PrometheusDataSource) MetaData() map[string]string {
 	return metadata
 }
 
+//--------------------------------------------------------------------------
+//  InstantMetricsQuerier
+//--------------------------------------------------------------------------
+
 func (pds *PrometheusDataSource) QueryRAMUsage(window string, offset string) source.QueryResultsChan {
 	const ramUsageQuery = `avg(
 		label_replace(
@@ -736,6 +740,10 @@ func (pds *PrometheusDataSource) QueryHistoricalPodLabels(window string, offset
 	return ctx.Query(queryHistoricalPodLabels)
 }
 
+//--------------------------------------------------------------------------
+//  RangeMetricsQuerier
+//--------------------------------------------------------------------------
+
 func (pds *PrometheusDataSource) QueryRAMRequestsOverTime(start, end time.Time, resolution time.Duration) source.QueryResultsChan {
 	const ramRequestsQuery = `avg(
 		label_replace(
@@ -1146,6 +1154,10 @@ func (pds *PrometheusDataSource) QueryNormalizationOverTime(start, end time.Time
 	return ctx.QueryRange(queryNormalization, start, end, resolution)
 }
 
+//--------------------------------------------------------------------------
+//  ClusterMetricsQuerier
+//--------------------------------------------------------------------------
+
 func (pds *PrometheusDataSource) QueryPVCost(start, end time.Time) source.QueryResultsChan {
 	const pvCostQuery = `avg(avg_over_time(pv_hourly_cost{%s}[%s])) by (%s, persistentvolume,provider_id)`
 
@@ -1880,7 +1892,7 @@ func (pds *PrometheusDataSource) QueryClusterRAM(start, end time.Time, step time
 	durStr := timeutil.DurationString(step)
 
 	if durStr == "" {
-		panic("failed to parse duration string passed to QueryClusterCores")
+		panic("failed to parse duration string passed to QueryClusterRAM")
 	}
 
 	clusterRAMQuery := fmt.Sprintf(queryClusterRAM, cfg.ClusterFilter, durStr, cfg.ClusterLabel, cfg.ClusterFilter, durStr, cfg.ClusterLabel, cfg.ClusterLabel)

+ 40 - 32
pkg/costmodel/allocation_helpers_test.go

@@ -220,58 +220,62 @@ func TestBuildPVMap(t *testing.T) {
 		"pvMap1": {
 			resolution: time.Hour * 6,
 			resultsPVCostPerGiBHour: []*source.QueryResult{
-				{
-					Metric: map[string]interface{}{
+				source.NewQueryResult(
+					map[string]interface{}{
 						"cluster_id": "cluster1",
 						"volumename": "pv1",
 					},
-					Values: []*util.Vector{
+					[]*util.Vector{
 						{
 							Value: 0.05,
 						},
 					},
-				},
-				{
-					Metric: map[string]interface{}{
+					source.DefaultResultKeys(),
+				),
+				source.NewQueryResult(
+					map[string]interface{}{
 						"cluster_id": "cluster1",
 						"volumename": "pv2",
 					},
-					Values: []*util.Vector{
+					[]*util.Vector{
 						{
 							Value: 0.05,
 						},
 					},
-				},
-				{
-					Metric: map[string]interface{}{
+					source.DefaultResultKeys(),
+				),
+				source.NewQueryResult(
+					map[string]interface{}{
 						"cluster_id": "cluster2",
 						"volumename": "pv3",
 					},
-					Values: []*util.Vector{
+					[]*util.Vector{
 						{
 							Value: 0.03,
 						},
 					},
-				},
-				{
-					Metric: map[string]interface{}{
+					source.DefaultResultKeys(),
+				),
+				source.NewQueryResult(
+					map[string]interface{}{
 						"cluster_id": "cluster2",
 						"volumename": "pv4",
 					},
-					Values: []*util.Vector{
+					[]*util.Vector{
 						{
 							Value: 0.05,
 						},
 					},
-				},
+					source.DefaultResultKeys(),
+				),
 			},
 			resultsActiveMinutes: []*source.QueryResult{
-				{
-					Metric: map[string]interface{}{
+				source.NewQueryResult(
+					map[string]interface{}{
 						"cluster_id":       "cluster1",
 						"persistentvolume": "pv1",
 					},
-					Values: []*util.Vector{
+					[]*util.Vector{
 						{
 							Timestamp: startFloat,
 						},
@@ -285,13 +289,14 @@ func TestBuildPVMap(t *testing.T) {
 							Timestamp: startFloat + (hour * 18),
 						},
 					},
-				},
-				{
-					Metric: map[string]interface{}{
+					source.DefaultResultKeys(),
+				),
+				source.NewQueryResult(
+					map[string]interface{}{
 						"cluster_id":       "cluster1",
 						"persistentvolume": "pv2",
 					},
-					Values: []*util.Vector{
+					[]*util.Vector{
 						{
 							Timestamp: startFloat,
 						},
@@ -308,13 +313,14 @@ func TestBuildPVMap(t *testing.T) {
 							Timestamp: startFloat + (hour * 24),
 						},
 					},
-				},
-				{
-					Metric: map[string]interface{}{
+					source.DefaultResultKeys(),
+				),
+				source.NewQueryResult(
+					map[string]interface{}{
 						"cluster_id":       "cluster2",
 						"persistentvolume": "pv3",
 					},
-					Values: []*util.Vector{
+					[]*util.Vector{
 						{
 							Timestamp: startFloat + (hour * 6),
 						},
@@ -325,13 +331,14 @@ func TestBuildPVMap(t *testing.T) {
 							Timestamp: startFloat + (hour * 18),
 						},
 					},
-				},
-				{
-					Metric: map[string]interface{}{
+					source.DefaultResultKeys(),
+				),
+				source.NewQueryResult(
+					map[string]interface{}{
 						"cluster_id":       "cluster2",
 						"persistentvolume": "pv4",
 					},
-					Values: []*util.Vector{
+					[]*util.Vector{
 						{
 							Timestamp: startFloat,
 						},
@@ -345,7 +352,8 @@ func TestBuildPVMap(t *testing.T) {
 							Timestamp: startFloat + (hour * 18),
 						},
 					},
-				},
+					source.DefaultResultKeys(),
+				),
 			},
 			expected: pvMap1NoBytes,
 		},

+ 67 - 56
pkg/costmodel/cluster_helpers_test.go

@@ -738,20 +738,21 @@ func TestBuildGPUCostMap(t *testing.T) {
 		{
 			name: "All Zeros",
 			promResult: []*source.QueryResult{
-				{
-					Metric: map[string]interface{}{
+				source.NewQueryResult(
+					map[string]interface{}{
 						"cluster_id":    "cluster1",
 						"node":          "node1",
 						"instance_type": "type1",
 						"provider_id":   "provider1",
 					},
-					Values: []*util.Vector{
+					[]*util.Vector{
 						{
 							Timestamp: 0,
 							Value:     0,
 						},
 					},
-				},
+					source.DefaultResultKeys(),
+				),
 			},
 			countMap: map[NodeIdentifier]float64{
 				{
@@ -771,20 +772,21 @@ func TestBuildGPUCostMap(t *testing.T) {
 		{
 			name: "Zero Node Count",
 			promResult: []*source.QueryResult{
-				{
-					Metric: map[string]interface{}{
+				source.NewQueryResult(
+					map[string]interface{}{
 						"cluster_id":    "cluster1",
 						"node":          "node1",
 						"instance_type": "type1",
 						"provider_id":   "provider1",
 					},
-					Values: []*util.Vector{
+					[]*util.Vector{
 						{
 							Timestamp: 0,
 							Value:     2,
 						},
 					},
-				},
+					source.DefaultResultKeys(),
+				),
 			},
 			countMap: map[NodeIdentifier]float64{
 				{
@@ -804,20 +806,21 @@ func TestBuildGPUCostMap(t *testing.T) {
 		{
 			name: "Missing Node Count",
 			promResult: []*source.QueryResult{
-				{
-					Metric: map[string]interface{}{
+				source.NewQueryResult(
+					map[string]interface{}{
 						"cluster_id":    "cluster1",
 						"node":          "node1",
 						"instance_type": "type1",
 						"provider_id":   "provider1",
 					},
-					Values: []*util.Vector{
+					[]*util.Vector{
 						{
 							Timestamp: 0,
 							Value:     2,
 						},
 					},
-				},
+					source.DefaultResultKeys(),
+				),
 			},
 			countMap: map[NodeIdentifier]float64{},
 			expected: map[NodeIdentifier]float64{
@@ -829,10 +832,8 @@ func TestBuildGPUCostMap(t *testing.T) {
 			},
 		},
 		{
-			name: "missing cost data",
-			promResult: []*source.QueryResult{
-				{},
-			},
+			name:       "missing cost data",
+			promResult: []*source.QueryResult{},
 			countMap: map[NodeIdentifier]float64{
 				{
 					Cluster:    "cluster1",
@@ -845,20 +846,21 @@ func TestBuildGPUCostMap(t *testing.T) {
 		{
 			name: "All values present",
 			promResult: []*source.QueryResult{
-				{
-					Metric: map[string]interface{}{
+				source.NewQueryResult(
+					map[string]interface{}{
 						"cluster_id":    "cluster1",
 						"node":          "node1",
 						"instance_type": "type1",
 						"provider_id":   "provider1",
 					},
-					Values: []*util.Vector{
+					[]*util.Vector{
 						{
 							Timestamp: 0,
 							Value:     2,
 						},
 					},
-				},
+					source.DefaultResultKeys(),
+				),
 			},
 			countMap: map[NodeIdentifier]float64{
 				{
@@ -900,62 +902,65 @@ func TestAssetCustompricing(t *testing.T) {
 	startTimestamp := float64(windowStart.Unix())
 
 	nodePromResult := []*source.QueryResult{
-		{
-			Metric: map[string]interface{}{
+		source.NewQueryResult(
+			map[string]interface{}{
 				"cluster_id":    "cluster1",
 				"node":          "node1",
 				"instance_type": "type1",
 				"provider_id":   "provider1",
 			},
-			Values: []*util.Vector{
+			[]*util.Vector{
 				{
 					Timestamp: startTimestamp,
 					Value:     0.5,
 				},
 			},
-		},
+			source.DefaultResultKeys(),
+		),
 	}
 
 	pvCostPromResult := []*source.QueryResult{
-		{
-			Metric: map[string]interface{}{
+		source.NewQueryResult(
+			map[string]interface{}{
 				"cluster_id":       "cluster1",
 				"persistentvolume": "pvc1",
 				"provider_id":      "provider1",
 			},
-			Values: []*util.Vector{
+			[]*util.Vector{
 				{
 					Timestamp: startTimestamp,
 					Value:     1.0,
 				},
 			},
-		},
+			source.DefaultResultKeys(),
+		),
 	}
 
 	pvSizePromResult := []*source.QueryResult{
-		{
-			Metric: map[string]interface{}{
+		source.NewQueryResult(
+			map[string]interface{}{
 				"cluster_id":       "cluster1",
 				"persistentvolume": "pvc1",
 				"provider_id":      "provider1",
 			},
-			Values: []*util.Vector{
+			[]*util.Vector{
 				{
 					Timestamp: startTimestamp,
 					Value:     1073741824.0,
 				},
 			},
-		},
+			source.DefaultResultKeys(),
+		),
 	}
 
 	pvMinsPromResult := []*source.QueryResult{
-		{
-			Metric: map[string]interface{}{
+		source.NewQueryResult(
+			map[string]interface{}{
 				"cluster_id":       "cluster1",
 				"persistentvolume": "pvc1",
 				"provider_id":      "provider1",
 			},
-			Values: []*util.Vector{
+			[]*util.Vector{
 				{
 					Timestamp: startTimestamp,
 					Value:     1.0,
@@ -965,17 +970,18 @@ func TestAssetCustompricing(t *testing.T) {
 					Value:     1.0,
 				},
 			},
-		},
+			source.DefaultResultKeys(),
+		),
 	}
 
 	pvAvgUsagePromResult := []*source.QueryResult{
-		{
-			Metric: map[string]interface{}{
+		source.NewQueryResult(
+			map[string]interface{}{
 				"cluster_id":            "cluster1",
 				"persistentvolumeclaim": "pv-claim1",
 				"namespace":             "ns1",
 			},
-			Values: []*util.Vector{
+			[]*util.Vector{
 				{
 					Timestamp: startTimestamp,
 					Value:     1.0,
@@ -985,17 +991,18 @@ func TestAssetCustompricing(t *testing.T) {
 					Value:     1.0,
 				},
 			},
-		},
+			source.DefaultResultKeys(),
+		),
 	}
 
 	pvMaxUsagePromResult := []*source.QueryResult{
-		{
-			Metric: map[string]interface{}{
+		source.NewQueryResult(
+			map[string]interface{}{
 				"cluster_id":            "cluster1",
 				"persistentvolumeclaim": "pv-claim1",
 				"namespace":             "ns1",
 			},
-			Values: []*util.Vector{
+			[]*util.Vector{
 				{
 					Timestamp: startTimestamp,
 					Value:     1.0,
@@ -1005,24 +1012,26 @@ func TestAssetCustompricing(t *testing.T) {
 					Value:     1.0,
 				},
 			},
-		},
+			source.DefaultResultKeys(),
+		),
 	}
 
 	pvInfoPromResult := []*source.QueryResult{
-		{
-			Metric: map[string]interface{}{
+		source.NewQueryResult(
+			map[string]interface{}{
 				"cluster_id":            "cluster1",
 				"persistentvolumeclaim": "pv-claim1",
 				"volumename":            "pvc1",
 				"namespace":             "ns1",
 			},
-			Values: []*util.Vector{
+			[]*util.Vector{
 				{
 					Timestamp: startTimestamp,
 					Value:     1.0,
 				},
 			},
-		},
+			source.DefaultResultKeys(),
+		),
 	}
 
 	gpuCountMap := map[NodeIdentifier]float64{
@@ -1123,8 +1132,8 @@ func TestBuildLabelsMap(t *testing.T) {
 	startTimestamp := float64(windowStart.Unix())
 
 	nodePromResult := []*source.QueryResult{
-		{
-			Metric: map[string]interface{}{
+		source.NewQueryResult(
+			map[string]interface{}{
 				"cluster_id":             "cluster1",
 				"node":                   "node1",
 				"instance_type":          "type1",
@@ -1132,15 +1141,16 @@ func TestBuildLabelsMap(t *testing.T) {
 				"label_testlabelkey1":    "testlabel1-value",
 				"label_test-label-key-2": "testlabel2.value",
 			},
-			Values: []*util.Vector{
+			[]*util.Vector{
 				{
 					Timestamp: startTimestamp,
 					Value:     0.5,
 				},
 			},
-		},
-		{
-			Metric: map[string]interface{}{
+			source.DefaultResultKeys(),
+		),
+		source.NewQueryResult(
+			map[string]interface{}{
 				"cluster_id":             "cluster1",
 				"node":                   "node2",
 				"instance_type":          "type1",
@@ -1148,13 +1158,14 @@ func TestBuildLabelsMap(t *testing.T) {
 				"label_testlabelkey1":    "testlabel1-value",
 				"label_test-label-key-2": "testlabel2.value",
 			},
-			Values: []*util.Vector{
+			[]*util.Vector{
 				{
 					Timestamp: startTimestamp,
 					Value:     0.5,
 				},
 			},
-		},
+			source.DefaultResultKeys(),
+		),
 	}
 
 	nodeLabelMap := buildLabelsMap(nodePromResult)