Skip to content

Commit

Permalink
Add missing metricNames to error
Browse files Browse the repository at this point in the history
In to see which metric names are missing, we can add them to the error
message.

Signed-off-by: leonnicolas <leonloechner@gmx.de>
  • Loading branch information
leonnicolas committed Jan 8, 2024
1 parent 7163ac9 commit d4090e9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
15 changes: 14 additions & 1 deletion prometheus/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,20 @@ func compareMetricFamilies(got, expected []*dto.MetricFamily, metricNames ...str
got = filterMetrics(got, metricNames)
expected = filterMetrics(expected, metricNames)
if len(metricNames) > len(got) {
return fmt.Errorf("expected metrics name not found")
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 {
missingMetricNames = append(missingMetricNames, name)
}
}
return fmt.Errorf("expected metric name(s) not found: %v", missingMetricNames)
}
}

Expand Down
4 changes: 2 additions & 2 deletions prometheus/testutil/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ func TestScrapeAndCompare(t *testing.T) {
some_total2{ label2 = "value2" } 1
`,
metricNames: []string{"some_total3"},
errPrefix: "expected metrics name not found",
errPrefix: "expected metric name(s) not found",
fail: true,
},
"one of multiple expected metric names is not scraped": {
Expand All @@ -399,7 +399,7 @@ func TestScrapeAndCompare(t *testing.T) {
some_total2{ label2 = "value2" } 1
`,
metricNames: []string{"some_total1", "some_total3"},
errPrefix: "expected metrics name not found",
errPrefix: "expected metric name(s) not found",
fail: true,
},
}
Expand Down

0 comments on commit d4090e9

Please sign in to comment.