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-09-04 13:59:32 +03:00
|
|
|
|
// Package testutil provides helpers to test code using the prometheus package
|
|
|
|
|
// of client_golang.
|
|
|
|
|
//
|
|
|
|
|
// While writing unit tests to verify correct instrumentation of your code, it's
|
|
|
|
|
// a common mistake to mostly test the instrumentation library instead of your
|
|
|
|
|
// own code. Rather than verifying that a prometheus.Counter's value has changed
|
|
|
|
|
// as expected or that it shows up in the exposition after registration, it is
|
|
|
|
|
// in general more robust and more faithful to the concept of unit tests to use
|
|
|
|
|
// mock implementations of the prometheus.Counter and prometheus.Registerer
|
|
|
|
|
// interfaces that simply assert that the Add or Register methods have been
|
|
|
|
|
// called with the expected arguments. However, this might be overkill in simple
|
|
|
|
|
// scenarios. The ToFloat64 function is provided for simple inspection of a
|
|
|
|
|
// single-value metric, but it has to be used with caution.
|
|
|
|
|
//
|
|
|
|
|
// End-to-end tests to verify all or larger parts of the metrics exposition can
|
|
|
|
|
// be implemented with the CollectAndCompare or GatherAndCompare functions. The
|
|
|
|
|
// most appropriate use is not so much testing instrumentation of your code, but
|
|
|
|
|
// testing custom prometheus.Collector implementations and in particular whole
|
|
|
|
|
// exporters, i.e. programs that retrieve telemetry data from a 3rd party source
|
|
|
|
|
// and convert it into Prometheus metrics.
|
2020-04-25 00:44:21 +03:00
|
|
|
|
//
|
|
|
|
|
// In a similar pattern, CollectAndLint and GatherAndLint can be used to detect
|
|
|
|
|
// metrics that have issues with their name, type, or metadata without being
|
|
|
|
|
// necessarily invalid, e.g. a counter with a name missing the “_total” suffix.
|
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"
|
2022-08-05 17:27:47 +03:00
|
|
|
|
"net/http"
|
2018-07-03 14:25:00 +03:00
|
|
|
|
|
2024-06-19 09:22:50 +03:00
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2018-08-23 01:05:02 +03:00
|
|
|
|
dto "github.com/prometheus/client_model/go"
|
2022-08-05 17:27:47 +03:00
|
|
|
|
"github.com/prometheus/common/expfmt"
|
2023-11-20 12:57:30 +03:00
|
|
|
|
"google.golang.org/protobuf/proto"
|
2018-08-23 01:05:02 +03:00
|
|
|
|
|
|
|
|
|
"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-09-04 13:38:29 +03:00
|
|
|
|
// ToFloat64 collects all Metrics from the provided Collector. It expects that
|
|
|
|
|
// this results in exactly one Metric being collected, which must be a Gauge,
|
|
|
|
|
// Counter, or Untyped. In all other cases, ToFloat64 panics. ToFloat64 returns
|
|
|
|
|
// the value of the collected Metric.
|
|
|
|
|
//
|
|
|
|
|
// The Collector provided is typically a simple instance of Gauge or Counter, or
|
|
|
|
|
// – less commonly – a GaugeVec or CounterVec with exactly one element. But any
|
|
|
|
|
// Collector fulfilling the prerequisites described above will do.
|
|
|
|
|
//
|
|
|
|
|
// Use this function with caution. It is computationally very expensive and thus
|
|
|
|
|
// not suited at all to read values from Metrics in regular code. This is really
|
|
|
|
|
// only for testing purposes, and even for testing, other approaches are often
|
|
|
|
|
// more appropriate (see this package's documentation).
|
|
|
|
|
//
|
|
|
|
|
// A clear anti-pattern would be to use a metric type from the prometheus
|
|
|
|
|
// package to track values that are also needed for something else than the
|
|
|
|
|
// exposition of Prometheus metrics. For example, you would like to track the
|
|
|
|
|
// number of items in a queue because your code should reject queuing further
|
|
|
|
|
// items if a certain limit is reached. It is tempting to track the number of
|
|
|
|
|
// items in a prometheus.Gauge, as it is then easily available as a metric for
|
|
|
|
|
// exposition, too. However, then you would need to call ToFloat64 in your
|
|
|
|
|
// regular code, potentially quite often. The recommended way is to track the
|
|
|
|
|
// number of items conventionally (in the way you would have done it without
|
|
|
|
|
// considering Prometheus metrics) and then expose the number with a
|
|
|
|
|
// prometheus.GaugeFunc.
|
|
|
|
|
func ToFloat64(c prometheus.Collector) float64 {
|
|
|
|
|
var (
|
|
|
|
|
m prometheus.Metric
|
|
|
|
|
mCount int
|
|
|
|
|
mChan = make(chan prometheus.Metric)
|
|
|
|
|
done = make(chan struct{})
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
for m = range mChan {
|
|
|
|
|
mCount++
|
|
|
|
|
}
|
|
|
|
|
close(done)
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
c.Collect(mChan)
|
|
|
|
|
close(mChan)
|
|
|
|
|
<-done
|
|
|
|
|
|
|
|
|
|
if mCount != 1 {
|
|
|
|
|
panic(fmt.Errorf("collected %d metrics instead of exactly 1", mCount))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pb := &dto.Metric{}
|
2022-06-17 10:04:06 +03:00
|
|
|
|
if err := m.Write(pb); err != nil {
|
|
|
|
|
panic(fmt.Errorf("error happened while collecting metrics: %w", err))
|
|
|
|
|
}
|
2018-09-04 13:38:29 +03:00
|
|
|
|
if pb.Gauge != nil {
|
|
|
|
|
return pb.Gauge.GetValue()
|
|
|
|
|
}
|
|
|
|
|
if pb.Counter != nil {
|
|
|
|
|
return pb.Counter.GetValue()
|
|
|
|
|
}
|
|
|
|
|
if pb.Untyped != nil {
|
|
|
|
|
return pb.Untyped.GetValue()
|
|
|
|
|
}
|
|
|
|
|
panic(fmt.Errorf("collected a non-gauge/counter/untyped metric: %s", pb))
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-14 01:17:45 +03:00
|
|
|
|
// CollectAndCount registers the provided Collector with a newly created
|
|
|
|
|
// pedantic Registry. It then calls GatherAndCount with that Registry and with
|
|
|
|
|
// the provided metricNames. In the unlikely case that the registration or the
|
|
|
|
|
// gathering fails, this function panics. (This is inconsistent with the other
|
|
|
|
|
// CollectAnd… functions in this package and has historical reasons. Changing
|
|
|
|
|
// the function signature would be a breaking change and will therefore only
|
|
|
|
|
// happen with the next major version bump.)
|
|
|
|
|
func CollectAndCount(c prometheus.Collector, metricNames ...string) int {
|
|
|
|
|
reg := prometheus.NewPedanticRegistry()
|
|
|
|
|
if err := reg.Register(c); err != nil {
|
2022-08-03 07:30:51 +03:00
|
|
|
|
panic(fmt.Errorf("registering collector failed: %w", err))
|
2020-05-14 01:17:45 +03:00
|
|
|
|
}
|
|
|
|
|
result, err := GatherAndCount(reg, metricNames...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
2020-01-09 13:07:07 +03:00
|
|
|
|
|
2020-05-14 01:17:45 +03:00
|
|
|
|
// GatherAndCount gathers all metrics from the provided Gatherer and counts
|
|
|
|
|
// them. It returns the number of metric children in all gathered metric
|
|
|
|
|
// families together. If any metricNames are provided, only metrics with those
|
|
|
|
|
// names are counted.
|
|
|
|
|
func GatherAndCount(g prometheus.Gatherer, metricNames ...string) (int, error) {
|
|
|
|
|
got, err := g.Gather()
|
|
|
|
|
if err != nil {
|
2022-08-03 07:30:51 +03:00
|
|
|
|
return 0, fmt.Errorf("gathering metrics failed: %w", err)
|
2020-05-14 01:17:45 +03:00
|
|
|
|
}
|
|
|
|
|
if metricNames != nil {
|
|
|
|
|
got = filterMetrics(got, metricNames)
|
|
|
|
|
}
|
2020-01-09 13:07:07 +03:00
|
|
|
|
|
2020-05-14 01:17:45 +03:00
|
|
|
|
result := 0
|
|
|
|
|
for _, mf := range got {
|
|
|
|
|
result += len(mf.GetMetric())
|
|
|
|
|
}
|
|
|
|
|
return result, nil
|
2020-01-09 13:07:07 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 17:27:47 +03:00
|
|
|
|
// ScrapeAndCompare calls a remote exporter's endpoint which is expected to return some metrics in
|
|
|
|
|
// plain text format. Then it compares it with the results that the `expected` would return.
|
|
|
|
|
// If the `metricNames` is not empty it would filter the comparison only to the given metric names.
|
|
|
|
|
func ScrapeAndCompare(url string, expected io.Reader, metricNames ...string) error {
|
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("scraping metrics failed: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
|
return fmt.Errorf("the scraping target returned a status code other than 200: %d",
|
|
|
|
|
resp.StatusCode)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scraped, err := convertReaderToMetricFamily(resp.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wanted, err := convertReaderToMetricFamily(expected)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return compareMetricFamilies(scraped, wanted, metricNames...)
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-09 18:08:21 +03:00
|
|
|
|
// CollectAndCompare collects the metrics identified by `metricNames` and compares them in the Prometheus text
|
|
|
|
|
// exposition format to the data read from expected.
|
2018-08-23 01:21:38 +03:00
|
|
|
|
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 {
|
2022-08-03 07:30:51 +03:00
|
|
|
|
return fmt.Errorf("registering collector failed: %w", err)
|
2018-07-03 14:25:00 +03:00
|
|
|
|
}
|
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 {
|
2022-02-23 14:22:52 +03:00
|
|
|
|
return TransactionalGatherAndCompare(prometheus.ToTransactionalGatherer(g), expected, metricNames...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TransactionalGatherAndCompare 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 TransactionalGatherAndCompare(g prometheus.TransactionalGatherer, expected io.Reader, metricNames ...string) error {
|
|
|
|
|
got, done, err := g.Gather()
|
|
|
|
|
defer done()
|
2018-07-03 14:25:00 +03:00
|
|
|
|
if err != nil {
|
2022-08-03 07:30:51 +03:00
|
|
|
|
return fmt.Errorf("gathering metrics failed: %w", err)
|
2018-07-03 14:25:00 +03:00
|
|
|
|
}
|
2022-08-05 17:27:47 +03:00
|
|
|
|
|
|
|
|
|
wanted, err := convertReaderToMetricFamily(expected)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2018-07-03 14:25:00 +03:00
|
|
|
|
}
|
2022-08-05 17:27:47 +03:00
|
|
|
|
|
|
|
|
|
return compareMetricFamilies(got, wanted, metricNames...)
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-09 18:08:21 +03:00
|
|
|
|
// 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
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 17:27:47 +03:00
|
|
|
|
// 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) {
|
2018-07-03 14:25:00 +03:00
|
|
|
|
var tp expfmt.TextParser
|
2022-08-05 17:27:47 +03:00
|
|
|
|
notNormalized, err := tp.TextToMetricFamilies(reader)
|
2018-07-03 14:25:00 +03:00
|
|
|
|
if err != nil {
|
2022-08-05 17:27:47 +03:00
|
|
|
|
return nil, fmt.Errorf("converting reader to metric families failed: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-20 12:57:30 +03:00
|
|
|
|
// The text protocol handles empty help fields inconsistently. When
|
|
|
|
|
// encoding, any non-nil value, include the empty string, produces a
|
|
|
|
|
// "# HELP" line. But when decoding, the help field is only set to a
|
|
|
|
|
// non-nil value if the "# HELP" line contains a non-empty value.
|
|
|
|
|
//
|
|
|
|
|
// Because metrics in a registry always have non-nil help fields, populate
|
|
|
|
|
// any nil help fields in the parsed metrics with the empty string so that
|
|
|
|
|
// when we compare text encodings, the results are consistent.
|
|
|
|
|
for _, metric := range notNormalized {
|
|
|
|
|
if metric.Help == nil {
|
|
|
|
|
metric.Help = proto.String("")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 17:27:47 +03:00
|
|
|
|
return internal.NormalizeMetricFamilies(notNormalized), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// compareMetricFamilies would compare 2 slices of metric families, and optionally filters both of
|
|
|
|
|
// them to the `metricNames` provided.
|
|
|
|
|
func compareMetricFamilies(got, expected []*dto.MetricFamily, metricNames ...string) error {
|
|
|
|
|
if metricNames != nil {
|
|
|
|
|
got = filterMetrics(got, metricNames)
|
2023-03-22 13:45:45 +03:00
|
|
|
|
expected = filterMetrics(expected, metricNames)
|
2024-05-15 12:50:24 +03:00
|
|
|
|
if len(metricNames) > len(got) {
|
|
|
|
|
var missingMetricNames []string
|
|
|
|
|
for _, name := range metricNames {
|
|
|
|
|
if ok := hasMetricByName(got, name); !ok {
|
|
|
|
|
missingMetricNames = append(missingMetricNames, name)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return fmt.Errorf("expected metric name(s) not found: %v", missingMetricNames)
|
|
|
|
|
}
|
2018-07-03 14:25:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 17:27:47 +03:00
|
|
|
|
return compare(got, expected)
|
2018-11-13 18:16:43 +03:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-14 14:55:09 +03:00
|
|
|
|
// compare encodes both provided slices of metric families into the text format,
|
|
|
|
|
// compares their string message, and returns an error if they do not match.
|
2018-11-15 20:40:33 +03:00
|
|
|
|
// The error contains the encoded text of both the desired and the actual
|
|
|
|
|
// result.
|
2018-11-14 14:55:09 +03:00
|
|
|
|
func compare(got, want []*dto.MetricFamily) error {
|
2018-11-13 18:16:43 +03:00
|
|
|
|
var gotBuf, wantBuf bytes.Buffer
|
2024-02-23 20:16:02 +03:00
|
|
|
|
enc := expfmt.NewEncoder(&gotBuf, expfmt.NewFormat(expfmt.TypeTextPlain))
|
2018-11-13 18:16:43 +03:00
|
|
|
|
for _, mf := range got {
|
|
|
|
|
if err := enc.Encode(mf); err != nil {
|
2022-08-03 07:30:51 +03:00
|
|
|
|
return fmt.Errorf("encoding gathered metrics failed: %w", err)
|
2018-11-13 18:16:43 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-23 20:16:02 +03:00
|
|
|
|
enc = expfmt.NewEncoder(&wantBuf, expfmt.NewFormat(expfmt.TypeTextPlain))
|
2018-11-13 18:16:43 +03:00
|
|
|
|
for _, mf := range want {
|
|
|
|
|
if err := enc.Encode(mf); err != nil {
|
2022-08-03 07:30:51 +03:00
|
|
|
|
return fmt.Errorf("encoding expected metrics failed: %w", err)
|
2018-07-03 14:25:00 +03:00
|
|
|
|
}
|
2018-11-13 18:16:43 +03:00
|
|
|
|
}
|
2024-06-19 09:22:50 +03:00
|
|
|
|
if diffErr := cmp.Diff(gotBuf.String(), wantBuf.String()); diffErr != "" {
|
2022-04-13 20:38:05 +03:00
|
|
|
|
return fmt.Errorf(diffErr)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2018-07-03 14:25:00 +03:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
2024-05-15 12:50:24 +03:00
|
|
|
|
|
|
|
|
|
func hasMetricByName(metrics []*dto.MetricFamily, name string) bool {
|
|
|
|
|
for _, mf := range metrics {
|
|
|
|
|
if mf.GetName() == name {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|