package testutil
import "k8s.io/component-base/metrics/testutil"
Index ¶
- Variables
- func AssertHistogramTotalCount(t TB, name string, labelFilter map[string]string, wantCount int)
- func AssertVectorCount(t TB, name string, labelFilter map[string]string, wantCount int)
- func CollectAndCompare(c metrics.Collector, expected io.Reader, metricNames ...string) error
- func ComputeHistogramDelta(before, after model.Samples, label model.LabelName)
- func CustomCollectAndCompare(c metrics.StableCollector, expected io.Reader, metricNames ...string) error
- func GatherAndCompare(g metrics.Gatherer, expected io.Reader, metricNames ...string) error
- func GetCounterMetricValue(m metrics.CounterMetric) (float64, error)
- func GetCounterValuesFromGatherer(gatherer metrics.Gatherer, metricName string, lvMap map[string]string, labelName string) (map[string]float64, error)
- func GetGaugeMetricValue(m metrics.GaugeMetric) (float64, error)
- func GetHistogramMetricCount(m metrics.ObserverMetric) (uint64, error)
- func GetHistogramMetricValue(m metrics.ObserverMetric) (float64, error)
- func GetMetricValuesForLabel(ms Metrics, metricName, label string) map[string]int64
- func LabelsMatch(metric *dto.Metric, labelFilter map[string]string) bool
- func NewFakeKubeRegistry(ver string) metrics.KubeRegistry
- func ParseMetrics(data string, output *Metrics) error
- func PrintSample(sample *model.Sample) string
- func ScrapeAndCompare(url string, expected io.Reader, metricNames ...string) error
- func TextToMetricFamilies(in io.Reader) (map[string]*dto.MetricFamily, error)
- func ValidateMetrics(metrics Metrics, metricName string, expectedLabels ...string) error
- type GathererFunc
- type Histogram
- func (hist *Histogram) Average() float64
- func (hist *Histogram) Quantile(q float64) float64
- func (hist *Histogram) Validate() error
- type HistogramVec
- func GetHistogramVecFromGatherer(gatherer metrics.Gatherer, metricName string, lvMap map[string]string) (HistogramVec, error)
- func (vec HistogramVec) Average() float64
- func (vec HistogramVec) GetAggregatedSampleCount() uint64
- func (vec HistogramVec) GetAggregatedSampleSum() float64
- func (vec HistogramVec) Quantile(q float64) float64
- func (vec HistogramVec) Validate() error
- type Linter
- type MetricFamily
- type Metrics
- type Problem
- type TB
Variables ¶
var ( // MetricNameLabel is label under which model.Sample stores metric name MetricNameLabel model.LabelName = model.MetricNameLabel // QuantileLabel is label under which model.Sample stores latency quantile value QuantileLabel model.LabelName = model.QuantileLabel )
Functions ¶
func AssertHistogramTotalCount ¶
func AssertVectorCount ¶
func CollectAndCompare ¶
CollectAndCompare registers the provided Collector with a newly created pedantic Registry. It then does the same as GatherAndCompare, gathering the metrics from the pedantic Registry.
func ComputeHistogramDelta ¶
ComputeHistogramDelta computes the change in histogram metric for a selected label. Results are stored in after samples
func CustomCollectAndCompare ¶
func CustomCollectAndCompare(c metrics.StableCollector, expected io.Reader, metricNames ...string) error
CustomCollectAndCompare registers the provided StableCollector with a newly created registry. It then does the same as GatherAndCompare, gathering the metrics from the pedantic Registry.
func GatherAndCompare ¶
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 GetCounterMetricValue ¶
func GetCounterMetricValue(m metrics.CounterMetric) (float64, error)
GetCounterMetricValue extracts metric value from CounterMetric
func GetCounterValuesFromGatherer ¶
func GetCounterValuesFromGatherer(gatherer metrics.Gatherer, metricName string, lvMap map[string]string, labelName string) (map[string]float64, error)
GetCounterVecFromGatherer collects a counter that matches the given name from a gatherer implementing k8s.io/component-base/metrics.Gatherer interface. It returns all counter values that had a label with a certain name in a map that uses the label value as keys.
Used only for testing purposes where we need to gather metrics directly from a running binary (without metrics endpoint).
func GetGaugeMetricValue ¶
func GetGaugeMetricValue(m metrics.GaugeMetric) (float64, error)
GetGaugeMetricValue extracts metric value from GaugeMetric
func GetHistogramMetricCount ¶
func GetHistogramMetricCount(m metrics.ObserverMetric) (uint64, error)
GetHistogramMetricCount extracts count of all samples from ObserverMetric
func GetHistogramMetricValue ¶
func GetHistogramMetricValue(m metrics.ObserverMetric) (float64, error)
GetHistogramMetricValue extracts sum of all samples from ObserverMetric
func GetMetricValuesForLabel ¶
GetMetricValuesForLabel returns value of metric for a given dimension
func LabelsMatch ¶
LabelsMatch returns true if metric has all expected labels otherwise false
func NewFakeKubeRegistry ¶
func NewFakeKubeRegistry(ver string) metrics.KubeRegistry
NewFakeKubeRegistry creates a fake `KubeRegistry` that takes the input version as `build in version`. It should only be used in testing scenario especially for the deprecated metrics. The input version format should be `major.minor.patch`, e.g. '1.18.0'.
func ParseMetrics ¶
ParseMetrics parses Metrics from data returned from prometheus endpoint
func PrintSample ¶
PrintSample returns formatted representation of metric Sample
func ScrapeAndCompare ¶
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 TextToMetricFamilies ¶
TextToMetricFamilies reads 'in' as the simple and flat text-based exchange format and creates MetricFamily proto messages. It returns the MetricFamily proto messages in a map where the metric names are the keys, along with any error encountered.
func ValidateMetrics ¶
ValidateMetrics verifies if every sample of metric has all expected labels
Types ¶
type GathererFunc ¶
type GathererFunc = prometheus.GathererFunc
GathererFunc is a type alias which enables writing gatherers as a function in tests without importing prometheus directly (https://github.com/kubernetes/kubernetes/issues/99876).
type Histogram ¶
Histogram wraps prometheus histogram DTO (data transfer object)
func (*Histogram) Average ¶
Average computes histogram's average value
func (*Histogram) Quantile ¶
Quantile computes q-th quantile of a cumulative histogram. It's expected the histogram is valid (by calling Validate)
func (*Histogram) Validate ¶
Validate makes sure the wrapped histogram has all necessary fields set and with valid values.
type HistogramVec ¶
type HistogramVec []*Histogram
HistogramVec wraps a slice of Histogram. Note that each Histogram must have the same number of buckets.
func GetHistogramVecFromGatherer ¶
func GetHistogramVecFromGatherer(gatherer metrics.Gatherer, metricName string, lvMap map[string]string) (HistogramVec, error)
GetHistogramVecFromGatherer collects a metric, that matches the input labelValue map, from a gatherer implementing k8s.io/component-base/metrics.Gatherer interface. Used only for testing purposes where we need to gather metrics directly from a running binary (without metrics endpoint).
func (HistogramVec) Average ¶
func (vec HistogramVec) Average() float64
Average computes wrapped histograms' average value.
func (HistogramVec) GetAggregatedSampleCount ¶
func (vec HistogramVec) GetAggregatedSampleCount() uint64
GetAggregatedSampleCount aggregates the sample count of each inner Histogram.
func (HistogramVec) GetAggregatedSampleSum ¶
func (vec HistogramVec) GetAggregatedSampleSum() float64
GetAggregatedSampleSum aggregates the sample sum of each inner Histogram.
func (HistogramVec) Quantile ¶
func (vec HistogramVec) Quantile(q float64) float64
Quantile first aggregates inner buckets of each Histogram, and then computes q-th quantile of a cumulative histogram.
func (HistogramVec) Validate ¶
func (vec HistogramVec) Validate() error
Validate makes sure the wrapped histograms have all necessary fields set and with valid values.
type Linter ¶
type Linter struct {
// contains filtered or unexported fields
}
A Linter is a Prometheus metrics linter. It identifies issues with metric names, types, and metadata, and reports them to the caller.
func NewPromLinter ¶
NewPromLinter creates a new Linter that reads an input stream of Prometheus metrics. Only the text exposition format is supported.
func (*Linter) Lint ¶
Lint performs a linting pass, returning a slice of Problems indicating any issues found in the metrics stream. The slice is sorted by metric name and issue description.
type MetricFamily ¶
type MetricFamily = dto.MetricFamily
MetricFamily is a type alias which enables writing gatherers in tests without importing prometheus directly (https://github.com/kubernetes/kubernetes/issues/99876).
type Metrics ¶
Metrics is generic metrics for other specific metrics
func NewMetrics ¶
func NewMetrics() Metrics
NewMetrics returns new metrics which are initialized.
func (*Metrics) Equal ¶
Equal returns true if all metrics are the same as the arguments.
type Problem ¶
A Problem is an issue detected by a Linter.
func (*Problem) String ¶
type TB ¶
type TB interface { Logf(format string, args ...any) Errorf(format string, args ...any) Fatalf(format string, args ...any) }
Source Files ¶
metrics.go promlint.go testutil.go
- Version
- v0.33.0 (latest)
- Published
- Apr 23, 2025
- Platform
- linux/amd64
- Imports
- 15 packages
- Last checked
- 7 hours ago –
Tools for package owners.