tools

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2022 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package tools provides utilities for writing tests for code that produces Prometheus metrics.

MetricName, MetricLabel and MetricValue provide an easy way to validate metrics during unit testing:

func TestMetrics(t *testing.T) {
	...
	ch := make(chan prometheus.Metric)
	go yourMetric.Collect(ch)

	m := <-ch
	assert.Equal(t, "foo_bar_gauge", metrics.MetricName(m))
	assert.Equal(t, 1.0, metrics.MetricValue(m).GetGauge().GetValue())
	assert.Equal(t, "valueA", metrics.MetricLabel(m, "labelA"))
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MetricLabel

func MetricLabel(metric prometheus.Metric, labelName string) (value string)

MetricLabel returns the value of a metric's label. Panics if metric is not a valid Prometheus metric.

func MetricName

func MetricName(metric prometheus.Metric) (name string)

MetricName returns the name of the provided Prometheus metric.

func MetricValue

func MetricValue(metric prometheus.Metric) *pcg.Metric

MetricValue converts a Prometheus metric to a prometheus/client_model/go metric, so the caller can read its value:

ch := make(chan prometheus.Metric)
go c.Collect(ch)

m := <-ch
metric := metrics.MetricValue(m)
assert.Equal(t, 1.0, metric.GetGauge().GetValue())

Panics if metric is not a valid Prometheus metric.

Types

This section is empty.

Jump to

Keyboard shortcuts

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