@@ -14,10 +14,12 @@ import (
14
14
var dumpMetricsFlag = flag .Bool ("dumpMetrics" , false , "" )
15
15
var printExtraMetrics = flag .Bool ("extraMetrics" , false , "" )
16
16
var printMultipleLabels = flag .Bool ("multipleLabels" , false , "" )
17
+ var endpointFlag = flag .String ("endpoint" , "" , "" )
17
18
18
19
type Metric struct {
19
- name string
20
- labels string
20
+ name string
21
+ labelsRawStr string
22
+ labelsWithValues []string
21
23
}
22
24
23
25
type MetricsCollection struct {
@@ -86,13 +88,25 @@ func TestDumpMetrics(t *testing.T) {
86
88
return
87
89
}
88
90
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 )
90
104
if err != nil {
91
105
t .Error (err )
92
106
return
93
107
}
94
108
95
- oldMetrics , err := getMetrics (oldExporterFileName )
109
+ oldMetrics , err := getMetricsFrom (oldExporterFileName , ep )
96
110
if err != nil {
97
111
t .Error (err )
98
112
return
@@ -202,20 +216,50 @@ func testResolution(t *testing.T, resolutionEp, resolutionName string) {
202
216
203
217
missingCount := 0
204
218
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 , "# " ) {
207
223
continue
208
224
}
209
225
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 {
211
247
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 )
213
252
}
214
253
}
254
+
215
255
if missingCount > 0 {
216
256
t .Errorf ("%d metrics are missing in new exporter for %s resolution:\n %s" , missingCount , resolutionName , missingMetrics )
217
257
}
218
258
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
+
219
263
extraCount := 0
220
264
extraMetrics := ""
221
265
for _ , metric := range newMetricsCollection .MetricNamesWithLabels {
@@ -362,13 +406,13 @@ func parseMetricsCollection(metricRaw string) MetricsCollection {
362
406
}
363
407
}
364
408
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
368
412
}
369
413
370
- for _ , x := range a {
371
- if ! contains (b , x ) {
414
+ for _ , x := range smaller {
415
+ if ! contains (larger , x ) {
372
416
return false
373
417
}
374
418
}
@@ -394,10 +438,10 @@ func groupByMetrics(metrics []Metric) map[string][]string {
394
438
metric := metrics [i ]
395
439
if _ , ok := mtr [metric .name ]; ok {
396
440
labels := mtr [metric .name ]
397
- labels = append (labels , metric .labels )
441
+ labels = append (labels , metric .labelsRawStr )
398
442
mtr [metric .name ] = labels
399
443
} else {
400
- mtr [metric .name ] = []string {metric .labels }
444
+ mtr [metric .name ] = []string {metric .labelsRawStr }
401
445
}
402
446
}
403
447
@@ -414,16 +458,21 @@ func parseMetrics(metrics []string) []Metric {
414
458
}
415
459
416
460
var mName , mLabels string
461
+ var labelsArr []string
417
462
if strings .Contains (metricRawStr , "{" ) {
418
463
mName = metricRawStr [:strings .Index (metricRawStr , "{" )]
419
464
mLabels = metricRawStr [strings .Index (metricRawStr , "{" )+ 1 : len (metricRawStr )- 1 ]
465
+ if mLabels != "" {
466
+ labelsArr = strings .Split (mLabels , "," )
467
+ }
420
468
} else {
421
469
mName = metricRawStr
422
470
}
423
471
424
472
metric := Metric {
425
- name : mName ,
426
- labels : mLabels ,
473
+ name : mName ,
474
+ labelsRawStr : mLabels ,
475
+ labelsWithValues : labelsArr ,
427
476
}
428
477
429
478
metricsData = append (metricsData , metric )
0 commit comments