forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kubernetes#53571 from frodenas/util-metrics-error
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
Showing
3 changed files
with
67 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |