Skip to content

Commit

Permalink
Filter expected metrics as well in CollectAndCompare (#1143)
Browse files Browse the repository at this point in the history
* Filter expected metrics as well in CollectAndCompare

Signed-off-by: Daria Danilenko <[email protected]>

* Add testcase for multiple expected metrics
Signed-off-by: Daria Danilenko <[email protected]>

* Change test values for filtering multiple expected metrics
Signed-off-by: Daria Danilenko <[email protected]>

---------

Signed-off-by: Daria Danilenko <[email protected]>
  • Loading branch information
DariaKunoichi authored Mar 22, 2023
1 parent 5c7821d commit eb33971
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions prometheus/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ func convertReaderToMetricFamily(reader io.Reader) ([]*dto.MetricFamily, error)
func compareMetricFamilies(got, expected []*dto.MetricFamily, metricNames ...string) error {
if metricNames != nil {
got = filterMetrics(got, metricNames)
expected = filterMetrics(expected, metricNames)
}

return compare(got, expected)
Expand Down
25 changes: 25 additions & 0 deletions prometheus/testutil/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,31 @@ func TestScrapeAndCompare(t *testing.T) {
}
}

func TestScrapeAndCompareWithMultipleExpected(t *testing.T) {
const expected = `
# HELP some_total A value that represents a counter.
# TYPE some_total counter
some_total{ label1 = "value1" } 1
# HELP some_total2 A value that represents a counter.
# TYPE some_total2 counter
some_total2{ label2 = "value2" } 1
`

expectedReader := strings.NewReader(expected)

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, expected)
}))
defer ts.Close()

if err := ScrapeAndCompare(ts.URL, expectedReader, "some_total2"); err != nil {
t.Errorf("unexpected scraping result:\n%s", err)
}
}

func TestScrapeAndCompareFetchingFail(t *testing.T) {
err := ScrapeAndCompare("some_url", strings.NewReader("some expectation"), "some_total")
if err == nil {
Expand Down

0 comments on commit eb33971

Please sign in to comment.