New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable promlint in metrics tests #92071
Merged
k8s-ci-robot
merged 2 commits into
kubernetes:master
from
RainbowMango:pr_force_promlint_in_testutils
Jun 19, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -17,7 +17,9 @@ limitations under the License. | |||||
package testutil | ||||||
|
||||||
import ( | ||||||
"fmt" | ||||||
"io" | ||||||
"strings" | ||||||
|
||||||
"github.com/prometheus/client_golang/prometheus/testutil/promlint" | ||||||
) | ||||||
|
@@ -28,14 +30,26 @@ import ( | |||||
// We setup this list for allow and not fail on the current violations. | ||||||
// Generally speaking, you need to fix the problem for a new metric rather than add it into the list. | ||||||
var exceptionMetrics = []string{ | ||||||
// k8s.io/kubernetes/vendor/k8s.io/apiserver/pkg/server/egressselector | ||||||
"apiserver_egress_dialer_dial_failure_count", // counter metrics should have "_total" suffix | ||||||
|
||||||
// k8s.io/apiserver/pkg/util/flowcontrol/fairqueuing/queueset | ||||||
"apiserver_flowcontrol_current_inqueue_requests", // label names should be written in 'snake_case' not 'camelCase', | ||||||
"apiserver_flowcontrol_current_executing_requests", // label names should be written in 'snake_case' not 'camelCase' | ||||||
"apiserver_flowcontrol_rejected_requests_total", // label names should be written in 'snake_case' not 'camelCase' | ||||||
|
||||||
// k8s.io/kubernetes/vendor/k8s.io/apiserver/pkg/server/healthz | ||||||
"apiserver_request_total", // label names should be written in 'snake_case' not 'camelCase' | ||||||
|
||||||
// k8s.io/kubernetes/vendor/k8s.io/apiserver/pkg/endpoints/filters | ||||||
"authenticated_user_requests", // counter metrics should have "_total" suffix | ||||||
"authentication_attempts", // counter metrics should have "_total" suffix | ||||||
|
||||||
// kube-apiserver | ||||||
"aggregator_openapi_v2_regeneration_count", | ||||||
"apiserver_admission_step_admission_duration_seconds_summary", | ||||||
"apiserver_current_inflight_requests", | ||||||
"apiserver_longrunning_gauge", | ||||||
"apiserver_request_total", | ||||||
"authenticated_user_requests", | ||||||
"authentication_attempts", | ||||||
"get_token_count", | ||||||
"get_token_fail_count", | ||||||
"ssh_tunnel_open_count", | ||||||
|
@@ -49,14 +63,22 @@ var exceptionMetrics = []string{ | |||||
"get_token_fail_count", | ||||||
"node_collector_evictions_number", | ||||||
|
||||||
// kubelet-resource-v1alpha1 | ||||||
"container_cpu_usage_seconds_total", | ||||||
"node_cpu_usage_seconds_total", | ||||||
// k8s.io/kubernetes/pkg/kubelet/server/stats | ||||||
"container_cpu_usage_seconds_total", // non-counter metrics should not have "_total" suffix | ||||||
"node_cpu_usage_seconds_total", // non-counter metrics should not have "_total" suffix | ||||||
|
||||||
// k8s.io/kubernetes/pkg/kubelet/pleg | ||||||
"kubelet_running_container_count", // non-histogram and non-summary metrics should not have "_count" suffix | ||||||
"kubelet_running_pod_count", // non-histogram and non-summary metrics should not have "_count" suffix | ||||||
} | ||||||
|
||||||
// A Problem is an issue detected by a Linter. | ||||||
type Problem promlint.Problem | ||||||
|
||||||
func (p *Problem) String() string { | ||||||
return fmt.Sprintf("%s:%s", p.Metric, p.Text) | ||||||
} | ||||||
|
||||||
// A Linter is a Prometheus metrics linter. It identifies issues with metric | ||||||
// names, types, and metadata, and reports them to the caller. | ||||||
type Linter struct { | ||||||
|
@@ -101,3 +123,42 @@ func NewPromLinter(r io.Reader) *Linter { | |||||
promLinter: promlint.New(r), | ||||||
} | ||||||
} | ||||||
|
||||||
func mergeProblems(problems []Problem) string { | ||||||
var problemsMsg []string | ||||||
|
||||||
for index := range problems { | ||||||
problemsMsg = append(problemsMsg, problems[index].String()) | ||||||
} | ||||||
|
||||||
return strings.Join(problemsMsg, ",") | ||||||
} | ||||||
|
||||||
// shouldIgnore returns true if metric in the exception list, otherwise returns false. | ||||||
func shouldIgnore(metricName string) bool { | ||||||
for i := range exceptionMetrics { | ||||||
if metricName == exceptionMetrics[i] { | ||||||
return true | ||||||
} | ||||||
} | ||||||
|
||||||
return false | ||||||
} | ||||||
|
||||||
// getLintError will ignore the metrics in exception list and converts lint problem to error. | ||||||
func getLintError(problems []promlint.Problem) error { | ||||||
var filteredProblems []Problem | ||||||
for _, problem := range problems { | ||||||
if shouldIgnore(problem.Metric) { | ||||||
continue | ||||||
} | ||||||
|
||||||
filteredProblems = append(filteredProblems, Problem(problem)) | ||||||
} | ||||||
|
||||||
if len(filteredProblems) == 0 { | ||||||
return nil | ||||||
} | ||||||
|
||||||
return fmt.Errorf("lint error: %s", mergeProblems(filteredProblems)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: why not convert the array of exception metrics to a map instead of implementing another iterator?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've thought about it. The exception list originally organized by components, some metrics with the same name may exist in more than one component.
It's worth to reconsider it. Maybe map[string]struct{}.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll have collisions either way, no?