Skip to content

Commit

Permalink
Merge pull request kubernetes#53571 from frodenas/util-metrics-error
Browse files Browse the repository at this point in the history
Automatic merge from submit-queue (batch tested with PRs 53444, 52067, 53571, 53182). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Return an error if metric cannot be registered

**What this PR does / why we need it**:

`prometheus.MustRegister` panics if a metric cannot be registered. This PR replaces it with `prometheus.Register`, as it does not panic, and returns the error if the metric cannot be registered.

I also adds lacking tests for `RegisterMetricAndTrackRateLimiterUsage`.

**Which issue this PR fixes**:

Fixes kubernetes#52872

**Special notes for your reviewer**:

None of the `metrics.RegisterMetricAndTrackRateLimiterUsage` invocations check the returned error, so I plan to submit new PRs to address this.

**Release note**:

```release-note
NONE
```
/sig instrumentation
  • Loading branch information
Kubernetes Submit Queue authored Oct 10, 2017
2 parents c381022 + a334eb1 commit 194501c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
10 changes: 10 additions & 0 deletions pkg/util/metrics/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)

go_library(
Expand All @@ -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(["**"]),
Expand Down
4 changes: 3 additions & 1 deletion pkg/util/metrics/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
54 changes: 54 additions & 0 deletions pkg/util/metrics/util_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
}

0 comments on commit 194501c

Please sign in to comment.