Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize empty help values in CollectAndCompare #1378

Merged
merged 1 commit into from Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions prometheus/testutil/testutil.go
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
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