package promtest

import "github.com/influxdata/influxdb/kit/prom/promtest"

Package promtest provides helpers for parsing and extracting prometheus metrics. These functions are only intended to be called from test files, as there is a dependency on the standard library testing package.

Index

Functions

func FindMetric

func FindMetric(mfs []*dto.MetricFamily, name string, labels map[string]string) *dto.Metric

FindMetric iterates through mfs to find the first metric family matching name. If a metric family matches, then the metrics inside the family are searched, and the first metric whose labels match the given labels are returned. If no matches are found, FindMetric returns nil.

FindMetric assumes that the labels on the metric family are well formed, i.e. there are no duplicate label names, and the label values are not empty strings.

func FromHTTPResponse

func FromHTTPResponse(r *http.Response) ([]*dto.MetricFamily, error)

FromHTTPResponse parses the prometheus metrics from the given *http.Response. It relies on properly set response headers to correctly parse. It will unconditionally close the response body.

This is particularly helpful when testing the output of the /metrics endpoint of a service. However, for comprehensive testing of metrics, it usually makes more sense to add collectors to a registry and call Registry.Gather to get the metrics without involving HTTP.

func MustFindMetric

func MustFindMetric(tb testing.TB, mfs []*dto.MetricFamily, name string, labels map[string]string) *dto.Metric

MustFindMetric returns the matching metric, or if no matching metric could be found, it calls tb.Log with helpful output of what was actually available, before calling tb.FailNow.

func MustGather

func MustGather(tb testing.TB, g prometheus.Gatherer) []*dto.MetricFamily

MustGather calls g.Gather and calls tb.Fatal if there was an error.

Source Files

promtest.go

Version
v1.12.0 (latest)
Published
Apr 8, 2025
Platform
linux/amd64
Imports
8 packages
Last checked
2 days ago

Tools for package owners.