From 159163247e1f54e08ab3389b1cbe3bb7f36cdadd Mon Sep 17 00:00:00 2001 From: RainbowMango Date: Mon, 30 Sep 2019 10:17:32 +0800 Subject: [PATCH 1/2] Introduce testutil package to support metrics testing. --- .../metrics/legacyregistry/registry.go | 2 +- .../metrics/testutil/testutil.go | 33 +++++++++++++++++++ .../k8s.io/component-base/metrics/wrappers.go | 6 ++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 staging/src/k8s.io/component-base/metrics/testutil/testutil.go diff --git a/staging/src/k8s.io/component-base/metrics/legacyregistry/registry.go b/staging/src/k8s.io/component-base/metrics/legacyregistry/registry.go index bc08e3c8904..fb963ad3200 100644 --- a/staging/src/k8s.io/component-base/metrics/legacyregistry/registry.go +++ b/staging/src/k8s.io/component-base/metrics/legacyregistry/registry.go @@ -26,7 +26,7 @@ import ( var ( defaultRegistry = metrics.NewKubeRegistry() // DefaultGatherer exposes the global registry gatherer - DefaultGatherer prometheus.Gatherer = defaultRegistry + DefaultGatherer metrics.Gatherer = defaultRegistry ) func init() { diff --git a/staging/src/k8s.io/component-base/metrics/testutil/testutil.go b/staging/src/k8s.io/component-base/metrics/testutil/testutil.go new file mode 100644 index 00000000000..69e2e630fa2 --- /dev/null +++ b/staging/src/k8s.io/component-base/metrics/testutil/testutil.go @@ -0,0 +1,33 @@ +/* +Copyright 2019 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package testutil + +import ( + "io" + + "github.com/prometheus/client_golang/prometheus/testutil" + + "k8s.io/component-base/metrics" +) + +// 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 GatherAndCompare(g metrics.Gatherer, expected io.Reader, metricNames ...string) error { + return testutil.GatherAndCompare(g, expected, metricNames...) +} diff --git a/staging/src/k8s.io/component-base/metrics/wrappers.go b/staging/src/k8s.io/component-base/metrics/wrappers.go index fa40314dc6e..f41dacd1169 100644 --- a/staging/src/k8s.io/component-base/metrics/wrappers.go +++ b/staging/src/k8s.io/component-base/metrics/wrappers.go @@ -79,3 +79,9 @@ type PromRegistry interface { Unregister(prometheus.Collector) bool Gather() ([]*dto.MetricFamily, error) } + +// Gatherer is the interface for the part of a registry in charge of gathering +// the collected metrics into a number of MetricFamilies. +type Gatherer interface { + prometheus.Gatherer +} From 1395acba4a04086b28ad549f15392bc8392ebcf5 Mon Sep 17 00:00:00 2001 From: RainbowMango Date: Mon, 30 Sep 2019 12:09:16 +0800 Subject: [PATCH 2/2] Add bazel by hack/update-bazel.sh --- .../src/k8s.io/component-base/metrics/BUILD | 1 + .../component-base/metrics/testutil/BUILD | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 staging/src/k8s.io/component-base/metrics/testutil/BUILD diff --git a/staging/src/k8s.io/component-base/metrics/BUILD b/staging/src/k8s.io/component-base/metrics/BUILD index 12c2d60d054..c3d3638ffe5 100644 --- a/staging/src/k8s.io/component-base/metrics/BUILD +++ b/staging/src/k8s.io/component-base/metrics/BUILD @@ -73,6 +73,7 @@ filegroup( "//staging/src/k8s.io/component-base/metrics/prometheus/restclient:all-srcs", "//staging/src/k8s.io/component-base/metrics/prometheus/version:all-srcs", "//staging/src/k8s.io/component-base/metrics/prometheus/workqueue:all-srcs", + "//staging/src/k8s.io/component-base/metrics/testutil:all-srcs", ], tags = ["automanaged"], ) diff --git a/staging/src/k8s.io/component-base/metrics/testutil/BUILD b/staging/src/k8s.io/component-base/metrics/testutil/BUILD new file mode 100644 index 00000000000..026b05fc034 --- /dev/null +++ b/staging/src/k8s.io/component-base/metrics/testutil/BUILD @@ -0,0 +1,27 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = ["testutil.go"], + importmap = "k8s.io/kubernetes/vendor/k8s.io/component-base/metrics/testutil", + importpath = "k8s.io/component-base/metrics/testutil", + visibility = ["//visibility:public"], + deps = [ + "//staging/src/k8s.io/component-base/metrics:go_default_library", + "//vendor/github.com/prometheus/client_golang/prometheus/testutil:go_default_library", + ], +) + +filegroup( + name = "package-srcs", + srcs = glob(["**"]), + tags = ["automanaged"], + visibility = ["//visibility:private"], +) + +filegroup( + name = "all-srcs", + srcs = [":package-srcs"], + tags = ["automanaged"], + visibility = ["//visibility:public"], +)