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