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

Add CollectAndFormat to testutil, allowing caller to assert as they want to on the exported metric #1503

Merged
merged 1 commit into from
May 9, 2024
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
30 changes: 27 additions & 3 deletions prometheus/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,8 @@ func ScrapeAndCompare(url string, expected io.Reader, metricNames ...string) err
return compareMetricFamilies(scraped, wanted, metricNames...)
}

// CollectAndCompare registers the provided Collector with a newly created
// pedantic Registry. It then calls GatherAndCompare with that Registry and with
// the provided metricNames.
// CollectAndCompare collects the metrics identified by `metricNames` and compares them in the Prometheus text
// exposition format to the data read from expected.
func CollectAndCompare(c prometheus.Collector, expected io.Reader, metricNames ...string) error {
reg := prometheus.NewPedanticRegistry()
if err := reg.Register(c); err != nil {
Expand Down Expand Up @@ -221,6 +220,31 @@ func TransactionalGatherAndCompare(g prometheus.TransactionalGatherer, expected
return compareMetricFamilies(got, wanted, metricNames...)
}

// CollectAndFormat collects the metrics identified by `metricNames` and returns them in the given format.
func CollectAndFormat(c prometheus.Collector, format expfmt.FormatType, metricNames ...string) ([]byte, error) {
reg := prometheus.NewPedanticRegistry()
if err := reg.Register(c); err != nil {
return nil, fmt.Errorf("registering collector failed: %w", err)
}

gotFiltered, err := reg.Gather()
if err != nil {
return nil, fmt.Errorf("gathering metrics failed: %w", err)
}

gotFiltered = filterMetrics(gotFiltered, metricNames)

var gotFormatted bytes.Buffer
enc := expfmt.NewEncoder(&gotFormatted, expfmt.NewFormat(format))
for _, mf := range gotFiltered {
if err := enc.Encode(mf); err != nil {
return nil, fmt.Errorf("encoding gathered metrics failed: %w", err)
}
}

return gotFormatted.Bytes(), nil
}

// convertReaderToMetricFamily would read from a io.Reader object and convert it to a slice of
// dto.MetricFamily.
func convertReaderToMetricFamily(reader io.Reader) ([]*dto.MetricFamily, error) {
Expand Down
31 changes: 31 additions & 0 deletions prometheus/testutil/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"strings"
"testing"

"github.com/prometheus/common/expfmt"

"github.com/prometheus/client_golang/prometheus"
)

Expand Down Expand Up @@ -431,3 +433,32 @@ func TestCollectAndCount(t *testing.T) {
t.Errorf("unexpected metric count, got %d, want %d", got, want)
}
}

func TestCollectAndFormat(t *testing.T) {
const expected = `# HELP foo_bar A value that represents the number of bars in foo.
# TYPE foo_bar counter
foo_bar{fizz="bang"} 1
`
c := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "foo_bar",
Help: "A value that represents the number of bars in foo.",
},
[]string{"fizz"},
)
c.WithLabelValues("bang").Inc()

got, err := CollectAndFormat(c, expfmt.TypeTextPlain, "foo_bar")
if err != nil {
t.Errorf("unexpected error: %s", err.Error())
}

gotS := string(got)
if err != nil {
t.Errorf("unexpected error: %s", err.Error())
}

if gotS != expected {
t.Errorf("unexpected metric output, got %q, expected %q", gotS, expected)
}
}