Skip to content

Commit 9490310

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 <[email protected]> Signed-off-by: leonnicolas <[email protected]>
1 parent f0133c0 commit 9490310

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
@@ -278,16 +278,9 @@ func compareMetricFamilies(got, expected []*dto.MetricFamily, metricNames ...str
278278
got = filterMetrics(got, metricNames)
279279
expected = filterMetrics(expected, metricNames)
280280
if len(metricNames) > len(got) {
281-
h := make(map[string]struct{})
282-
for _, mf := range got {
283-
if mf == nil {
284-
continue
285-
}
286-
h[mf.GetName()] = struct{}{}
287-
}
288281
var missingMetricNames []string
289282
for _, name := range metricNames {
290-
if _, ok := h[name]; !ok {
283+
if ok := hasMetricByName(got, name); !ok {
291284
missingMetricNames = append(missingMetricNames, name)
292285
}
293286
}
@@ -334,3 +327,12 @@ func filterMetrics(metrics []*dto.MetricFamily, names []string) []*dto.MetricFam
334327
}
335328
return filtered
336329
}
330+
331+
func hasMetricByName(metrics []*dto.MetricFamily, name string) bool {
332+
for _, mf := range metrics {
333+
if mf.GetName() == name {
334+
return true
335+
}
336+
}
337+
return false
338+
}

prometheus/testutil/testutil_test.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,8 @@ func TestScrapeAndCompare(t *testing.T) {
331331
scenarios := map[string]struct {
332332
want string
333333
metricNames []string
334-
errPrefix string
335-
fail bool
334+
// expectedErrPrefix if empty, means no fail is expected for the comparison.
335+
expectedErrPrefix string
336336
}{
337337
"empty metric Names": {
338338
want: `
@@ -378,9 +378,8 @@ func TestScrapeAndCompare(t *testing.T) {
378378
379379
some_total2{ label2 = "value2" } 1
380380
`,
381-
metricNames: []string{"some_total3"},
382-
errPrefix: "expected metric name(s) not found",
383-
fail: true,
381+
metricNames: []string{"some_total3"},
382+
expectedErrPrefix: "expected metric name(s) not found",
384383
},
385384
"one of multiple expected metric names is not scraped": {
386385
want: `
@@ -394,9 +393,8 @@ func TestScrapeAndCompare(t *testing.T) {
394393
395394
some_total2{ label2 = "value2" } 1
396395
`,
397-
metricNames: []string{"some_total1", "some_total3"},
398-
errPrefix: "expected metric name(s) not found",
399-
fail: true,
396+
metricNames: []string{"some_total1", "some_total3"},
397+
expectedErrPrefix: "expected metric name(s) not found",
400398
},
401399
}
402400
for name, scenario := range scenarios {
@@ -408,10 +406,10 @@ func TestScrapeAndCompare(t *testing.T) {
408406
}))
409407
defer ts.Close()
410408
if err := ScrapeAndCompare(ts.URL, expectedReader, scenario.metricNames...); err != nil {
411-
if !scenario.fail || !strings.HasPrefix(err.Error(), scenario.errPrefix) {
409+
if scenario.expectedErrPrefix == "" || !strings.HasPrefix(err.Error(), scenario.expectedErrPrefix) {
412410
t.Errorf("unexpected error happened: %s", err)
413411
}
414-
} else if scenario.fail {
412+
} else if scenario.expectedErrPrefix != "" {
415413
t.Errorf("expected an error but got nil")
416414
}
417415
})

0 commit comments

Comments
 (0)