From a334eb1a49ddc3f9eb1c8861535662b067ab93af Mon Sep 17 00:00:00 2001 From: Ferran Rodenas Date: Sun, 8 Oct 2017 13:35:43 +0200 Subject: [PATCH] Return an error if metric cannot be registered `prometheus.MustRegister` panics if the metric cannot be registered. Instead, use `prometheus.Register` and return the error if the metric cannot be registered. Also, add tests for `RegisterMetricAndTrackRateLimiterUsage`. Signed-off-by: Ferran Rodenas --- pkg/util/metrics/BUILD | 10 +++++++ pkg/util/metrics/util.go | 4 ++- pkg/util/metrics/util_test.go | 54 +++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 pkg/util/metrics/util_test.go diff --git a/pkg/util/metrics/BUILD b/pkg/util/metrics/BUILD index 6c330b8f1e0d6..ceba0d122b839 100644 --- a/pkg/util/metrics/BUILD +++ b/pkg/util/metrics/BUILD @@ -3,6 +3,7 @@ package(default_visibility = ["//visibility:public"]) load( "@io_bazel_rules_go//go:def.bzl", "go_library", + "go_test", ) go_library( @@ -16,6 +17,15 @@ go_library( ], ) +go_test( + name = "go_default_test", + srcs = ["util_test.go"], + library = ":go_default_library", + deps = [ + "//vendor/k8s.io/client-go/util/flowcontrol:go_default_library", + ], +) + filegroup( name = "package-srcs", srcs = glob(["**"]), diff --git a/pkg/util/metrics/util.go b/pkg/util/metrics/util.go index e183be1d55e03..c1d92475622d7 100644 --- a/pkg/util/metrics/util.go +++ b/pkg/util/metrics/util.go @@ -51,7 +51,9 @@ func registerRateLimiterMetric(ownerName string) error { Help: fmt.Sprintf("A metric measuring the saturation of the rate limiter for %v", ownerName), }) rateLimiterMetrics[ownerName] = metric - prometheus.MustRegister(metric) + if err := prometheus.Register(metric); err != nil { + return fmt.Errorf("error registering rate limiter usage metric: %v", err) + } return nil } diff --git a/pkg/util/metrics/util_test.go b/pkg/util/metrics/util_test.go new file mode 100644 index 0000000000000..38e14b577b4a8 --- /dev/null +++ b/pkg/util/metrics/util_test.go @@ -0,0 +1,54 @@ +/* +Copyright 2017 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 metrics + +import ( + "strings" + "testing" + + "k8s.io/client-go/util/flowcontrol" +) + +func TestRegisterMetricAndTrackRateLimiterUsage(t *testing.T) { + testCases := []struct { + ownerName string + rateLimiter flowcontrol.RateLimiter + err string + }{ + { + ownerName: "owner_name", + rateLimiter: flowcontrol.NewTokenBucketRateLimiter(1, 1), + err: "", + }, + { + ownerName: "invalid-owner-name", + rateLimiter: flowcontrol.NewTokenBucketRateLimiter(1, 1), + err: "error registering rate limiter usage metric", + }, + } + + for i, tc := range testCases { + e := RegisterMetricAndTrackRateLimiterUsage(tc.ownerName, tc.rateLimiter) + if e != nil { + if tc.err == "" { + t.Errorf("[%d] unexpected error: %v", i, e) + } else if !strings.Contains(e.Error(), tc.err) { + t.Errorf("[%d] expected an error containing %q: %v", i, tc.err, e) + } + } + } +}