Skip to content

Commit d65cb65

Browse files
pohlyk8s-publishing-bot
authored andcommitted
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. Kubernetes-commit: 9f161590bee774e9aefb0ca89442fe9993cbee87
1 parent 8618ec8 commit d65cb65

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

metrics/testutil/testutil.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ import (
2020
"fmt"
2121
"io"
2222

23+
"github.com/prometheus/client_golang/prometheus"
2324
"github.com/prometheus/client_golang/prometheus/testutil"
25+
dto "github.com/prometheus/client_model/go"
2426

2527
apimachineryversion "k8s.io/apimachinery/pkg/version"
2628
"k8s.io/component-base/metrics"
@@ -33,6 +35,14 @@ type TB interface {
3335
Fatalf(format string, args ...any)
3436
}
3537

38+
// MetricFamily is a type alias which enables writing gatherers in tests
39+
// without importing prometheus directly (https://github.com/kubernetes/kubernetes/issues/99876).
40+
type MetricFamily = dto.MetricFamily
41+
42+
// GathererFunc is a type alias which enables writing gatherers as a function in tests
43+
// without importing prometheus directly (https://github.com/kubernetes/kubernetes/issues/99876).
44+
type GathererFunc = prometheus.GathererFunc
45+
3646
// CollectAndCompare registers the provided Collector with a newly created
3747
// pedantic Registry. It then does the same as GatherAndCompare, gathering the
3848
// metrics from the pedantic Registry.

0 commit comments

Comments
 (0)