commit
220bcf5e32
|
@ -138,20 +138,13 @@ func GatherAndCompare(g prometheus.Gatherer, expected io.Reader, metricNames ...
|
||||||
}
|
}
|
||||||
want := internal.NormalizeMetricFamilies(wantRaw)
|
want := internal.NormalizeMetricFamilies(wantRaw)
|
||||||
|
|
||||||
if len(got) != len(want) {
|
return compare(got, want)
|
||||||
return notMatchingError(got, want)
|
|
||||||
}
|
|
||||||
for i := range got {
|
|
||||||
if got[i].String() != want[i].String() {
|
|
||||||
return notMatchingError(got, want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// notMatchingError encodes both provided slices of metric families into the
|
// compare encodes both provided slices of metric families into the text format,
|
||||||
// text format and creates a readable error message from the result.
|
// compares their string message, and returns an error if they do not match.
|
||||||
func notMatchingError(got, want []*dto.MetricFamily) error {
|
// The error contains the encoded text, of both the desired, and actual result.
|
||||||
|
func compare(got, want []*dto.MetricFamily) error {
|
||||||
var gotBuf, wantBuf bytes.Buffer
|
var gotBuf, wantBuf bytes.Buffer
|
||||||
enc := expfmt.NewEncoder(&gotBuf, expfmt.FmtText)
|
enc := expfmt.NewEncoder(&gotBuf, expfmt.FmtText)
|
||||||
for _, mf := range got {
|
for _, mf := range got {
|
||||||
|
@ -166,6 +159,7 @@ func notMatchingError(got, want []*dto.MetricFamily) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if wantBuf.String() != gotBuf.String() {
|
||||||
return fmt.Errorf(`
|
return fmt.Errorf(`
|
||||||
metric output does not match expectation; want:
|
metric output does not match expectation; want:
|
||||||
|
|
||||||
|
@ -175,6 +169,9 @@ got:
|
||||||
|
|
||||||
%s
|
%s
|
||||||
`, wantBuf.String(), gotBuf.String())
|
`, wantBuf.String(), gotBuf.String())
|
||||||
|
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func filterMetrics(metrics []*dto.MetricFamily, names []string) []*dto.MetricFamily {
|
func filterMetrics(metrics []*dto.MetricFamily, names []string) []*dto.MetricFamily {
|
||||||
|
|
|
@ -165,6 +165,82 @@ func TestCollectAndCompareNoLabel(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCollectAndCompareHistogram(t *testing.T) {
|
||||||
|
inputs := []struct {
|
||||||
|
name string
|
||||||
|
c prometheus.Collector
|
||||||
|
metadata string
|
||||||
|
expect string
|
||||||
|
labels []string
|
||||||
|
observation float64
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Testing Histogram Collector",
|
||||||
|
c: prometheus.NewHistogram(prometheus.HistogramOpts{
|
||||||
|
Name: "some_histogram",
|
||||||
|
Help: "An example of a histogram",
|
||||||
|
Buckets: []float64{1, 2, 3},
|
||||||
|
}),
|
||||||
|
metadata: `
|
||||||
|
# HELP some_histogram An example of a histogram
|
||||||
|
# TYPE some_histogram histogram
|
||||||
|
`,
|
||||||
|
expect: `
|
||||||
|
some_histogram{le="1"} 0
|
||||||
|
some_histogram{le="2"} 0
|
||||||
|
some_histogram{le="3"} 1
|
||||||
|
some_histogram_bucket{le="+Inf"} 1
|
||||||
|
some_histogram_sum 2.5
|
||||||
|
some_histogram_count 1
|
||||||
|
|
||||||
|
`,
|
||||||
|
observation: 2.5,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Testing HistogramVec Collector",
|
||||||
|
c: prometheus.NewHistogramVec(prometheus.HistogramOpts{
|
||||||
|
Name: "some_histogram",
|
||||||
|
Help: "An example of a histogram",
|
||||||
|
Buckets: []float64{1, 2, 3},
|
||||||
|
}, []string{"test"}),
|
||||||
|
|
||||||
|
metadata: `
|
||||||
|
# HELP some_histogram An example of a histogram
|
||||||
|
# TYPE some_histogram histogram
|
||||||
|
`,
|
||||||
|
expect: `
|
||||||
|
some_histogram_bucket{test="test",le="1"} 0
|
||||||
|
some_histogram_bucket{test="test",le="2"} 0
|
||||||
|
some_histogram_bucket{test="test",le="3"} 1
|
||||||
|
some_histogram_bucket{test="test",le="+Inf"} 1
|
||||||
|
some_histogram_sum{test="test"} 2.5
|
||||||
|
some_histogram_count{test="test"} 1
|
||||||
|
|
||||||
|
`,
|
||||||
|
observation: 2.5,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, input := range inputs {
|
||||||
|
switch collector := input.c.(type) {
|
||||||
|
case prometheus.Histogram:
|
||||||
|
collector.Observe(input.observation)
|
||||||
|
case *prometheus.HistogramVec:
|
||||||
|
collector.WithLabelValues("test").Observe(input.observation)
|
||||||
|
default:
|
||||||
|
t.Fatalf("unsuported collector tested")
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run(input.name, func(t *testing.T) {
|
||||||
|
if err := CollectAndCompare(input.c, strings.NewReader(input.metadata+input.expect)); err != nil {
|
||||||
|
t.Errorf("unexpected collecting result:\n%s", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestNoMetricFilter(t *testing.T) {
|
func TestNoMetricFilter(t *testing.T) {
|
||||||
const metadata = `
|
const metadata = `
|
||||||
# HELP some_total A value that represents a counter.
|
# HELP some_total A value that represents a counter.
|
||||||
|
|
Loading…
Reference in New Issue