Skip to content

Commit 50cda50

Browse files
committed
fix static check warnings by Goland.
Signed-off-by: RainbowMango <[email protected]>
1 parent 3c5e60e commit 50cda50

File tree

2 files changed

+31
-35
lines changed

2 files changed

+31
-35
lines changed

prometheus/testutil/promlint/promlint.go

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2017 The Prometheus Authors
1+
// Copyright 2020 The Prometheus Authors
22
// Licensed under the Apache License, Version 2.0 (the "License");
33
// you may not use this file except in compliance with the License.
44
// You may obtain a copy of the License at
@@ -21,8 +21,9 @@ import (
2121
"sort"
2222
"strings"
2323

24-
dto "github.com/prometheus/client_model/go"
2524
"github.com/prometheus/common/expfmt"
25+
26+
dto "github.com/prometheus/client_model/go"
2627
)
2728

2829
// A Linter is a Prometheus metrics linter. It identifies issues with metric
@@ -40,21 +41,16 @@ type Problem struct {
4041
Text string
4142
}
4243

43-
// problems is a slice of Problems with a helper method to easily append
44-
// additional Problems to the slice.
45-
type problems []Problem
46-
47-
// Add appends a new Problem to the slice for the specified metric, with
48-
// the specified issue text.
49-
func (p *problems) Add(mf dto.MetricFamily, text string) {
50-
*p = append(*p, Problem{
44+
// newProblem is helper function to create a Problem.
45+
func newProblem(mf dto.MetricFamily, text string) Problem {
46+
return Problem{
5147
Metric: mf.GetName(),
5248
Text: text,
53-
})
49+
}
5450
}
5551

5652
// New creates a new Linter that reads an input stream of Prometheus metrics.
57-
// Only the text exposition format is supported.
53+
// Only the Prometheus text exposition format is supported.
5854
func New(r io.Reader) *Linter {
5955
return &Linter{
6056
r: r,
@@ -118,19 +114,19 @@ func lint(mf dto.MetricFamily) []Problem {
118114

119115
// lintHelp detects issues related to the help text for a metric.
120116
func lintHelp(mf dto.MetricFamily) []Problem {
121-
var problems problems
117+
var problems []Problem
122118

123119
// Expect all metrics to have help text available.
124120
if mf.Help == nil {
125-
problems.Add(mf, "no help text")
121+
problems = append(problems, newProblem(mf, "no help text"))
126122
}
127123

128124
return problems
129125
}
130126

131127
// lintMetricUnits detects issues with metric unit names.
132128
func lintMetricUnits(mf dto.MetricFamily) []Problem {
133-
var problems problems
129+
var problems []Problem
134130

135131
unit, base, ok := metricUnits(*mf.Name)
136132
if !ok {
@@ -143,25 +139,25 @@ func lintMetricUnits(mf dto.MetricFamily) []Problem {
143139
return nil
144140
}
145141

146-
problems.Add(mf, fmt.Sprintf("use base unit %q instead of %q", base, unit))
142+
problems = append(problems, newProblem(mf, fmt.Sprintf("use base unit %q instead of %q", base, unit)))
147143

148144
return problems
149145
}
150146

151147
// lintCounter detects issues specific to counters, as well as patterns that should
152148
// only be used with counters.
153149
func lintCounter(mf dto.MetricFamily) []Problem {
154-
var problems problems
150+
var problems []Problem
155151

156152
isCounter := mf.GetType() == dto.MetricType_COUNTER
157153
isUntyped := mf.GetType() == dto.MetricType_UNTYPED
158154
hasTotalSuffix := strings.HasSuffix(mf.GetName(), "_total")
159155

160156
switch {
161157
case isCounter && !hasTotalSuffix:
162-
problems.Add(mf, `counter metrics should have "_total" suffix`)
158+
problems = append(problems, newProblem(mf, `counter metrics should have "_total" suffix`))
163159
case !isUntyped && !isCounter && hasTotalSuffix:
164-
problems.Add(mf, `non-counter metrics should not have "_total" suffix`)
160+
problems = append(problems, newProblem(mf, `non-counter metrics should not have "_total" suffix`))
165161
}
166162

167163
return problems
@@ -176,32 +172,32 @@ func lintHistogramSummaryReserved(mf dto.MetricFamily) []Problem {
176172
return nil
177173
}
178174

179-
var problems problems
175+
var problems []Problem
180176

181177
isHistogram := t == dto.MetricType_HISTOGRAM
182178
isSummary := t == dto.MetricType_SUMMARY
183179

184180
n := mf.GetName()
185181

186182
if !isHistogram && strings.HasSuffix(n, "_bucket") {
187-
problems.Add(mf, `non-histogram metrics should not have "_bucket" suffix`)
183+
problems = append(problems, newProblem(mf, `non-histogram metrics should not have "_bucket" suffix`))
188184
}
189185
if !isHistogram && !isSummary && strings.HasSuffix(n, "_count") {
190-
problems.Add(mf, `non-histogram and non-summary metrics should not have "_count" suffix`)
186+
problems = append(problems, newProblem(mf, `non-histogram and non-summary metrics should not have "_count" suffix`))
191187
}
192188
if !isHistogram && !isSummary && strings.HasSuffix(n, "_sum") {
193-
problems.Add(mf, `non-histogram and non-summary metrics should not have "_sum" suffix`)
189+
problems = append(problems, newProblem(mf, `non-histogram and non-summary metrics should not have "_sum" suffix`))
194190
}
195191

196192
for _, m := range mf.GetMetric() {
197193
for _, l := range m.GetLabel() {
198194
ln := l.GetName()
199195

200196
if !isHistogram && ln == "le" {
201-
problems.Add(mf, `non-histogram metrics should not have "le" label`)
197+
problems = append(problems, newProblem(mf, `non-histogram metrics should not have "le" label`))
202198
}
203199
if !isSummary && ln == "quantile" {
204-
problems.Add(mf, `non-summary metrics should not have "quantile" label`)
200+
problems = append(problems, newProblem(mf, `non-summary metrics should not have "quantile" label`))
205201
}
206202
}
207203
}
@@ -211,7 +207,7 @@ func lintHistogramSummaryReserved(mf dto.MetricFamily) []Problem {
211207

212208
// lintMetricTypeInName detects when metric types are included in the metric name.
213209
func lintMetricTypeInName(mf dto.MetricFamily) []Problem {
214-
var problems problems
210+
var problems []Problem
215211
n := strings.ToLower(mf.GetName())
216212

217213
for i, t := range dto.MetricType_name {
@@ -221,17 +217,17 @@ func lintMetricTypeInName(mf dto.MetricFamily) []Problem {
221217

222218
typename := strings.ToLower(t)
223219
if strings.Contains(n, "_"+typename+"_") || strings.HasSuffix(n, "_"+typename) {
224-
problems.Add(mf, fmt.Sprintf(`metric name should not include type '%s'`, typename))
220+
problems = append(problems, newProblem(mf, fmt.Sprintf(`metric name should not include type '%s'`, typename)))
225221
}
226222
}
227223
return problems
228224
}
229225

230226
// lintReservedChars detects colons in metric names.
231227
func lintReservedChars(mf dto.MetricFamily) []Problem {
232-
var problems problems
228+
var problems []Problem
233229
if strings.Contains(mf.GetName(), ":") {
234-
problems.Add(mf, "metric names should not contain ':'")
230+
problems = append(problems, newProblem(mf, "metric names should not contain ':'"))
235231
}
236232
return problems
237233
}
@@ -240,15 +236,15 @@ var camelCase = regexp.MustCompile(`[a-z][A-Z]`)
240236

241237
// lintCamelCase detects metric names and label names written in camelCase.
242238
func lintCamelCase(mf dto.MetricFamily) []Problem {
243-
var problems problems
239+
var problems []Problem
244240
if camelCase.FindString(mf.GetName()) != "" {
245-
problems.Add(mf, "metric names should be written in 'snake_case' not 'camelCase'")
241+
problems = append(problems, newProblem(mf, "metric names should be written in 'snake_case' not 'camelCase'"))
246242
}
247243

248244
for _, m := range mf.GetMetric() {
249245
for _, l := range m.GetLabel() {
250246
if camelCase.FindString(l.GetName()) != "" {
251-
problems.Add(mf, "label names should be written in 'snake_case' not 'camelCase'")
247+
problems = append(problems, newProblem(mf, "label names should be written in 'snake_case' not 'camelCase'"))
252248
}
253249
}
254250
}
@@ -257,11 +253,11 @@ func lintCamelCase(mf dto.MetricFamily) []Problem {
257253

258254
// lintUnitAbbreviations detects abbreviated units in the metric name.
259255
func lintUnitAbbreviations(mf dto.MetricFamily) []Problem {
260-
var problems problems
256+
var problems []Problem
261257
n := strings.ToLower(mf.GetName())
262258
for _, s := range unitAbbreviations {
263259
if strings.Contains(n, "_"+s+"_") || strings.HasSuffix(n, "_"+s) {
264-
problems.Add(mf, "metric names should not contain abbreviated units")
260+
problems = append(problems, newProblem(mf, "metric names should not contain abbreviated units"))
265261
}
266262
}
267263
return problems

prometheus/testutil/promlint/promlint_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2017 The Prometheus Authors
1+
// Copyright 2020 The Prometheus Authors
22
// Licensed under the Apache License, Version 2.0 (the "License");
33
// you may not use this file except in compliance with the License.
44
// You may obtain a copy of the License at

0 commit comments

Comments
 (0)