|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package testutil |
| 18 | + |
| 19 | +import ( |
| 20 | + "io" |
| 21 | + |
| 22 | + "github.com/prometheus/client_golang/prometheus/testutil/promlint" |
| 23 | +) |
| 24 | + |
| 25 | +// exceptionMetrics is an exception list of metrics which violates promlint rules. |
| 26 | +// |
| 27 | +// The original entries come from the existing metrics when we introduce promlint. |
| 28 | +// We setup this list for allow and not fail on the current violations. |
| 29 | +// Generally speaking, you need to fix the problem for a new metric rather than add it into the list. |
| 30 | +var exceptionMetrics = []string{ |
| 31 | + // kube-apiserver |
| 32 | + "aggregator_openapi_v2_regeneration_count", |
| 33 | + "apiserver_admission_step_admission_duration_seconds_summary", |
| 34 | + "apiserver_current_inflight_requests", |
| 35 | + "apiserver_longrunning_gauge", |
| 36 | + "apiserver_request_total", |
| 37 | + "authenticated_user_requests", |
| 38 | + "authentication_attempts", |
| 39 | + "get_token_count", |
| 40 | + "get_token_fail_count", |
| 41 | + "ssh_tunnel_open_count", |
| 42 | + "ssh_tunnel_open_fail_count", |
| 43 | + |
| 44 | + // kube-controller-manager |
| 45 | + "attachdetach_controller_forced_detaches", |
| 46 | + "authenticated_user_requests", |
| 47 | + "authentication_attempts", |
| 48 | + "get_token_count", |
| 49 | + "get_token_fail_count", |
| 50 | + "kubernetes_build_info", |
| 51 | + "node_collector_evictions_number", |
| 52 | + |
| 53 | + // kube-proxy |
| 54 | + "kubernetes_build_info", |
| 55 | + |
| 56 | + // kube-scheduler |
| 57 | + "scheduler_total_preemption_attempts", |
| 58 | + |
| 59 | + // kubelet-resource-v1alpha1 |
| 60 | + "container_cpu_usage_seconds_total", |
| 61 | + "node_cpu_usage_seconds_total", |
| 62 | +} |
| 63 | + |
| 64 | +// A Problem is an issue detected by a Linter. |
| 65 | +type Problem promlint.Problem |
| 66 | + |
| 67 | +// A Linter is a Prometheus metrics linter. It identifies issues with metric |
| 68 | +// names, types, and metadata, and reports them to the caller. |
| 69 | +type Linter struct { |
| 70 | + promLinter *promlint.Linter |
| 71 | +} |
| 72 | + |
| 73 | +// Lint performs a linting pass, returning a slice of Problems indicating any |
| 74 | +// issues found in the metrics stream. The slice is sorted by metric name |
| 75 | +// and issue description. |
| 76 | +func (l *Linter) Lint() ([]Problem, error) { |
| 77 | + promProblems, err := l.promLinter.Lint() |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + |
| 82 | + // Ignore problems those in exception list |
| 83 | + problems := make([]Problem, 0, len(promProblems)) |
| 84 | + for i := range promProblems { |
| 85 | + if !l.shouldIgnore(promProblems[i].Metric) { |
| 86 | + problems = append(problems, Problem(promProblems[i])) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return problems, nil |
| 91 | +} |
| 92 | + |
| 93 | +// shouldIgnore returns true if metric in the exception list, otherwise returns false. |
| 94 | +func (l *Linter) shouldIgnore(metricName string) bool { |
| 95 | + for i := range exceptionMetrics { |
| 96 | + if metricName == exceptionMetrics[i] { |
| 97 | + return true |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return false |
| 102 | +} |
| 103 | + |
| 104 | +// NewPromLinter creates a new Linter that reads an input stream of Prometheus metrics. |
| 105 | +// Only the text exposition format is supported. |
| 106 | +func NewPromLinter(r io.Reader) *Linter { |
| 107 | + return &Linter{ |
| 108 | + promLinter: promlint.New(r), |
| 109 | + } |
| 110 | +} |
0 commit comments