Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
- 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>
  • Loading branch information
leonnicolas and bwplotka committed Feb 7, 2024
1 parent d4090e9 commit 437f988
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
18 changes: 10 additions & 8 deletions prometheus/testutil/testutil.go
Expand Up @@ -255,16 +255,9 @@ func compareMetricFamilies(got, expected []*dto.MetricFamily, metricNames ...str
got = filterMetrics(got, metricNames)
expected = filterMetrics(expected, metricNames)
if len(metricNames) > len(got) {
h := make(map[string]struct{})
for _, mf := range got {
if mf == nil {
continue
}
h[mf.GetName()] = struct{}{}
}
var missingMetricNames []string
for _, name := range metricNames {
if _, ok := h[name]; !ok {
if ok := hasMetricByName(got, name); !ok {
missingMetricNames = append(missingMetricNames, name)
}
}
Expand Down Expand Up @@ -372,3 +365,12 @@ func filterMetrics(metrics []*dto.MetricFamily, names []string) []*dto.MetricFam
}
return filtered
}

func hasMetricByName(metrics []*dto.MetricFamily, name string) bool {
for _, mf := range metrics {
if mf.GetName() == name {
return true
}
}
return false
}
18 changes: 8 additions & 10 deletions prometheus/testutil/testutil_test.go
Expand Up @@ -335,8 +335,8 @@ func TestScrapeAndCompare(t *testing.T) {
scenarios := map[string]struct {
want string
metricNames []string
errPrefix string
fail bool
// expectedErrPrefix if empty, means no fail is expected for the comparison.
expectedErrPrefix string
}{
"empty metric Names": {
want: `
Expand Down Expand Up @@ -382,9 +382,8 @@ func TestScrapeAndCompare(t *testing.T) {
some_total2{ label2 = "value2" } 1
`,
metricNames: []string{"some_total3"},
errPrefix: "expected metric name(s) not found",
fail: true,
metricNames: []string{"some_total3"},
expectedErrPrefix: "expected metric name(s) not found",
},
"one of multiple expected metric names is not scraped": {
want: `
Expand All @@ -398,9 +397,8 @@ func TestScrapeAndCompare(t *testing.T) {
some_total2{ label2 = "value2" } 1
`,
metricNames: []string{"some_total1", "some_total3"},
errPrefix: "expected metric name(s) not found",
fail: true,
metricNames: []string{"some_total1", "some_total3"},
expectedErrPrefix: "expected metric name(s) not found",
},
}
for name, scenario := range scenarios {
Expand All @@ -412,10 +410,10 @@ func TestScrapeAndCompare(t *testing.T) {
}))
defer ts.Close()
if err := ScrapeAndCompare(ts.URL, expectedReader, scenario.metricNames...); err != nil {
if !scenario.fail || !strings.HasPrefix(err.Error(), scenario.errPrefix) {
if scenario.expectedErrPrefix == "" || !strings.HasPrefix(err.Error(), scenario.expectedErrPrefix) {
t.Errorf("unexpected error happened: %s", err)
}
} else if scenario.fail {
} else if scenario.expectedErrPrefix != "" {
t.Errorf("expected an error but got nil")
}
})
Expand Down

0 comments on commit 437f988

Please sign in to comment.