Skip to content

Commit d4090e9

Browse files
committed
Add missing metricNames to error
In to see which metric names are missing, we can add them to the error message. Signed-off-by: leonnicolas <[email protected]>
1 parent 7163ac9 commit d4090e9

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

prometheus/testutil/testutil.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,20 @@ 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-
return fmt.Errorf("expected metrics name not found")
258+
h := make(map[string]struct{})
259+
for _, mf := range got {
260+
if mf == nil {
261+
continue
262+
}
263+
h[mf.GetName()] = struct{}{}
264+
}
265+
var missingMetricNames []string
266+
for _, name := range metricNames {
267+
if _, ok := h[name]; !ok {
268+
missingMetricNames = append(missingMetricNames, name)
269+
}
270+
}
271+
return fmt.Errorf("expected metric name(s) not found: %v", missingMetricNames)
259272
}
260273
}
261274

prometheus/testutil/testutil_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ func TestScrapeAndCompare(t *testing.T) {
383383
some_total2{ label2 = "value2" } 1
384384
`,
385385
metricNames: []string{"some_total3"},
386-
errPrefix: "expected metrics name not found",
386+
errPrefix: "expected metric name(s) not found",
387387
fail: true,
388388
},
389389
"one of multiple expected metric names is not scraped": {
@@ -399,7 +399,7 @@ func TestScrapeAndCompare(t *testing.T) {
399399
some_total2{ label2 = "value2" } 1
400400
`,
401401
metricNames: []string{"some_total1", "some_total3"},
402-
errPrefix: "expected metrics name not found",
402+
errPrefix: "expected metric name(s) not found",
403403
fail: true,
404404
},
405405
}

0 commit comments

Comments
 (0)