2020-04-23 09:10:45 +03:00
|
|
|
// Copyright 2020 The Prometheus Authors
|
2020-04-23 09:09:29 +03:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// Package promlint provides a linter for Prometheus metrics.
|
|
|
|
package promlint
|
|
|
|
|
|
|
|
import (
|
2022-08-03 07:30:51 +03:00
|
|
|
"errors"
|
2020-04-23 09:09:29 +03:00
|
|
|
"io"
|
|
|
|
"sort"
|
2020-04-23 09:10:45 +03:00
|
|
|
|
|
|
|
dto "github.com/prometheus/client_model/go"
|
2023-10-10 12:38:10 +03:00
|
|
|
"github.com/prometheus/common/expfmt"
|
2020-04-23 09:09:29 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// A Linter is a Prometheus metrics linter. It identifies issues with metric
|
|
|
|
// names, types, and metadata, and reports them to the caller.
|
|
|
|
type Linter struct {
|
2020-04-25 16:59:53 +03:00
|
|
|
// The linter will read metrics in the Prometheus text format from r and
|
|
|
|
// then lint it, _and_ it will lint the metrics provided directly as
|
|
|
|
// MetricFamily proto messages in mfs. Note, however, that the current
|
|
|
|
// constructor functions New and NewWithMetricFamilies only ever set one
|
|
|
|
// of them.
|
2020-04-25 00:42:49 +03:00
|
|
|
r io.Reader
|
|
|
|
mfs []*dto.MetricFamily
|
2020-04-23 09:09:29 +03:00
|
|
|
|
2023-10-10 12:38:10 +03:00
|
|
|
customValidations []Validation
|
2020-04-23 09:09:29 +03:00
|
|
|
}
|
|
|
|
|
2020-04-25 00:42:49 +03:00
|
|
|
// New creates a new Linter that reads an input stream of Prometheus metrics in
|
|
|
|
// the Prometheus text exposition format.
|
2020-04-23 09:09:29 +03:00
|
|
|
func New(r io.Reader) *Linter {
|
|
|
|
return &Linter{
|
|
|
|
r: r,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-25 00:42:49 +03:00
|
|
|
// NewWithMetricFamilies creates a new Linter that reads from a slice of
|
|
|
|
// MetricFamily protobuf messages.
|
|
|
|
func NewWithMetricFamilies(mfs []*dto.MetricFamily) *Linter {
|
|
|
|
return &Linter{
|
|
|
|
mfs: mfs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-10 12:38:10 +03:00
|
|
|
// AddCustomValidations adds custom validations to the linter.
|
|
|
|
func (l *Linter) AddCustomValidations(vs ...Validation) {
|
|
|
|
if l.customValidations == nil {
|
|
|
|
l.customValidations = make([]Validation, 0, len(vs))
|
|
|
|
}
|
|
|
|
l.customValidations = append(l.customValidations, vs...)
|
|
|
|
}
|
|
|
|
|
2020-04-23 09:09:29 +03:00
|
|
|
// Lint performs a linting pass, returning a slice of Problems indicating any
|
2020-04-25 00:42:49 +03:00
|
|
|
// issues found in the metrics stream. The slice is sorted by metric name
|
2020-04-23 09:09:29 +03:00
|
|
|
// and issue description.
|
|
|
|
func (l *Linter) Lint() ([]Problem, error) {
|
|
|
|
var problems []Problem
|
|
|
|
|
2020-04-25 00:42:49 +03:00
|
|
|
if l.r != nil {
|
|
|
|
d := expfmt.NewDecoder(l.r, expfmt.FmtText)
|
|
|
|
|
|
|
|
mf := &dto.MetricFamily{}
|
|
|
|
for {
|
|
|
|
if err := d.Decode(mf); err != nil {
|
2022-08-03 07:30:51 +03:00
|
|
|
if errors.Is(err, io.EOF) {
|
2020-04-25 00:42:49 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, err
|
2020-04-23 09:09:29 +03:00
|
|
|
}
|
|
|
|
|
2023-10-10 12:38:10 +03:00
|
|
|
problems = append(problems, l.lint(mf)...)
|
2020-04-23 09:09:29 +03:00
|
|
|
}
|
2020-04-25 00:42:49 +03:00
|
|
|
}
|
|
|
|
for _, mf := range l.mfs {
|
2023-10-10 12:38:10 +03:00
|
|
|
problems = append(problems, l.lint(mf)...)
|
2020-04-23 09:09:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure deterministic output.
|
|
|
|
sort.SliceStable(problems, func(i, j int) bool {
|
|
|
|
if problems[i].Metric == problems[j].Metric {
|
|
|
|
return problems[i].Text < problems[j].Text
|
|
|
|
}
|
|
|
|
return problems[i].Metric < problems[j].Metric
|
|
|
|
})
|
|
|
|
|
|
|
|
return problems, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// lint is the entry point for linting a single metric.
|
2023-10-10 12:38:10 +03:00
|
|
|
func (l *Linter) lint(mf *dto.MetricFamily) []Problem {
|
2020-04-23 09:10:45 +03:00
|
|
|
var problems []Problem
|
2020-04-23 09:09:29 +03:00
|
|
|
|
2023-10-10 12:38:10 +03:00
|
|
|
for _, fn := range defaultValidations {
|
|
|
|
errs := fn(mf)
|
|
|
|
for _, err := range errs {
|
|
|
|
problems = append(problems, newProblem(mf, err.Error()))
|
2020-04-23 09:09:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-10 12:38:10 +03:00
|
|
|
if l.customValidations != nil {
|
|
|
|
for _, fn := range l.customValidations {
|
|
|
|
errs := fn(mf)
|
|
|
|
for _, err := range errs {
|
|
|
|
problems = append(problems, newProblem(mf, err.Error()))
|
2020-04-23 09:09:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-10 12:38:10 +03:00
|
|
|
// TODO(mdlayher): lint rules for specific metrics types.
|
2020-04-23 09:09:29 +03:00
|
|
|
return problems
|
|
|
|
}
|