Skip to content

Commit

Permalink
Improved interface
Browse files Browse the repository at this point in the history
- Expected text format is now read from an io.Reader.
- Metrics are gathered from a Gatherer.
- Added a convenience wrapper to collect from a Collector.

Signed-off-by: beorn7 <beorn@soundcloud.com>
  • Loading branch information
beorn7 committed Sep 2, 2018
1 parent 154f28a commit 5ba0993
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
29 changes: 17 additions & 12 deletions prometheus/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package testutil
import (
"bytes"
"fmt"
"io"
"reflect"
"sort"

Expand All @@ -26,29 +27,37 @@ import (
"github.com/prometheus/client_golang/prometheus"
)

// GatherAndCompare retrieves all metrics exposed by a collector and compares it
// to an expected output in the Prometheus text exposition format.
// metricNames allows only comparing the given metrics. All are compared if it's nil.
func GatherAndCompare(c prometheus.Collector, expected string, metricNames ...string) error {
// CollectAndCompare registers the provided Collector with a newly created
// pedantic Registry. It then does the same as GatherAndCompare, gathering the
// metrics from the pedantic Registry.
func CollectAndCompare(c prometheus.Collector, expected io.Reader, metricNames ...string) error {
reg := prometheus.NewPedanticRegistry()
if err := reg.Register(c); err != nil {
return fmt.Errorf("registering collector failed: %s", err)
}
metrics, err := reg.Gather()
return GatherAndCompare(reg, expected, metricNames...)
}

// GatherAndCompare gathers all metrics from the provided Gatherer and compares
// it to an expected output read from the provided Reader in the Prometheus text
// exposition format. If any metricNames are provided, only metrics with those
// names are compared.
func GatherAndCompare(g prometheus.Gatherer, expected io.Reader, metricNames ...string) error {
metrics, err := g.Gather()
if err != nil {
return fmt.Errorf("gathering metrics failed: %s", err)
}
if metricNames != nil {
metrics = filterMetrics(metrics, metricNames)
}
var tp expfmt.TextParser
expectedMetrics, err := tp.TextToMetricFamilies(bytes.NewReader([]byte(expected)))
expectedMetrics, err := tp.TextToMetricFamilies(expected)
if err != nil {
return fmt.Errorf("parsing expected metrics failed: %s", err)
}

if !reflect.DeepEqual(metrics, normalizeMetricFamilies(expectedMetrics)) {
// Encode the gathered output to the readbale text format for comparison.
// Encode the gathered output to the readable text format for comparison.
var buf1 bytes.Buffer
enc := expfmt.NewEncoder(&buf1, expfmt.FmtText)
for _, mf := range metrics {
Expand Down Expand Up @@ -82,16 +91,12 @@ got:
func filterMetrics(metrics []*dto.MetricFamily, names []string) []*dto.MetricFamily {
var filtered []*dto.MetricFamily
for _, m := range metrics {
drop := true
for _, name := range names {
if m.GetName() == name {
drop = false
filtered = append(filtered, m)
break
}
}
if !drop {
filtered = append(filtered, m)
}
}
return filtered
}
Expand Down
11 changes: 6 additions & 5 deletions prometheus/testutil/testutil_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018 he Prometheus Authors
// Copyright 2018 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand All @@ -14,12 +14,13 @@
package testutil

import (
"strings"
"testing"

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

func TestGatherAndCompare(t *testing.T) {
func TestCollectAndCompare(t *testing.T) {
const metadata = `
# HELP some_total A value that represents a counter.
# TYPE some_total counter
Expand All @@ -39,7 +40,7 @@ func TestGatherAndCompare(t *testing.T) {
some_total{ label1 = "value1" } 1
`

if err := GatherAndCompare(c, metadata+expected, "some_total"); err != nil {
if err := CollectAndCompare(c, strings.NewReader(metadata+expected), "some_total"); err != nil {
t.Errorf("unexpected collecting result:\n%s", err)
}
}
Expand All @@ -63,7 +64,7 @@ func TestNoMetricFilter(t *testing.T) {
some_total{label1="value1"} 1
`

if err := GatherAndCompare(c, metadata+expected); err != nil {
if err := CollectAndCompare(c, strings.NewReader(metadata+expected)); err != nil {
t.Errorf("unexpected collecting result:\n%s", err)
}
}
Expand Down Expand Up @@ -103,7 +104,7 @@ some_total{label1="value1"} 1
`

err := GatherAndCompare(c, metadata+expected)
err := CollectAndCompare(c, strings.NewReader(metadata+expected))
if err == nil {
t.Error("Expected error, got no error.")
}
Expand Down

0 comments on commit 5ba0993

Please sign in to comment.