metrictest

package module
v0.0.0-...-9ea7eb9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 6, 2019 License: Apache-2.0 Imports: 7 Imported by: 0

README

metrictest

go-doc

A small metric testing library for Prometheus Go client.

Counter/Gauge

The current value of Counters and Gauges can be asserted.

counter := prometheus.NewCounter(prometheus.CounterOpts{...})
counter.Add(10)

metrictest.AssertCounter(t, 10, counter)

CounterVec/GaugeVec

The current value of a single Counter in a CounterVec can be asserted. Asserting a value of 0 is the same as asserting that the Counter is not contained in the CounterVec. The interface for GaugeVec is identical to that of CounterVec.

counter := prometheus.NewCounterVec(prometheus.CounterOpts{...}, []string{"hero", "villain"})
counter.WithLabelValues("batman", "joker").Add(10)

metrictest.AssertCounterVec(t, 10, counter, "batman", "joker")

Histogram/Summary

Asserts for Histogram and Summary work a little differently from Gauge and Counter. Instead of asserting the value of each bucket/percentile, we assert the count and sum of all samples observed by the Histogram or Summary. As before, asserting a value of 0 is the same as asserting that no samples have been observed.

histogram := prometheus.NewHistogram(prometheus.HistogramOpts{...})
histogram.Observe(10)
histogram.Observe(20)

metrictest.AssertHistogramSamples(t, 2, 30, histogram)

HistogramVec/SummaryVec

As with Histogram, we can assert the sample count and sum of a single Histogram in the HistogramVec. Asserting a value of 0 for count is the same as asserting that the Histogram is not contained in the HistogramVec. The interface for SummaryVec is identical to that of HistogramVec.

histogramVec := prometheus.NewHistogramVec(prometheus.HistogramOpts{...}, []string{"hero", "villain"})
histogramVec.WithLabelValues("a-label", "another-label").Observe(10)
histogramVec.WithLabelValues("a-label", "another-label").Observe(20)

metrictest.AssertHistogramVecSamples(t, 2, 30, histogram, "a-label", "another-label")

Documentation

Overview

Package metrictest is a small metric testing library for the Prometheus Go client https://github.com/prometheus/client_golang. See documentation for each Assert function for usage details.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertCounter

func AssertCounter(t TestingT, expected float64, counter prometheus.Counter)

AssertCounter asserts the value of a prometheus.Counter

counter.Add(10)
metrictest.AssertCounter(t, 10, counter)

func AssertCounterVec

func AssertCounterVec(t TestingT, expected float64, counterVec *prometheus.CounterVec, labels ...string)

AssertCounterVec asserts the value of a single counter in a prometheus.CounterVec

counterVec.WithLabelValues("a-label", "another-label").Inc(10)
metrictest.AssertCounterVec(t, 10, counterVec, "a-label", "another-label")

Asserting that the value of a counter is 0 is equivalent to asserting that the counter does not exist.

func AssertGauge

func AssertGauge(t TestingT, expected float64, gauge prometheus.Gauge)

AssertGauge asserts the value of a prometheus.Gauge

gauge.Set(10)
metrictest.AssertGauge(t, 10, gauge)

func AssertGaugeVec

func AssertGaugeVec(t TestingT, expected float64, gaugeVec *prometheus.GaugeVec, labels ...string)

AssertGaugeVec asserts the value of a single gauge in a prometheus.GaugeVec

gaugeVec.WithLabelValues("a-label", "another-label").Set(10)
metrictest.AssertGaugeVec(t, 10, gaugeVec, "a-label", "another-label")

Asserting that the value of a gauge is 0 is equivalent to asserting that the gauge does not exist.

func AssertHistogramSamples

func AssertHistogramSamples(t TestingT, count int, sum float64, histogram prometheus.Histogram)

AssertHistogramSamples asserts the count and sum of all samples captured by the prometheus.Histogram

histogram.Observe(10)
histogram.Observe(20)
metrictest.AssertHistogramSamples(t, 2, 30, histogram)

func AssertHistogramVecSamples

func AssertHistogramVecSamples(t TestingT, count int, sum float64, histogramVec *prometheus.HistogramVec, labels ...string)

AssertHistogramVecSamples asserts the count and sum of all samples captured by a single histogram contained by the prometheus.HistogramVec

histogramVec.WithLabelValues("a-label", "another-label").Observe(10)
histogramVec.WithLabelValues("a-label", "another-label").Observe(20)
metrictest.AssertHistogramSamples(t, 2, 30, histogram, "a-label", "another-label")

func AssertSummarySamples

func AssertSummarySamples(t TestingT, count int, sum float64, summary prometheus.Histogram)

AssertSummarySamples asserts the count and sum of all samples captured by the prometheus.Summary

summary.Observe(10)
summary.Observe(20)
metrictest.AssertSummarySamples(t, 2, 30, summary)

func AssertSummaryVecSamples

func AssertSummaryVecSamples(t TestingT, count int, sum float64, summaryVec *prometheus.SummaryVec, labels ...string)

AssertSummaryVecSamples asserts the count and sum of all samples captured by a single summary contained by the prometheus.SummaryVec

summaryVec.WithLabelValues("a-label", "another-label").Observe(10)
summaryVec.WithLabelValues("a-label", "another-label").Observe(20)
metrictest.AssertSummarySamples(t, 2, 30, summary, "a-label", "another-label")

func ToExpVar

func ToExpVar(c prometheus.Collector) (string, error)

ToExpVar collects metrics from the collector and prints them in ExpVar format.

Types

type TestingT

type TestingT interface {
	Errorf(format string, args ...interface{})
}

TestingT is an interface wrapper around *testing.T

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL