Skip to content

Commit f0133c0

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 f447422 commit f0133c0

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

prometheus/testutil/testutil.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,20 @@ 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-
return fmt.Errorf("expected metrics name not found")
281+
h := make(map[string]struct{})
282+
for _, mf := range got {
283+
if mf == nil {
284+
continue
285+
}
286+
h[mf.GetName()] = struct{}{}
287+
}
288+
var missingMetricNames []string
289+
for _, name := range metricNames {
290+
if _, ok := h[name]; !ok {
291+
missingMetricNames = append(missingMetricNames, name)
292+
}
293+
}
294+
return fmt.Errorf("expected metric name(s) not found: %v", missingMetricNames)
282295
}
283296
}
284297

prometheus/testutil/testutil_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ func TestScrapeAndCompare(t *testing.T) {
379379
some_total2{ label2 = "value2" } 1
380380
`,
381381
metricNames: []string{"some_total3"},
382-
errPrefix: "expected metrics name not found",
382+
errPrefix: "expected metric name(s) not found",
383383
fail: true,
384384
},
385385
"one of multiple expected metric names is not scraped": {
@@ -395,7 +395,7 @@ func TestScrapeAndCompare(t *testing.T) {
395395
some_total2{ label2 = "value2" } 1
396396
`,
397397
metricNames: []string{"some_total1", "some_total3"},
398-
errPrefix: "expected metrics name not found",
398+
errPrefix: "expected metric name(s) not found",
399399
fail: true,
400400
},
401401
}

0 commit comments

Comments
 (0)