Skip to content

Commit

Permalink
fix static check warnings by Goland.
Browse files Browse the repository at this point in the history
Signed-off-by: RainbowMango <renhongcai@huawei.com>
  • Loading branch information
RainbowMango committed Apr 23, 2020
1 parent 3c5e60e commit 6bd6811
Showing 1 changed file with 26 additions and 31 deletions.
57 changes: 26 additions & 31 deletions prometheus/testutil/promlint/promlint.go
Expand Up @@ -40,17 +40,12 @@ type Problem struct {
Text string
}

// problems is a slice of Problems with a helper method to easily append
// additional Problems to the slice.
type problems []Problem

// Add appends a new Problem to the slice for the specified metric, with
// the specified issue text.
func (p *problems) Add(mf dto.MetricFamily, text string) {
*p = append(*p, Problem{
// newProblem is helper function to create a Problem.
func newProblem(mf dto.MetricFamily, text string) Problem {
return Problem{
Metric: mf.GetName(),
Text: text,
})
}
}

// New creates a new Linter that reads an input stream of Prometheus metrics.
Expand Down Expand Up @@ -118,19 +113,19 @@ func lint(mf dto.MetricFamily) []Problem {

// lintHelp detects issues related to the help text for a metric.
func lintHelp(mf dto.MetricFamily) []Problem {
var problems problems
var problems []Problem

// Expect all metrics to have help text available.
if mf.Help == nil {
problems.Add(mf, "no help text")
problems = append(problems, newProblem(mf, "no help text"))
}

return problems
}

// lintMetricUnits detects issues with metric unit names.
func lintMetricUnits(mf dto.MetricFamily) []Problem {
var problems problems
var problems []Problem

unit, base, ok := metricUnits(*mf.Name)
if !ok {
Expand All @@ -143,25 +138,25 @@ func lintMetricUnits(mf dto.MetricFamily) []Problem {
return nil
}

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

return problems
}

// lintCounter detects issues specific to counters, as well as patterns that should
// only be used with counters.
func lintCounter(mf dto.MetricFamily) []Problem {
var problems problems
var problems []Problem

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

switch {
case isCounter && !hasTotalSuffix:
problems.Add(mf, `counter metrics should have "_total" suffix`)
problems = append(problems, newProblem(mf, `counter metrics should have "_total" suffix`))
case !isUntyped && !isCounter && hasTotalSuffix:
problems.Add(mf, `non-counter metrics should not have "_total" suffix`)
problems = append(problems, newProblem(mf, `non-counter metrics should not have "_total" suffix`))
}

return problems
Expand All @@ -176,32 +171,32 @@ func lintHistogramSummaryReserved(mf dto.MetricFamily) []Problem {
return nil
}

var problems problems
var problems []Problem

isHistogram := t == dto.MetricType_HISTOGRAM
isSummary := t == dto.MetricType_SUMMARY

n := mf.GetName()

if !isHistogram && strings.HasSuffix(n, "_bucket") {
problems.Add(mf, `non-histogram metrics should not have "_bucket" suffix`)
problems = append(problems, newProblem(mf, `non-histogram metrics should not have "_bucket" suffix`))
}
if !isHistogram && !isSummary && strings.HasSuffix(n, "_count") {
problems.Add(mf, `non-histogram and non-summary metrics should not have "_count" suffix`)
problems = append(problems, newProblem(mf, `non-histogram and non-summary metrics should not have "_count" suffix`))
}
if !isHistogram && !isSummary && strings.HasSuffix(n, "_sum") {
problems.Add(mf, `non-histogram and non-summary metrics should not have "_sum" suffix`)
problems = append(problems, newProblem(mf, `non-histogram and non-summary metrics should not have "_sum" suffix`))
}

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

if !isHistogram && ln == "le" {
problems.Add(mf, `non-histogram metrics should not have "le" label`)
problems = append(problems, newProblem(mf, `non-histogram metrics should not have "le" label`))
}
if !isSummary && ln == "quantile" {
problems.Add(mf, `non-summary metrics should not have "quantile" label`)
problems = append(problems, newProblem(mf, `non-summary metrics should not have "quantile" label`))
}
}
}
Expand All @@ -211,7 +206,7 @@ func lintHistogramSummaryReserved(mf dto.MetricFamily) []Problem {

// lintMetricTypeInName detects when metric types are included in the metric name.
func lintMetricTypeInName(mf dto.MetricFamily) []Problem {
var problems problems
var problems []Problem
n := strings.ToLower(mf.GetName())

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

typename := strings.ToLower(t)
if strings.Contains(n, "_"+typename+"_") || strings.HasSuffix(n, "_"+typename) {
problems.Add(mf, fmt.Sprintf(`metric name should not include type '%s'`, typename))
problems = append(problems, newProblem(mf, fmt.Sprintf(`metric name should not include type '%s'`, typename)))
}
}
return problems
}

// lintReservedChars detects colons in metric names.
func lintReservedChars(mf dto.MetricFamily) []Problem {
var problems problems
var problems []Problem
if strings.Contains(mf.GetName(), ":") {
problems.Add(mf, "metric names should not contain ':'")
problems = append(problems, newProblem(mf, "metric names should not contain ':'"))
}
return problems
}
Expand All @@ -240,15 +235,15 @@ var camelCase = regexp.MustCompile(`[a-z][A-Z]`)

// lintCamelCase detects metric names and label names written in camelCase.
func lintCamelCase(mf dto.MetricFamily) []Problem {
var problems problems
var problems []Problem
if camelCase.FindString(mf.GetName()) != "" {
problems.Add(mf, "metric names should be written in 'snake_case' not 'camelCase'")
problems = append(problems, newProblem(mf, "metric names should be written in 'snake_case' not 'camelCase'"))
}

for _, m := range mf.GetMetric() {
for _, l := range m.GetLabel() {
if camelCase.FindString(l.GetName()) != "" {
problems.Add(mf, "label names should be written in 'snake_case' not 'camelCase'")
problems = append(problems, newProblem(mf, "label names should be written in 'snake_case' not 'camelCase'"))
}
}
}
Expand All @@ -257,11 +252,11 @@ func lintCamelCase(mf dto.MetricFamily) []Problem {

// lintUnitAbbreviations detects abbreviated units in the metric name.
func lintUnitAbbreviations(mf dto.MetricFamily) []Problem {
var problems problems
var problems []Problem
n := strings.ToLower(mf.GetName())
for _, s := range unitAbbreviations {
if strings.Contains(n, "_"+s+"_") || strings.HasSuffix(n, "_"+s) {
problems.Add(mf, "metric names should not contain abbreviated units")
problems = append(problems, newProblem(mf, "metric names should not contain abbreviated units"))
}
}
return problems
Expand Down

0 comments on commit 6bd6811

Please sign in to comment.