| 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 promlintscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/ |
| 16 | |
| 17 | import ( |
| 18 | "errorsscip-go gomod github.com/golang/go/src go1.23.0 errors/" |
| 19 | "ioscip-go gomod github.com/golang/go/src go1.23.0 io/" |
| 20 | "sortscip-go gomod github.com/golang/go/src go1.23.0 sort/" |
| 21 | |
| 22 | dtoscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/ "github.com/prometheus/client_model/goscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/" |
| 23 | "github.com/prometheus/common/expfmtscip-go gomod github.com/prometheus/common v0.66.1 `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 Linterscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/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 | rscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Linter#r. ioscip-go gomod github.com/golang/go/src go1.23.0 io/.Readerscip-go gomod github.com/golang/go/src go1.23.0 io/Reader# |
| 35 | mfsscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Linter#mfs. []*dtoscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/.MetricFamilyscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/MetricFamily# |
| 36 | |
| 37 | customValidationsscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Linter#customValidations. []Validationscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/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 Newscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/New().(r ioscip-go gomod github.com/golang/go/src go1.23.0 io/.Readerscip-go gomod github.com/golang/go/src go1.23.0 io/Reader#) *Linterscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Linter# { |
| 43 | return &Linterscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Linter#{ |
| 44 | rscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Linter#r.: r, |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // NewWithMetricFamilies creates a new Linter that reads from a slice of |
| 49 | // MetricFamily protobuf messages. |
| 50 | func NewWithMetricFamiliesscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/NewWithMetricFamilies().(mfs []*dtoscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/.MetricFamilyscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/MetricFamily#) *Linterscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Linter# { |
| 51 | return &Linterscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Linter#{ |
| 52 | mfsscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Linter#mfs.: mfs, |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // AddCustomValidations adds custom validations to the linter. |
| 57 | func (l *Linterscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Linter#) AddCustomValidationsscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Linter#AddCustomValidations().(vs ...Validationscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Validation#) { |
| 58 | if l.customValidationsscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Linter#customValidations. == nil { |
| 59 | l.customValidationsscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Linter#customValidations. = make([]Validationscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Validation#, 0, len(vs)) |
| 60 | } |
| 61 | l.customValidationsscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Linter#customValidations. = append(l.customValidationsscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Linter#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 *Linterscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Linter#) Lintscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Linter#Lint().() ([]Problemscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Problem#, error) { |
| 68 | var problems []Problemscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Problem# |
| 69 | |
| 70 | if l.rscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Linter#r. != nil { |
| 71 | d := expfmtscip-go gomod github.com/prometheus/common v0.66.1 `github.com/prometheus/common/expfmt`/.NewDecoderscip-go gomod github.com/prometheus/common v0.66.1 `github.com/prometheus/common/expfmt`/NewDecoder().(l.rscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Linter#r., expfmtscip-go gomod github.com/prometheus/common v0.66.1 `github.com/prometheus/common/expfmt`/.NewFormatscip-go gomod github.com/prometheus/common v0.66.1 `github.com/prometheus/common/expfmt`/NewFormat().(expfmtscip-go gomod github.com/prometheus/common v0.66.1 `github.com/prometheus/common/expfmt`/.TypeTextPlainscip-go gomod github.com/prometheus/common v0.66.1 `github.com/prometheus/common/expfmt`/TypeTextPlain.)) |
| 72 | |
| 73 | mf := &dtoscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/.MetricFamilyscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/MetricFamily#{} |
| 74 | for { |
| 75 | if err := d.Decodescip-go gomod github.com/prometheus/common v0.66.1 `github.com/prometheus/common/expfmt`/Decoder#Decode().(mf); err != nil { |
| 76 | if errorsscip-go gomod github.com/golang/go/src go1.23.0 errors/.Isscip-go gomod github.com/golang/go/src go1.23.0 errors/Is().(err, ioscip-go gomod github.com/golang/go/src go1.23.0 io/.EOFscip-go gomod github.com/golang/go/src go1.23.0 io/EOF.) { |
| 77 | break |
| 78 | } |
| 79 | |
| 80 | return nil, err |
| 81 | } |
| 82 | |
| 83 | problems = append(problems, l.lintscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Linter#lint().(mf)...) |
| 84 | } |
| 85 | } |
| 86 | for _, mf := range l.mfsscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Linter#mfs. { |
| 87 | problems = append(problems, l.lintscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Linter#lint().(mf)...) |
| 88 | } |
| 89 | |
| 90 | // Ensure deterministic output. |
| 91 | sortscip-go gomod github.com/golang/go/src go1.23.0 sort/.SliceStablescip-go gomod github.com/golang/go/src go1.23.0 sort/SliceStable().(problems, func(i, j int) bool { |
| 92 | if problems[i].Metricscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Problem#Metric. == problems[j].Metricscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Problem#Metric. { |
| 93 | return problems[i].Textscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Problem#Text. < problems[j].Textscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Problem#Text. |
| 94 | } |
| 95 | return problems[i].Metricscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Problem#Metric. < problems[j].Metricscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Problem#Metric. |
| 96 | }) |
| 97 | |
| 98 | return problems, nil |
| 99 | } |
| 100 | |
| 101 | // lint is the entry point for linting a single metric. |
| 102 | func (l *Linterscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Linter#) lintscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Linter#lint().(mf *dtoscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/.MetricFamilyscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/MetricFamily#) []Problemscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Problem# { |
| 103 | var problems []Problemscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Problem# |
| 104 | |
| 105 | for _, fn := range defaultValidationsscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/defaultValidations. { |
| 106 | errs := fn(mf) |
| 107 | for _, err := range errs { |
| 108 | problems = append(problems, newProblemscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/newProblem().(mf, err.Error())) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | if l.customValidationsscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Linter#customValidations. != nil { |
| 113 | for _, fn := range l.customValidationsscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/Linter#customValidations. { |
| 114 | errs := fn(mf) |
| 115 | for _, err := range errs { |
| 116 | problems = append(problems, newProblemscip-go gomod github.com/prometheus/client_golang . `github.com/prometheus/client_golang/prometheus/testutil/promlint`/newProblem().(mf, err.Error())) |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // TODO(mdlayher): lint rules for specific metrics types. |
| 122 | return problems |
| 123 | } |