metrics testing: add type aliases to avoid direct prometheus imports

In tests it is sometimes unavoidable to use the Prometheus types directly,
for example when writing a custom gatherer which needs to normalize data
before testing it. device_taint_eviction_test.go does this to strip
out unpredictable data in a histogram.

With type aliases in a package that is explicitly meant for tests we
can avoid adding exceptions for such tests to the global exception list.
This commit is contained in:
Patrick Ohly 2025-03-18 08:56:53 +01:00
parent 37b47f4724
commit 9f161590be
3 changed files with 12 additions and 5 deletions

View File

@ -37,7 +37,6 @@ source "${KUBE_ROOT}/hack/lib/util.sh"
# See: https://github.com/kubernetes/kubernetes/issues/89267
allowed_prometheus_importers=(
./cluster/images/etcd-version-monitor/etcd-version-monitor.go
./pkg/controller/devicetainteviction/device_taint_eviction_test.go
./staging/src/k8s.io/component-base/metrics/prometheusextension/timing_histogram.go
./staging/src/k8s.io/component-base/metrics/prometheusextension/timing_histogram_test.go
./staging/src/k8s.io/component-base/metrics/prometheusextension/timing_histogram_vec.go

View File

@ -32,8 +32,6 @@ import (
"github.com/onsi/gomega"
"github.com/onsi/gomega/gstruct"
gomegatypes "github.com/onsi/gomega/types"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -1205,7 +1203,7 @@ device_taint_eviction_controller_pod_deletions_total %[1]d
controller.metrics.PodDeletionsTotal.FQName(),
controller.metrics.PodDeletionsLatency.FQName(),
}
gather := func() ([]*dto.MetricFamily, error) {
gather := func() ([]*metricstestutil.MetricFamily, error) {
got, err := controller.metrics.Gather()
for _, mf := range got {
for _, m := range mf.Metric {
@ -1218,7 +1216,7 @@ device_taint_eviction_controller_pod_deletions_total %[1]d
return got, err
}
return metricstestutil.GatherAndCompare(prometheus.GathererFunc(gather), strings.NewReader(expectedMetric), names...)
return metricstestutil.GatherAndCompare(metricstestutil.GathererFunc(gather), strings.NewReader(expectedMetric), names...)
}
// TestEviction runs through the full flow of starting the controller and evicting one pod.

View File

@ -20,7 +20,9 @@ import (
"fmt"
"io"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
dto "github.com/prometheus/client_model/go"
apimachineryversion "k8s.io/apimachinery/pkg/version"
"k8s.io/component-base/metrics"
@ -33,6 +35,14 @@ type TB interface {
Fatalf(format string, args ...any)
}
// MetricFamily is a type alias which enables writing gatherers in tests
// without importing prometheus directly (https://github.com/kubernetes/kubernetes/issues/99876).
type MetricFamily = dto.MetricFamily
// 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 GathererFunc = prometheus.GathererFunc
// 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.