Skip to content

Commit

Permalink
Normalize empty help values in CollectAndCompare (#1378)
Browse files Browse the repository at this point in the history
Due to an inconsistency in the text protocol between encoding and
decoding, it was not possible to use the testutil.CollectAndCompare
function to test metrics with empty help values. To fix this, normalize
empty help values from the expected/want side of the test so that they
compare correctly with empty values on the actual/got side of the test.

Signed-off-by: Billy Keyes <bluekeyes@gmail.com>
  • Loading branch information
bluekeyes authored Nov 20, 2023
1 parent 3f80cd1 commit 80d3f0b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
15 changes: 15 additions & 0 deletions prometheus/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
"github.com/davecgh/go-spew/spew"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
"google.golang.org/protobuf/proto"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/internal"
Expand Down Expand Up @@ -230,6 +231,20 @@ func convertReaderToMetricFamily(reader io.Reader) ([]*dto.MetricFamily, error)
return nil, fmt.Errorf("converting reader to metric families failed: %w", err)
}

// The text protocol handles empty help fields inconsistently. When
// encoding, any non-nil value, include the empty string, produces a
// "# HELP" line. But when decoding, the help field is only set to a
// non-nil value if the "# HELP" line contains a non-empty value.
//
// Because metrics in a registry always have non-nil help fields, populate
// any nil help fields in the parsed metrics with the empty string so that
// when we compare text encodings, the results are consistent.
for _, metric := range notNormalized {
if metric.Help == nil {
metric.Help = proto.String("")
}
}

return internal.NormalizeMetricFamilies(notNormalized), nil
}

Expand Down
20 changes: 20 additions & 0 deletions prometheus/testutil/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,26 @@ func TestCollectAndCompareNoLabel(t *testing.T) {
}
}

func TestCollectAndCompareNoHelp(t *testing.T) {
const metadata = `
# TYPE some_total counter
`

c := prometheus.NewCounter(prometheus.CounterOpts{
Name: "some_total",
})
c.Inc()

expected := `
some_total 1
`

if err := CollectAndCompare(c, strings.NewReader(metadata+expected), "some_total"); err != nil {
t.Errorf("unexpected collecting result:\n%s", err)
}
}

func TestCollectAndCompareHistogram(t *testing.T) {
inputs := []struct {
name string
Expand Down

0 comments on commit 80d3f0b

Please sign in to comment.