| 1 |
// Copyright 2020 The Prometheus Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
// Package promlint provides a linter for Prometheus metrics. |
| 15 |
package promlint |
| 16 |
|
| 17 |
import ( |
| 18 |
"errors" |
| 19 |
"io" |
| 20 |
"sort" |
| 21 |
|
| 22 |
dto "github.com/prometheus/client_model/go" |
| 23 |
"github.com/prometheus/common/expfmt" |
| 24 |
) |
| 25 |
|
| 26 |
// A Linter is a Prometheus metrics linter. It identifies issues with metric |
| 27 |
// names, types, and metadata, and reports them to the caller. |
| 28 |
type Linter struct { |
| 29 |
// The linter will read metrics in the Prometheus text format from r and |
| 30 |
// then lint it, _and_ it will lint the metrics provided directly as |
| 31 |
// MetricFamily proto messages in mfs. Note, however, that the current |
| 32 |
// constructor functions New and NewWithMetricFamilies only ever set one |
| 33 |
// of them. |
| 34 |
r io.Reader |
| 35 |
mfs []*dto.MetricFamily |
| 36 |
|
| 37 |
customValidations []Validation |
| 38 |
} |
| 39 |
|
| 40 |
// New creates a new Linter that reads an input stream of Prometheus metrics in |
| 41 |
// the Prometheus text exposition format. |
| 42 |
func New(r io.Reader) *Linter { |
| 43 |
return &Linter{ |
| 44 |
r: r, |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
// NewWithMetricFamilies creates a new Linter that reads from a slice of |
| 49 |
// MetricFamily protobuf messages. |
| 50 |
func NewWithMetricFamilies(mfs []*dto.MetricFamily) *Linter { |
| 51 |
return &Linter{ |
| 52 |
mfs: mfs, |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
// AddCustomValidations adds custom validations to the linter. |
| 57 |
func (l *Linter) AddCustomValidations(vs ...Validation) { |
| 58 |
if l.customValidations == nil { |
| 59 |
l.customValidations = make([]Validation, 0, len(vs)) |
| 60 |
} |
| 61 |
l.customValidations = append(l.customValidations, vs...) |
| 62 |
} |
| 63 |
|
| 64 |
// Lint performs a linting pass, returning a slice of Problems indicating any |
| 65 |
// issues found in the metrics stream. The slice is sorted by metric name |
| 66 |
// and issue description. |
| 67 |
func (l *Linter) Lint() ([]Problem, error) { |
| 68 |
var problems []Problem |
| 69 |
|
| 70 |
if l.r != nil { |
| 71 |
d := expfmt.NewDecoder(l.r, expfmt.NewFormat(expfmt.TypeTextPlain)) |
| 72 |
|
| 73 |
mf := &dto.MetricFamily{} |
| 74 |
for { |
| 75 |
if err := d.Decode(mf); err != nil { |
| 76 |
if errors.Is(err, io.EOF) { |
| 77 |
break |
| 78 |
} |
| 79 |
|
| 80 |
return nil, err |
| 81 |
} |
| 82 |
|
| 83 |
problems = append(problems, l.lint(mf)...) |
| 84 |
} |
| 85 |
} |
| 86 |
for _, mf := range l.mfs { |
| 87 |
problems = append(problems, l.lint(mf)...) |
| 88 |
} |
| 89 |
|
| 90 |
// Ensure deterministic output. |
| 91 |
sort.SliceStable(problems, func(i, j int) bool { |
| 92 |
if problems[i].Metric == problems[j].Metric { |
| 93 |
return problems[i].Text < problems[j].Text |
| 94 |
} |
| 95 |
return problems[i].Metric < problems[j].Metric |
| 96 |
}) |
| 97 |
|
| 98 |
return problems, nil |
| 99 |
} |
| 100 |
|
| 101 |
// lint is the entry point for linting a single metric. |
| 102 |
func (l *Linter) lint(mf *dto.MetricFamily) []Problem { |
| 103 |
var problems []Problem |
| 104 |
|
| 105 |
for _, fn := range defaultValidations { |
| 106 |
errs := fn(mf) |
| 107 |
for _, err := range errs { |
| 108 |
problems = append(problems, newProblem(mf, err.Error())) |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
if l.customValidations != nil { |
| 113 |
for _, fn := range l.customValidations { |
| 114 |
errs := fn(mf) |
| 115 |
for _, err := range errs { |
| 116 |
problems = append(problems, newProblem(mf, err.Error())) |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
// TODO(mdlayher): lint rules for specific metrics types. |
| 122 |
return problems |
| 123 |
} |