@@ -14,10 +14,12 @@ import (
1414var dumpMetricsFlag = flag .Bool ("dumpMetrics" , false , "" )
1515var printExtraMetrics = flag .Bool ("extraMetrics" , false , "" )
1616var printMultipleLabels = flag .Bool ("multipleLabels" , false , "" )
17+ var endpointFlag = flag .String ("endpoint" , "" , "" )
1718
1819type Metric struct {
19- name string
20- labels string
20+ name string
21+ labelsRawStr string
22+ labelsWithValues []string
2123}
2224
2325type MetricsCollection struct {
@@ -86,13 +88,25 @@ func TestDumpMetrics(t *testing.T) {
8688 return
8789 }
8890
89- newMetrics , err := getMetrics (updatedExporterFileName )
91+ var ep string
92+ switch * endpointFlag {
93+ case "hr" :
94+ ep = highResolutionEndpoint
95+ case "mr" :
96+ ep = medResolutionEndpoint
97+ case "lr" :
98+ ep = lowResolutionEndpoint
99+ default :
100+ ep = "metrics"
101+ }
102+
103+ newMetrics , err := getMetricsFrom (updatedExporterFileName , ep )
90104 if err != nil {
91105 t .Error (err )
92106 return
93107 }
94108
95- oldMetrics , err := getMetrics (oldExporterFileName )
109+ oldMetrics , err := getMetricsFrom (oldExporterFileName , ep )
96110 if err != nil {
97111 t .Error (err )
98112 return
@@ -202,20 +216,50 @@ func testResolution(t *testing.T, resolutionEp, resolutionName string) {
202216
203217 missingCount := 0
204218 missingMetrics := ""
205- for _ , metric := range oldMetricsCollection .MetricNamesWithLabels {
206- if metric == "" || strings .HasPrefix (metric , "# " ) {
219+ missingLabelsCount := 0
220+ missingLabels := ""
221+ for _ , oldMetric := range oldMetricsCollection .MetricsData {
222+ if oldMetric .name == "" || strings .HasPrefix (oldMetric .name , "# " ) {
207223 continue
208224 }
209225
210- if ! contains (newMetricsCollection .MetricNamesWithLabels , metric ) {
226+ metricFound := false
227+ labelsMatch := false
228+ for _ , newMetric := range newMetricsCollection .MetricsData {
229+ if newMetric .name != oldMetric .name {
230+ continue
231+ }
232+
233+ metricFound = true
234+
235+ if newMetric .labelsRawStr == oldMetric .labelsRawStr {
236+ labelsMatch = true
237+ break
238+ }
239+
240+ if arrIsSubsetOf (oldMetric .labelsWithValues , newMetric .labelsWithValues ) {
241+ labelsMatch = true
242+ break
243+ }
244+ }
245+
246+ if ! metricFound {
211247 missingCount ++
212- missingMetrics += fmt .Sprintf ("%s\n " , metric )
248+ missingMetrics += fmt .Sprintf ("%s\n " , oldMetric .name )
249+ } else if ! labelsMatch {
250+ missingLabelsCount ++
251+ missingLabels += fmt .Sprintf ("%s\n " , oldMetric .name )
213252 }
214253 }
254+
215255 if missingCount > 0 {
216256 t .Errorf ("%d metrics are missing in new exporter for %s resolution:\n %s" , missingCount , resolutionName , missingMetrics )
217257 }
218258
259+ if missingLabelsCount > 0 {
260+ t .Errorf ("%d metrics's labels missing in new exporter for %s resolution:\n %s" , missingCount , resolutionName , missingLabels )
261+ }
262+
219263 extraCount := 0
220264 extraMetrics := ""
221265 for _ , metric := range newMetricsCollection .MetricNamesWithLabels {
@@ -362,13 +406,13 @@ func parseMetricsCollection(metricRaw string) MetricsCollection {
362406 }
363407}
364408
365- func arrIsSubsetOf (a , b []string ) bool {
366- if len (a ) == 0 {
367- return len (b ) == 0
409+ func arrIsSubsetOf (smaller , larger []string ) bool {
410+ if len (smaller ) == 0 {
411+ return len (larger ) == 0
368412 }
369413
370- for _ , x := range a {
371- if ! contains (b , x ) {
414+ for _ , x := range smaller {
415+ if ! contains (larger , x ) {
372416 return false
373417 }
374418 }
@@ -394,10 +438,10 @@ func groupByMetrics(metrics []Metric) map[string][]string {
394438 metric := metrics [i ]
395439 if _ , ok := mtr [metric .name ]; ok {
396440 labels := mtr [metric .name ]
397- labels = append (labels , metric .labels )
441+ labels = append (labels , metric .labelsRawStr )
398442 mtr [metric .name ] = labels
399443 } else {
400- mtr [metric .name ] = []string {metric .labels }
444+ mtr [metric .name ] = []string {metric .labelsRawStr }
401445 }
402446 }
403447
@@ -414,16 +458,21 @@ func parseMetrics(metrics []string) []Metric {
414458 }
415459
416460 var mName , mLabels string
461+ var labelsArr []string
417462 if strings .Contains (metricRawStr , "{" ) {
418463 mName = metricRawStr [:strings .Index (metricRawStr , "{" )]
419464 mLabels = metricRawStr [strings .Index (metricRawStr , "{" )+ 1 : len (metricRawStr )- 1 ]
465+ if mLabels != "" {
466+ labelsArr = strings .Split (mLabels , "," )
467+ }
420468 } else {
421469 mName = metricRawStr
422470 }
423471
424472 metric := Metric {
425- name : mName ,
426- labels : mLabels ,
473+ name : mName ,
474+ labelsRawStr : mLabels ,
475+ labelsWithValues : labelsArr ,
427476 }
428477
429478 metricsData = append (metricsData , metric )
0 commit comments