mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
Simplify promlint problems gathering, use protobuf accessors
This commit is contained in:
parent
34a4813464
commit
cc4198f421
|
@ -39,6 +39,19 @@ type Problem struct {
|
||||||
Text string
|
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{
|
||||||
|
Metric: mf.GetName(),
|
||||||
|
Text: text,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// New creates a new Linter that reads an input stream of Prometheus metrics.
|
// New creates a new Linter that reads an input stream of Prometheus metrics.
|
||||||
// Only the text exposition format is supported.
|
// Only the text exposition format is supported.
|
||||||
func New(r io.Reader) *Linter {
|
func New(r io.Reader) *Linter {
|
||||||
|
@ -100,14 +113,11 @@ func lint(mf dto.MetricFamily) []Problem {
|
||||||
|
|
||||||
// lintHelp detects issues related to the help text for a metric.
|
// lintHelp detects issues related to the help text for a metric.
|
||||||
func lintHelp(mf dto.MetricFamily) []Problem {
|
func lintHelp(mf dto.MetricFamily) []Problem {
|
||||||
var problems []Problem
|
var problems problems
|
||||||
|
|
||||||
// Expect all metrics to have help text available.
|
// Expect all metrics to have help text available.
|
||||||
if mf.Help == nil {
|
if mf.Help == nil {
|
||||||
problems = append(problems, Problem{
|
problems.Add(mf, "no help text")
|
||||||
Metric: *mf.Name,
|
|
||||||
Text: "no help text",
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return problems
|
return problems
|
||||||
|
@ -115,7 +125,7 @@ func lintHelp(mf dto.MetricFamily) []Problem {
|
||||||
|
|
||||||
// lintMetricUnits detects issues with metric unit names.
|
// lintMetricUnits detects issues with metric unit names.
|
||||||
func lintMetricUnits(mf dto.MetricFamily) []Problem {
|
func lintMetricUnits(mf dto.MetricFamily) []Problem {
|
||||||
var problems []Problem
|
var problems problems
|
||||||
|
|
||||||
unit, base, ok := metricUnits(*mf.Name)
|
unit, base, ok := metricUnits(*mf.Name)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -128,10 +138,7 @@ func lintMetricUnits(mf dto.MetricFamily) []Problem {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
problems = append(problems, Problem{
|
problems.Add(mf, fmt.Sprintf("use base unit %q instead of %q", base, unit))
|
||||||
Metric: *mf.Name,
|
|
||||||
Text: fmt.Sprintf("use base unit %q instead of %q", base, unit),
|
|
||||||
})
|
|
||||||
|
|
||||||
return problems
|
return problems
|
||||||
}
|
}
|
||||||
|
@ -139,23 +146,17 @@ func lintMetricUnits(mf dto.MetricFamily) []Problem {
|
||||||
// lintCounter detects issues specific to counters, as well as patterns that should
|
// lintCounter detects issues specific to counters, as well as patterns that should
|
||||||
// only be used with counters.
|
// only be used with counters.
|
||||||
func lintCounter(mf dto.MetricFamily) []Problem {
|
func lintCounter(mf dto.MetricFamily) []Problem {
|
||||||
var problems []Problem
|
var problems problems
|
||||||
|
|
||||||
isCounter := *mf.Type == dto.MetricType_COUNTER
|
isCounter := mf.GetType() == dto.MetricType_COUNTER
|
||||||
isUntyped := *mf.Type == dto.MetricType_UNTYPED
|
isUntyped := mf.GetType() == dto.MetricType_UNTYPED
|
||||||
hasTotalSuffix := strings.HasSuffix(*mf.Name, "_total")
|
hasTotalSuffix := strings.HasSuffix(mf.GetName(), "_total")
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case isCounter && !hasTotalSuffix:
|
case isCounter && !hasTotalSuffix:
|
||||||
problems = append(problems, Problem{
|
problems.Add(mf, `counter metrics should have "_total" suffix`)
|
||||||
Metric: *mf.Name,
|
|
||||||
Text: `counter metrics should have "_total" suffix`,
|
|
||||||
})
|
|
||||||
case !isUntyped && !isCounter && hasTotalSuffix:
|
case !isUntyped && !isCounter && hasTotalSuffix:
|
||||||
problems = append(problems, Problem{
|
problems.Add(mf, `non-counter metrics should not have "_total" suffix`)
|
||||||
Metric: *mf.Name,
|
|
||||||
Text: `non-counter metrics should not have "_total" suffix`,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return problems
|
return problems
|
||||||
|
|
Loading…
Reference in a new issue