2018-08-23 00:59:40 +03:00
|
|
|
// 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
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2018-07-03 14:25:00 +03:00
|
|
|
|
2018-08-23 00:57:08 +03:00
|
|
|
package testutil
|
2018-07-03 14:25:00 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2018-08-23 01:21:38 +03:00
|
|
|
"io"
|
2018-07-03 14:25:00 +03:00
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"github.com/prometheus/common/expfmt"
|
2018-08-23 01:05:02 +03:00
|
|
|
|
|
|
|
dto "github.com/prometheus/client_model/go"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2018-09-03 00:36:34 +03:00
|
|
|
"github.com/prometheus/client_golang/prometheus/internal"
|
2018-07-03 14:25:00 +03:00
|
|
|
)
|
|
|
|
|
2018-08-23 01:21:38 +03:00
|
|
|
// 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 {
|
2018-07-03 14:25:00 +03:00
|
|
|
reg := prometheus.NewPedanticRegistry()
|
|
|
|
if err := reg.Register(c); err != nil {
|
|
|
|
return fmt.Errorf("registering collector failed: %s", err)
|
|
|
|
}
|
2018-08-23 01:21:38 +03:00
|
|
|
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()
|
2018-07-03 14:25:00 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("gathering metrics failed: %s", err)
|
|
|
|
}
|
|
|
|
if metricNames != nil {
|
|
|
|
metrics = filterMetrics(metrics, metricNames)
|
|
|
|
}
|
|
|
|
var tp expfmt.TextParser
|
2018-08-23 01:21:38 +03:00
|
|
|
expectedMetrics, err := tp.TextToMetricFamilies(expected)
|
2018-07-03 14:25:00 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing expected metrics failed: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-09-03 00:36:34 +03:00
|
|
|
if !reflect.DeepEqual(metrics, internal.NormalizeMetricFamilies(expectedMetrics)) {
|
2018-08-23 01:21:38 +03:00
|
|
|
// Encode the gathered output to the readable text format for comparison.
|
2018-07-03 14:25:00 +03:00
|
|
|
var buf1 bytes.Buffer
|
|
|
|
enc := expfmt.NewEncoder(&buf1, expfmt.FmtText)
|
|
|
|
for _, mf := range metrics {
|
|
|
|
if err := enc.Encode(mf); err != nil {
|
|
|
|
return fmt.Errorf("encoding result failed: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Encode normalized expected metrics again to generate them in the same ordering
|
|
|
|
// the registry does to spot differences more easily.
|
|
|
|
var buf2 bytes.Buffer
|
|
|
|
enc = expfmt.NewEncoder(&buf2, expfmt.FmtText)
|
2018-09-03 00:36:34 +03:00
|
|
|
for _, mf := range internal.NormalizeMetricFamilies(expectedMetrics) {
|
2018-07-03 14:25:00 +03:00
|
|
|
if err := enc.Encode(mf); err != nil {
|
|
|
|
return fmt.Errorf("encoding result failed: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf(`
|
|
|
|
metric output does not match expectation; want:
|
|
|
|
|
|
|
|
%s
|
|
|
|
|
|
|
|
got:
|
|
|
|
|
|
|
|
%s
|
|
|
|
`, buf2.String(), buf1.String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func filterMetrics(metrics []*dto.MetricFamily, names []string) []*dto.MetricFamily {
|
|
|
|
var filtered []*dto.MetricFamily
|
|
|
|
for _, m := range metrics {
|
|
|
|
for _, name := range names {
|
|
|
|
if m.GetName() == name {
|
2018-08-23 01:21:38 +03:00
|
|
|
filtered = append(filtered, m)
|
2018-07-03 14:25:00 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filtered
|
|
|
|
}
|