@@ -23,7 +23,6 @@ import (
23
23
24
24
"github.com/onsi/ginkgo"
25
25
"github.com/onsi/gomega"
26
- "github.com/prometheus/common/model"
27
26
28
27
v1 "k8s.io/api/core/v1"
29
28
storagev1 "k8s.io/api/storage/v1"
@@ -283,8 +282,8 @@ var _ = utils.SIGDescribe("[Serial] Volume metrics", func() {
283
282
284
283
metricKey := "volume_operation_total_seconds_count"
285
284
dimensions := []string {"operation_name" , "plugin_name" }
286
- valid := hasValidMetrics (testutil .Metrics (controllerMetrics ), metricKey , dimensions ... )
287
- framework .ExpectEqual ( valid , true , "Invalid metric in P/V Controller metrics: %q" , metricKey )
285
+ err = testutil . ValidateMetrics (testutil .Metrics (controllerMetrics ), metricKey , dimensions ... )
286
+ framework .ExpectNoError ( err , "Invalid metric in P/V Controller metrics: %q" , metricKey )
288
287
289
288
framework .Logf ("Deleting pod %q/%q" , pod .Namespace , pod .Name )
290
289
framework .ExpectNoError (e2epod .DeletePodWithWait (c , pod ))
@@ -313,8 +312,8 @@ var _ = utils.SIGDescribe("[Serial] Volume metrics", func() {
313
312
// Metrics should have dimensions plugin_name and state available
314
313
totalVolumesKey := "volume_manager_total_volumes"
315
314
dimensions := []string {"state" , "plugin_name" }
316
- valid := hasValidMetrics (testutil .Metrics (kubeMetrics ), totalVolumesKey , dimensions ... )
317
- framework .ExpectEqual ( valid , true , "Invalid metric in Volume Manager metrics: %q" , totalVolumesKey )
315
+ err = testutil . ValidateMetrics (testutil .Metrics (kubeMetrics ), totalVolumesKey , dimensions ... )
316
+ framework .ExpectNoError ( err , "Invalid metric in Volume Manager metrics: %q" , totalVolumesKey )
318
317
319
318
framework .Logf ("Deleting pod %q/%q" , pod .Namespace , pod .Name )
320
319
framework .ExpectNoError (e2epod .DeletePodWithWait (c , pod ))
@@ -441,7 +440,7 @@ var _ = utils.SIGDescribe("[Serial] Volume metrics", func() {
441
440
// Concretely, we expect the difference of the updated values and original values for each
442
441
// test suit are equal to expectValues.
443
442
actualValues := calculateRelativeValues (originMetricValues [i ],
444
- getPVControllerMetrics ( controllerMetrics , metric .name , metric .dimension ))
443
+ testutil . GetMetricValuesForLabel ( testutil . Metrics ( controllerMetrics ) , metric .name , metric .dimension ))
445
444
framework .ExpectEqual (actualValues , expectValues , "Wrong pv controller metric %s(%s): wanted %v, got %v" ,
446
445
metric .name , metric .dimension , expectValues , actualValues )
447
446
}
@@ -460,7 +459,7 @@ var _ = utils.SIGDescribe("[Serial] Volume metrics", func() {
460
459
framework .ExpectNoError (err , "Error getting c-m metricValues: %v" , err )
461
460
for _ , metric := range metrics {
462
461
originMetricValues = append (originMetricValues ,
463
- getPVControllerMetrics ( controllerMetrics , metric .name , metric .dimension ))
462
+ testutil . GetMetricValuesForLabel ( testutil . Metrics ( controllerMetrics ) , metric .name , metric .dimension ))
464
463
}
465
464
})
466
465
@@ -696,27 +695,12 @@ func waitForPVControllerSync(metricsGrabber *metrics.Grabber, metricName, dimens
696
695
framework .Logf ("Error fetching controller-manager metrics" )
697
696
return false , err
698
697
}
699
- return len (getPVControllerMetrics ( updatedMetrics , metricName , dimension )) > 0 , nil
698
+ return len (testutil . GetMetricValuesForLabel ( testutil . Metrics ( updatedMetrics ) , metricName , dimension )) > 0 , nil
700
699
}
701
700
waitErr := wait .ExponentialBackoff (backoff , verifyMetricFunc )
702
701
framework .ExpectNoError (waitErr , "Unable to get pv controller metrics" )
703
702
}
704
703
705
- func getPVControllerMetrics (ms metrics.ControllerManagerMetrics , metricName , dimension string ) map [string ]int64 {
706
- result := make (map [string ]int64 )
707
- for method , samples := range ms {
708
- if method != metricName {
709
- continue
710
- }
711
- for _ , sample := range samples {
712
- count := int64 (sample .Value )
713
- dimensionName := string (sample .Metric [model .LabelName (dimension )])
714
- result [dimensionName ] = count
715
- }
716
- }
717
- return result
718
- }
719
-
720
704
func calculateRelativeValues (originValues , updatedValues map [string ]int64 ) map [string ]int64 {
721
705
relativeValues := make (map [string ]int64 )
722
706
for key , value := range updatedValues {
@@ -733,26 +717,6 @@ func calculateRelativeValues(originValues, updatedValues map[string]int64) map[s
733
717
return relativeValues
734
718
}
735
719
736
- func hasValidMetrics (metrics testutil.Metrics , metricKey string , dimensions ... string ) bool {
737
- var errCount int
738
- framework .Logf ("Looking for sample in metric %q" , metricKey )
739
- samples , ok := metrics [metricKey ]
740
- if ! ok {
741
- framework .Logf ("Key %q was not found in metrics" , metricKey )
742
- return false
743
- }
744
- for _ , sample := range samples {
745
- framework .Logf ("Found sample %q" , sample .String ())
746
- for _ , d := range dimensions {
747
- if _ , ok := sample .Metric [model .LabelName (d )]; ! ok {
748
- framework .Logf ("Error getting dimension %q for metric %q, sample %q" , d , metricKey , sample .String ())
749
- errCount ++
750
- }
751
- }
752
- }
753
- return errCount == 0
754
- }
755
-
756
720
func getStatesMetrics (metricKey string , givenMetrics testutil.Metrics ) map [string ]map [string ]int64 {
757
721
states := make (map [string ]map [string ]int64 )
758
722
for _ , sample := range givenMetrics [metricKey ] {
@@ -776,8 +740,9 @@ func waitForADControllerStatesMetrics(metricsGrabber *metrics.Grabber, metricNam
776
740
e2eskipper .Skipf ("Could not get controller-manager metrics - skipping" )
777
741
return false , err
778
742
}
779
- if ! hasValidMetrics (testutil .Metrics (updatedMetrics ), metricName , dimensions ... ) {
780
- return false , fmt .Errorf ("could not get valid metrics for %q" , metricName )
743
+ err = testutil .ValidateMetrics (testutil .Metrics (updatedMetrics ), metricName , dimensions ... )
744
+ if err != nil {
745
+ return false , fmt .Errorf ("could not get valid metrics: %v " , err )
781
746
}
782
747
states := getStatesMetrics (metricName , testutil .Metrics (updatedMetrics ))
783
748
for _ , name := range stateNames {
0 commit comments