testutil
packageThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
Links
Documentation¶
Overview¶
Package testutil provides helpers to test code using the prometheus packageof client_golang.
While writing unit tests to verify correct instrumentation of your code, it'sa common mistake to mostly test the instrumentation library instead of yourown code. Rather than verifying that a prometheus.Counter's value has changedas expected or that it shows up in the exposition after registration, it isin general more robust and more faithful to the concept of unit tests to usemock implementations of the prometheus.Counter and prometheus.Registererinterfaces that simply assert that the Add or Register methods have beencalled with the expected arguments. However, this might be overkill in simplescenarios. The ToFloat64 function is provided for simple inspection of asingle-value metric, but it has to be used with caution.
End-to-end tests to verify all or larger parts of the metrics exposition canbe implemented with the CollectAndCompare or GatherAndCompare functions. Themost appropriate use is not so much testing instrumentation of your code, buttesting custom prometheus.Collector implementations and in particular wholeexporters, i.e. programs that retrieve telemetry data from a 3rd party sourceand convert it into Prometheus metrics.
In a similar pattern, CollectAndLint and GatherAndLint can be used to detectmetrics that have issues with their name, type, or metadata without beingnecessarily invalid, e.g. a counter with a name missing the “_total” suffix.
Index¶
- func CollectAndCompare(c prometheus.Collector, expected io.Reader, metricNames ...string) error
- func CollectAndCount(c prometheus.Collector, metricNames ...string) int
- func CollectAndFormat(c prometheus.Collector, format expfmt.FormatType, metricNames ...string) ([]byte, error)
- func CollectAndLint(c prometheus.Collector, metricNames ...string) ([]promlint.Problem, error)
- func GatherAndCompare(g prometheus.Gatherer, expected io.Reader, metricNames ...string) error
- func GatherAndCount(g prometheus.Gatherer, metricNames ...string) (int, error)
- func GatherAndLint(g prometheus.Gatherer, metricNames ...string) ([]promlint.Problem, error)
- func ScrapeAndCompare(url string, expected io.Reader, metricNames ...string) error
- func ToFloat64(c prometheus.Collector) float64
- func TransactionalGatherAndCompare(g prometheus.TransactionalGatherer, expected io.Reader, metricNames ...string) error
Constants¶
This section is empty.
Variables¶
This section is empty.
Functions¶
funcCollectAndCompare¶
CollectAndCompare collects the metrics identified by `metricNames` and compares them in the Prometheus textexposition format to the data read from expected.
NOTE: Be mindful of accidental discrepancies between expected and metricNames; metricNames filterboth expected and collected metrics. Seehttps://github.com/prometheus/client_golang/issues/1351.
funcCollectAndCount¶added inv0.12.1
func CollectAndCount(cprometheus.Collector, metricNames ...string)int
CollectAndCount registers the provided Collector with a newly createdpedantic Registry. It then calls GatherAndCount with that Registry and withthe provided metricNames. In the unlikely case that the registration or thegathering fails, this function panics. (This is inconsistent with the otherCollectAnd… functions in this package and has historical reasons. Changingthe function signature would be a breaking change and will therefore onlyhappen with the next major version bump.)
funcCollectAndFormat¶added inv1.20.0
func CollectAndFormat(cprometheus.Collector, formatexpfmt.FormatType, metricNames ...string) ([]byte,error)
CollectAndFormat collects the metrics identified by `metricNames` and returns them in the given format.
funcCollectAndLint¶added inv0.12.1
CollectAndLint registers the provided Collector with a newly created pedanticRegistry. It then calls GatherAndLint with that Registry and with theprovided metricNames.
funcGatherAndCompare¶
GatherAndCompare gathers all metrics from the provided Gatherer and comparesit to an expected output read from the provided Reader in the Prometheus textexposition format. If any metricNames are provided, only metrics with thosenames are compared.
NOTE: Be mindful of accidental discrepancies between expected and metricNames; metricNames filterboth expected and gathered metrics. Seehttps://github.com/prometheus/client_golang/issues/1351.
funcGatherAndCount¶added inv0.12.1
func GatherAndCount(gprometheus.Gatherer, metricNames ...string) (int,error)
GatherAndCount gathers all metrics from the provided Gatherer and countsthem. It returns the number of metric children in all gathered metricfamilies together. If any metricNames are provided, only metrics with thosenames are counted.
funcGatherAndLint¶added inv0.12.1
GatherAndLint gathers all metrics from the provided Gatherer and checks themwith the linter in the promlint package. If any metricNames are provided,only metrics with those names are checked.
funcScrapeAndCompare¶added inv1.13.0
ScrapeAndCompare calls a remote exporter's endpoint which is expected to return some metrics inplain 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.
NOTE: Be mindful of accidental discrepancies between expected and metricNames; metricNames filterboth expected and scraped metrics. Seehttps://github.com/prometheus/client_golang/issues/1351.
funcToFloat64¶
func ToFloat64(cprometheus.Collector)float64
ToFloat64 collects all Metrics from the provided Collector. It expects thatthis results in exactly one Metric being collected, which must be a Gauge,Counter, or Untyped. In all other cases, ToFloat64 panics. ToFloat64 returnsthe 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 anyCollector fulfilling the prerequisites described above will do.
Use this function with caution. It is computationally very expensive and thusnot suited at all to read values from Metrics in regular code. This is reallyonly for testing purposes, and even for testing, other approaches are oftenmore appropriate (see this package's documentation).
A clear anti-pattern would be to use a metric type from the prometheuspackage to track values that are also needed for something else than theexposition of Prometheus metrics. For example, you would like to track thenumber of items in a queue because your code should reject queuing furtheritems if a certain limit is reached. It is tempting to track the number ofitems in a prometheus.Gauge, as it is then easily available as a metric forexposition, too. However, then you would need to call ToFloat64 in yourregular code, potentially quite often. The recommended way is to track thenumber of items conventionally (in the way you would have done it withoutconsidering Prometheus metrics) and then expose the number with aprometheus.GaugeFunc.
funcTransactionalGatherAndCompare¶added inv1.13.0
func TransactionalGatherAndCompare(gprometheus.TransactionalGatherer, expectedio.Reader, metricNames ...string)error
TransactionalGatherAndCompare gathers all metrics from the provided Gatherer and comparesit to an expected output read from the provided Reader in the Prometheus textexposition format. If any metricNames are provided, only metrics with thosenames are compared.
NOTE: Be mindful of accidental discrepancies between expected and metricNames; metricNames filterboth expected and gathered metrics. Seehttps://github.com/prometheus/client_golang/issues/1351.
Types¶
This section is empty.
Directories¶
Path | Synopsis |
---|---|
Package promlint provides a linter for Prometheus metrics. | Package promlint provides a linter for Prometheus metrics. |