@@ -17,7 +17,9 @@ limitations under the License.
17
17
package testutil
18
18
19
19
import (
20
+ "fmt"
20
21
"io"
22
+ "strings"
21
23
22
24
"github.com/prometheus/client_golang/prometheus/testutil/promlint"
23
25
)
@@ -28,14 +30,26 @@ import (
28
30
// We setup this list for allow and not fail on the current violations.
29
31
// Generally speaking, you need to fix the problem for a new metric rather than add it into the list.
30
32
var exceptionMetrics = []string {
33
+ // k8s.io/kubernetes/vendor/k8s.io/apiserver/pkg/server/egressselector
34
+ "apiserver_egress_dialer_dial_failure_count" , // counter metrics should have "_total" suffix
35
+
36
+ // k8s.io/apiserver/pkg/util/flowcontrol/fairqueuing/queueset
37
+ "apiserver_flowcontrol_current_inqueue_requests" , // label names should be written in 'snake_case' not 'camelCase',
38
+ "apiserver_flowcontrol_current_executing_requests" , // label names should be written in 'snake_case' not 'camelCase'
39
+ "apiserver_flowcontrol_rejected_requests_total" , // label names should be written in 'snake_case' not 'camelCase'
40
+
41
+ // k8s.io/kubernetes/vendor/k8s.io/apiserver/pkg/server/healthz
42
+ "apiserver_request_total" , // label names should be written in 'snake_case' not 'camelCase'
43
+
44
+ // k8s.io/kubernetes/vendor/k8s.io/apiserver/pkg/endpoints/filters
45
+ "authenticated_user_requests" , // counter metrics should have "_total" suffix
46
+ "authentication_attempts" , // counter metrics should have "_total" suffix
47
+
31
48
// kube-apiserver
32
49
"aggregator_openapi_v2_regeneration_count" ,
33
50
"apiserver_admission_step_admission_duration_seconds_summary" ,
34
51
"apiserver_current_inflight_requests" ,
35
52
"apiserver_longrunning_gauge" ,
36
- "apiserver_request_total" ,
37
- "authenticated_user_requests" ,
38
- "authentication_attempts" ,
39
53
"get_token_count" ,
40
54
"get_token_fail_count" ,
41
55
"ssh_tunnel_open_count" ,
@@ -49,14 +63,22 @@ var exceptionMetrics = []string{
49
63
"get_token_fail_count" ,
50
64
"node_collector_evictions_number" ,
51
65
52
- // kubelet-resource-v1alpha1
53
- "container_cpu_usage_seconds_total" ,
54
- "node_cpu_usage_seconds_total" ,
66
+ // k8s.io/kubernetes/pkg/kubelet/server/stats
67
+ "container_cpu_usage_seconds_total" , // non-counter metrics should not have "_total" suffix
68
+ "node_cpu_usage_seconds_total" , // non-counter metrics should not have "_total" suffix
69
+
70
+ // k8s.io/kubernetes/pkg/kubelet/pleg
71
+ "kubelet_running_container_count" , // non-histogram and non-summary metrics should not have "_count" suffix
72
+ "kubelet_running_pod_count" , // non-histogram and non-summary metrics should not have "_count" suffix
55
73
}
56
74
57
75
// A Problem is an issue detected by a Linter.
58
76
type Problem promlint.Problem
59
77
78
+ func (p * Problem ) String () string {
79
+ return fmt .Sprintf ("%s:%s" , p .Metric , p .Text )
80
+ }
81
+
60
82
// A Linter is a Prometheus metrics linter. It identifies issues with metric
61
83
// names, types, and metadata, and reports them to the caller.
62
84
type Linter struct {
@@ -101,3 +123,42 @@ func NewPromLinter(r io.Reader) *Linter {
101
123
promLinter : promlint .New (r ),
102
124
}
103
125
}
126
+
127
+ func mergeProblems (problems []Problem ) string {
128
+ var problemsMsg []string
129
+
130
+ for index := range problems {
131
+ problemsMsg = append (problemsMsg , problems [index ].String ())
132
+ }
133
+
134
+ return strings .Join (problemsMsg , "," )
135
+ }
136
+
137
+ // shouldIgnore returns true if metric in the exception list, otherwise returns false.
138
+ func shouldIgnore (metricName string ) bool {
139
+ for i := range exceptionMetrics {
140
+ if metricName == exceptionMetrics [i ] {
141
+ return true
142
+ }
143
+ }
144
+
145
+ return false
146
+ }
147
+
148
+ // getLintError will ignore the metrics in exception list and converts lint problem to error.
149
+ func getLintError (problems []promlint.Problem ) error {
150
+ var filteredProblems []Problem
151
+ for _ , problem := range problems {
152
+ if shouldIgnore (problem .Metric ) {
153
+ continue
154
+ }
155
+
156
+ filteredProblems = append (filteredProblems , Problem (problem ))
157
+ }
158
+
159
+ if len (filteredProblems ) == 0 {
160
+ return nil
161
+ }
162
+
163
+ return fmt .Errorf ("lint error: %s" , mergeProblems (filteredProblems ))
164
+ }
0 commit comments