Skip to content

Commit d6b4379

Browse files
jcass8695Steven Swartz
authored andcommitted
add: CollectAndFormat to testutil (prometheus#1503)
CollectAndFormat is a helper function that returns the formatted metrics to the caller, allowing them to use it how they want. This is different to CollectAndCompare where the comparison is done strictly on behalf of the caller. Often it is more convenient to perform a simple substring match on the formatted metric. Signed-off-by: Jack Cassidy <[email protected]> Signed-off-by: Steven Swartz <[email protected]>
1 parent 32e1ef8 commit d6b4379

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

prometheus/testutil/testutil.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,8 @@ func ScrapeAndCompare(url string, expected io.Reader, metricNames ...string) err
183183
return compareMetricFamilies(scraped, wanted, metricNames...)
184184
}
185185

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

223+
// CollectAndFormat collects the metrics identified by `metricNames` and returns them in the given format.
224+
func CollectAndFormat(c prometheus.Collector, format expfmt.FormatType, metricNames ...string) ([]byte, error) {
225+
reg := prometheus.NewPedanticRegistry()
226+
if err := reg.Register(c); err != nil {
227+
return nil, fmt.Errorf("registering collector failed: %w", err)
228+
}
229+
230+
gotFiltered, err := reg.Gather()
231+
if err != nil {
232+
return nil, fmt.Errorf("gathering metrics failed: %w", err)
233+
}
234+
235+
gotFiltered = filterMetrics(gotFiltered, metricNames)
236+
237+
var gotFormatted bytes.Buffer
238+
enc := expfmt.NewEncoder(&gotFormatted, expfmt.NewFormat(format))
239+
for _, mf := range gotFiltered {
240+
if err := enc.Encode(mf); err != nil {
241+
return nil, fmt.Errorf("encoding gathered metrics failed: %w", err)
242+
}
243+
}
244+
245+
return gotFormatted.Bytes(), nil
246+
}
247+
224248
// convertReaderToMetricFamily would read from a io.Reader object and convert it to a slice of
225249
// dto.MetricFamily.
226250
func convertReaderToMetricFamily(reader io.Reader) ([]*dto.MetricFamily, error) {

prometheus/testutil/testutil_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"strings"
2121
"testing"
2222

23+
"github.com/prometheus/common/expfmt"
24+
2325
"github.com/prometheus/client_golang/prometheus"
2426
)
2527

@@ -431,3 +433,32 @@ func TestCollectAndCount(t *testing.T) {
431433
t.Errorf("unexpected metric count, got %d, want %d", got, want)
432434
}
433435
}
436+
437+
func TestCollectAndFormat(t *testing.T) {
438+
const expected = `# HELP foo_bar A value that represents the number of bars in foo.
439+
# TYPE foo_bar counter
440+
foo_bar{fizz="bang"} 1
441+
`
442+
c := prometheus.NewCounterVec(
443+
prometheus.CounterOpts{
444+
Name: "foo_bar",
445+
Help: "A value that represents the number of bars in foo.",
446+
},
447+
[]string{"fizz"},
448+
)
449+
c.WithLabelValues("bang").Inc()
450+
451+
got, err := CollectAndFormat(c, expfmt.TypeTextPlain, "foo_bar")
452+
if err != nil {
453+
t.Errorf("unexpected error: %s", err.Error())
454+
}
455+
456+
gotS := string(got)
457+
if err != nil {
458+
t.Errorf("unexpected error: %s", err.Error())
459+
}
460+
461+
if gotS != expected {
462+
t.Errorf("unexpected metric output, got %q, expected %q", gotS, expected)
463+
}
464+
}

0 commit comments

Comments
 (0)