Skip to content

Commit 437f988

Browse files
leonnicolasbwplotka
andcommitted
Apply suggestions from code review
- remove if nil check - use two nested loops instead of map - use new function `hasMetricByName` for readability Co-authored-by: Bartlomiej Plotka <bwplotka@gmail.com> Signed-off-by: leonnicolas <60091705+leonnicolas@users.noreply.github.com>
1 parent d4090e9 commit 437f988

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

prometheus/testutil/testutil.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -255,16 +255,9 @@ func compareMetricFamilies(got, expected []*dto.MetricFamily, metricNames ...str
255255
got = filterMetrics(got, metricNames)
256256
expected = filterMetrics(expected, metricNames)
257257
if len(metricNames) > len(got) {
258-
h := make(map[string]struct{})
259-
for _, mf := range got {
260-
if mf == nil {
261-
continue
262-
}
263-
h[mf.GetName()] = struct{}{}
264-
}
265258
var missingMetricNames []string
266259
for _, name := range metricNames {
267-
if _, ok := h[name]; !ok {
260+
if ok := hasMetricByName(got, name); !ok {
268261
missingMetricNames = append(missingMetricNames, name)
269262
}
270263
}
@@ -372,3 +365,12 @@ func filterMetrics(metrics []*dto.MetricFamily, names []string) []*dto.MetricFam
372365
}
373366
return filtered
374367
}
368+
369+
func hasMetricByName(metrics []*dto.MetricFamily, name string) bool {
370+
for _, mf := range metrics {
371+
if mf.GetName() == name {
372+
return true
373+
}
374+
}
375+
return false
376+
}

prometheus/testutil/testutil_test.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,8 @@ func TestScrapeAndCompare(t *testing.T) {
335335
scenarios := map[string]struct {
336336
want string
337337
metricNames []string
338-
errPrefix string
339-
fail bool
338+
// expectedErrPrefix if empty, means no fail is expected for the comparison.
339+
expectedErrPrefix string
340340
}{
341341
"empty metric Names": {
342342
want: `
@@ -382,9 +382,8 @@ func TestScrapeAndCompare(t *testing.T) {
382382
383383
some_total2{ label2 = "value2" } 1
384384
`,
385-
metricNames: []string{"some_total3"},
386-
errPrefix: "expected metric name(s) not found",
387-
fail: true,
385+
metricNames: []string{"some_total3"},
386+
expectedErrPrefix: "expected metric name(s) not found",
388387
},
389388
"one of multiple expected metric names is not scraped": {
390389
want: `
@@ -398,9 +397,8 @@ func TestScrapeAndCompare(t *testing.T) {
398397
399398
some_total2{ label2 = "value2" } 1
400399
`,
401-
metricNames: []string{"some_total1", "some_total3"},
402-
errPrefix: "expected metric name(s) not found",
403-
fail: true,
400+
metricNames: []string{"some_total1", "some_total3"},
401+
expectedErrPrefix: "expected metric name(s) not found",
404402
},
405403
}
406404
for name, scenario := range scenarios {
@@ -412,10 +410,10 @@ func TestScrapeAndCompare(t *testing.T) {
412410
}))
413411
defer ts.Close()
414412
if err := ScrapeAndCompare(ts.URL, expectedReader, scenario.metricNames...); err != nil {
415-
if !scenario.fail || !strings.HasPrefix(err.Error(), scenario.errPrefix) {
413+
if scenario.expectedErrPrefix == "" || !strings.HasPrefix(err.Error(), scenario.expectedErrPrefix) {
416414
t.Errorf("unexpected error happened: %s", err)
417415
}
418-
} else if scenario.fail {
416+
} else if scenario.expectedErrPrefix != "" {
419417
t.Errorf("expected an error but got nil")
420418
}
421419
})

0 commit comments

Comments
 (0)