mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-11 21:12:07 +00:00
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:
parent
37b47f4724
commit
9f161590be
@ -37,7 +37,6 @@ source "${KUBE_ROOT}/hack/lib/util.sh"
|
|||||||
# See: https://github.com/kubernetes/kubernetes/issues/89267
|
# See: https://github.com/kubernetes/kubernetes/issues/89267
|
||||||
allowed_prometheus_importers=(
|
allowed_prometheus_importers=(
|
||||||
./cluster/images/etcd-version-monitor/etcd-version-monitor.go
|
./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.go
|
||||||
./staging/src/k8s.io/component-base/metrics/prometheusextension/timing_histogram_test.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
|
./staging/src/k8s.io/component-base/metrics/prometheusextension/timing_histogram_vec.go
|
||||||
|
@ -32,8 +32,6 @@ import (
|
|||||||
"github.com/onsi/gomega"
|
"github.com/onsi/gomega"
|
||||||
"github.com/onsi/gomega/gstruct"
|
"github.com/onsi/gomega/gstruct"
|
||||||
gomegatypes "github.com/onsi/gomega/types"
|
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/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
@ -1205,7 +1203,7 @@ device_taint_eviction_controller_pod_deletions_total %[1]d
|
|||||||
controller.metrics.PodDeletionsTotal.FQName(),
|
controller.metrics.PodDeletionsTotal.FQName(),
|
||||||
controller.metrics.PodDeletionsLatency.FQName(),
|
controller.metrics.PodDeletionsLatency.FQName(),
|
||||||
}
|
}
|
||||||
gather := func() ([]*dto.MetricFamily, error) {
|
gather := func() ([]*metricstestutil.MetricFamily, error) {
|
||||||
got, err := controller.metrics.Gather()
|
got, err := controller.metrics.Gather()
|
||||||
for _, mf := range got {
|
for _, mf := range got {
|
||||||
for _, m := range mf.Metric {
|
for _, m := range mf.Metric {
|
||||||
@ -1218,7 +1216,7 @@ device_taint_eviction_controller_pod_deletions_total %[1]d
|
|||||||
return got, err
|
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.
|
// TestEviction runs through the full flow of starting the controller and evicting one pod.
|
||||||
|
@ -20,7 +20,9 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/prometheus/client_golang/prometheus/testutil"
|
"github.com/prometheus/client_golang/prometheus/testutil"
|
||||||
|
dto "github.com/prometheus/client_model/go"
|
||||||
|
|
||||||
apimachineryversion "k8s.io/apimachinery/pkg/version"
|
apimachineryversion "k8s.io/apimachinery/pkg/version"
|
||||||
"k8s.io/component-base/metrics"
|
"k8s.io/component-base/metrics"
|
||||||
@ -33,6 +35,14 @@ type TB interface {
|
|||||||
Fatalf(format string, args ...any)
|
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
|
// CollectAndCompare registers the provided Collector with a newly created
|
||||||
// pedantic Registry. It then does the same as GatherAndCompare, gathering the
|
// pedantic Registry. It then does the same as GatherAndCompare, gathering the
|
||||||
// metrics from the pedantic Registry.
|
// metrics from the pedantic Registry.
|
||||||
|
Loading…
Reference in New Issue
Block a user