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 (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"regexp"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/prometheus/common/expfmt"
|
2020-04-23 09:10:45 +03:00
|
|
|
|
|
|
|
dto "github.com/prometheus/client_model/go"
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// A Problem is an issue detected by a Linter.
|
|
|
|
type Problem struct {
|
|
|
|
// The name of the metric indicated by this Problem.
|
|
|
|
Metric string
|
|
|
|
|
|
|
|
// A description of the issue for this Problem.
|
|
|
|
Text string
|
|
|
|
}
|
|
|
|
|
2020-04-23 09:10:45 +03:00
|
|
|
// newProblem is helper function to create a Problem.
|
2020-04-25 00:42:49 +03:00
|
|
|
func newProblem(mf *dto.MetricFamily, text string) Problem {
|
2020-04-23 09:10:45 +03:00
|
|
|
return Problem{
|
2020-04-23 09:09:29 +03:00
|
|
|
Metric: mf.GetName(),
|
|
|
|
Text: text,
|
2020-04-23 09:10:45 +03:00
|
|
|
}
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, err
|
2020-04-23 09:09:29 +03:00
|
|
|
}
|
|
|
|
|
2020-04-25 00:42:49 +03:00
|
|
|
problems = append(problems, lint(mf)...)
|
2020-04-23 09:09:29 +03:00
|
|
|
}
|
2020-04-25 00:42:49 +03:00
|
|
|
}
|
|
|
|
for _, mf := range l.mfs {
|
2020-04-23 09:09:29 +03:00
|
|
|
problems = append(problems, lint(mf)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2020-04-25 00:42:49 +03:00
|
|
|
func lint(mf *dto.MetricFamily) []Problem {
|
|
|
|
fns := []func(mf *dto.MetricFamily) []Problem{
|
2020-04-23 09:09:29 +03:00
|
|
|
lintHelp,
|
|
|
|
lintMetricUnits,
|
|
|
|
lintCounter,
|
|
|
|
lintHistogramSummaryReserved,
|
|
|
|
lintMetricTypeInName,
|
|
|
|
lintReservedChars,
|
|
|
|
lintCamelCase,
|
|
|
|
lintUnitAbbreviations,
|
|
|
|
}
|
|
|
|
|
|
|
|
var problems []Problem
|
|
|
|
for _, fn := range fns {
|
|
|
|
problems = append(problems, fn(mf)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(mdlayher): lint rules for specific metrics types.
|
|
|
|
return problems
|
|
|
|
}
|
|
|
|
|
|
|
|
// lintHelp detects issues related to the help text for a metric.
|
2020-04-25 00:42:49 +03:00
|
|
|
func lintHelp(mf *dto.MetricFamily) []Problem {
|
2020-04-23 09:10:45 +03:00
|
|
|
var problems []Problem
|
2020-04-23 09:09:29 +03:00
|
|
|
|
|
|
|
// Expect all metrics to have help text available.
|
|
|
|
if mf.Help == nil {
|
2020-04-23 09:10:45 +03:00
|
|
|
problems = append(problems, newProblem(mf, "no help text"))
|
2020-04-23 09:09:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return problems
|
|
|
|
}
|
|
|
|
|
|
|
|
// lintMetricUnits detects issues with metric unit names.
|
2020-04-25 00:42:49 +03:00
|
|
|
func lintMetricUnits(mf *dto.MetricFamily) []Problem {
|
2020-04-23 09:10:45 +03:00
|
|
|
var problems []Problem
|
2020-04-23 09:09:29 +03:00
|
|
|
|
|
|
|
unit, base, ok := metricUnits(*mf.Name)
|
|
|
|
if !ok {
|
|
|
|
// No known units detected.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unit is already a base unit.
|
|
|
|
if unit == base {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-23 09:10:45 +03:00
|
|
|
problems = append(problems, newProblem(mf, fmt.Sprintf("use base unit %q instead of %q", base, unit)))
|
2020-04-23 09:09:29 +03:00
|
|
|
|
|
|
|
return problems
|
|
|
|
}
|
|
|
|
|
|
|
|
// lintCounter detects issues specific to counters, as well as patterns that should
|
|
|
|
// only be used with counters.
|
2020-04-25 00:42:49 +03:00
|
|
|
func lintCounter(mf *dto.MetricFamily) []Problem {
|
2020-04-23 09:10:45 +03:00
|
|
|
var problems []Problem
|
2020-04-23 09:09:29 +03:00
|
|
|
|
|
|
|
isCounter := mf.GetType() == dto.MetricType_COUNTER
|
|
|
|
isUntyped := mf.GetType() == dto.MetricType_UNTYPED
|
|
|
|
hasTotalSuffix := strings.HasSuffix(mf.GetName(), "_total")
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case isCounter && !hasTotalSuffix:
|
2020-04-23 09:10:45 +03:00
|
|
|
problems = append(problems, newProblem(mf, `counter metrics should have "_total" suffix`))
|
2020-04-23 09:09:29 +03:00
|
|
|
case !isUntyped && !isCounter && hasTotalSuffix:
|
2020-04-23 09:10:45 +03:00
|
|
|
problems = append(problems, newProblem(mf, `non-counter metrics should not have "_total" suffix`))
|
2020-04-23 09:09:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return problems
|
|
|
|
}
|
|
|
|
|
|
|
|
// lintHistogramSummaryReserved detects when other types of metrics use names or labels
|
|
|
|
// reserved for use by histograms and/or summaries.
|
2020-04-25 00:42:49 +03:00
|
|
|
func lintHistogramSummaryReserved(mf *dto.MetricFamily) []Problem {
|
2020-04-23 09:09:29 +03:00
|
|
|
// These rules do not apply to untyped metrics.
|
|
|
|
t := mf.GetType()
|
|
|
|
if t == dto.MetricType_UNTYPED {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-23 09:10:45 +03:00
|
|
|
var problems []Problem
|
2020-04-23 09:09:29 +03:00
|
|
|
|
|
|
|
isHistogram := t == dto.MetricType_HISTOGRAM
|
|
|
|
isSummary := t == dto.MetricType_SUMMARY
|
|
|
|
|
|
|
|
n := mf.GetName()
|
|
|
|
|
|
|
|
if !isHistogram && strings.HasSuffix(n, "_bucket") {
|
2020-04-23 09:10:45 +03:00
|
|
|
problems = append(problems, newProblem(mf, `non-histogram metrics should not have "_bucket" suffix`))
|
2020-04-23 09:09:29 +03:00
|
|
|
}
|
|
|
|
if !isHistogram && !isSummary && strings.HasSuffix(n, "_count") {
|
2020-04-23 09:10:45 +03:00
|
|
|
problems = append(problems, newProblem(mf, `non-histogram and non-summary metrics should not have "_count" suffix`))
|
2020-04-23 09:09:29 +03:00
|
|
|
}
|
|
|
|
if !isHistogram && !isSummary && strings.HasSuffix(n, "_sum") {
|
2020-04-23 09:10:45 +03:00
|
|
|
problems = append(problems, newProblem(mf, `non-histogram and non-summary metrics should not have "_sum" suffix`))
|
2020-04-23 09:09:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range mf.GetMetric() {
|
|
|
|
for _, l := range m.GetLabel() {
|
|
|
|
ln := l.GetName()
|
|
|
|
|
|
|
|
if !isHistogram && ln == "le" {
|
2020-04-23 09:10:45 +03:00
|
|
|
problems = append(problems, newProblem(mf, `non-histogram metrics should not have "le" label`))
|
2020-04-23 09:09:29 +03:00
|
|
|
}
|
|
|
|
if !isSummary && ln == "quantile" {
|
2020-04-23 09:10:45 +03:00
|
|
|
problems = append(problems, newProblem(mf, `non-summary metrics should not have "quantile" label`))
|
2020-04-23 09:09:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return problems
|
|
|
|
}
|
|
|
|
|
|
|
|
// lintMetricTypeInName detects when metric types are included in the metric name.
|
2020-04-25 00:42:49 +03:00
|
|
|
func lintMetricTypeInName(mf *dto.MetricFamily) []Problem {
|
2020-04-23 09:10:45 +03:00
|
|
|
var problems []Problem
|
2020-04-23 09:09:29 +03:00
|
|
|
n := strings.ToLower(mf.GetName())
|
|
|
|
|
|
|
|
for i, t := range dto.MetricType_name {
|
|
|
|
if i == int32(dto.MetricType_UNTYPED) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
typename := strings.ToLower(t)
|
|
|
|
if strings.Contains(n, "_"+typename+"_") || strings.HasSuffix(n, "_"+typename) {
|
2020-04-23 09:10:45 +03:00
|
|
|
problems = append(problems, newProblem(mf, fmt.Sprintf(`metric name should not include type '%s'`, typename)))
|
2020-04-23 09:09:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return problems
|
|
|
|
}
|
|
|
|
|
|
|
|
// lintReservedChars detects colons in metric names.
|
2020-04-25 00:42:49 +03:00
|
|
|
func lintReservedChars(mf *dto.MetricFamily) []Problem {
|
2020-04-23 09:10:45 +03:00
|
|
|
var problems []Problem
|
2020-04-23 09:09:29 +03:00
|
|
|
if strings.Contains(mf.GetName(), ":") {
|
2020-04-23 09:10:45 +03:00
|
|
|
problems = append(problems, newProblem(mf, "metric names should not contain ':'"))
|
2020-04-23 09:09:29 +03:00
|
|
|
}
|
|
|
|
return problems
|
|
|
|
}
|
|
|
|
|
|
|
|
var camelCase = regexp.MustCompile(`[a-z][A-Z]`)
|
|
|
|
|
|
|
|
// lintCamelCase detects metric names and label names written in camelCase.
|
2020-04-25 00:42:49 +03:00
|
|
|
func lintCamelCase(mf *dto.MetricFamily) []Problem {
|
2020-04-23 09:10:45 +03:00
|
|
|
var problems []Problem
|
2020-04-23 09:09:29 +03:00
|
|
|
if camelCase.FindString(mf.GetName()) != "" {
|
2020-04-23 09:10:45 +03:00
|
|
|
problems = append(problems, newProblem(mf, "metric names should be written in 'snake_case' not 'camelCase'"))
|
2020-04-23 09:09:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range mf.GetMetric() {
|
|
|
|
for _, l := range m.GetLabel() {
|
|
|
|
if camelCase.FindString(l.GetName()) != "" {
|
2020-04-23 09:10:45 +03:00
|
|
|
problems = append(problems, newProblem(mf, "label names should be written in 'snake_case' not 'camelCase'"))
|
2020-04-23 09:09:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return problems
|
|
|
|
}
|
|
|
|
|
|
|
|
// lintUnitAbbreviations detects abbreviated units in the metric name.
|
2020-04-25 00:42:49 +03:00
|
|
|
func lintUnitAbbreviations(mf *dto.MetricFamily) []Problem {
|
2020-04-23 09:10:45 +03:00
|
|
|
var problems []Problem
|
2020-04-23 09:09:29 +03:00
|
|
|
n := strings.ToLower(mf.GetName())
|
|
|
|
for _, s := range unitAbbreviations {
|
|
|
|
if strings.Contains(n, "_"+s+"_") || strings.HasSuffix(n, "_"+s) {
|
2020-04-23 09:10:45 +03:00
|
|
|
problems = append(problems, newProblem(mf, "metric names should not contain abbreviated units"))
|
2020-04-23 09:09:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return problems
|
|
|
|
}
|
|
|
|
|
|
|
|
// metricUnits attempts to detect known unit types used as part of a metric name,
|
|
|
|
// e.g. "foo_bytes_total" or "bar_baz_milligrams".
|
|
|
|
func metricUnits(m string) (unit string, base string, ok bool) {
|
|
|
|
ss := strings.Split(m, "_")
|
|
|
|
|
|
|
|
for unit, base := range units {
|
|
|
|
// Also check for "no prefix".
|
|
|
|
for _, p := range append(unitPrefixes, "") {
|
|
|
|
for _, s := range ss {
|
|
|
|
// Attempt to explicitly match a known unit with a known prefix,
|
|
|
|
// as some words may look like "units" when matching suffix.
|
|
|
|
//
|
|
|
|
// As an example, "thermometers" should not match "meters", but
|
|
|
|
// "kilometers" should.
|
|
|
|
if s == p+unit {
|
|
|
|
return p + unit, base, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Units and their possible prefixes recognized by this library. More can be
|
|
|
|
// added over time as needed.
|
|
|
|
var (
|
|
|
|
// map a unit to the appropriate base unit.
|
|
|
|
units = map[string]string{
|
|
|
|
// Base units.
|
|
|
|
"amperes": "amperes",
|
|
|
|
"bytes": "bytes",
|
2020-05-28 18:05:16 +03:00
|
|
|
"celsius": "celsius", // Also allow Celsius because it is common in typical Prometheus use cases.
|
2020-04-23 09:09:29 +03:00
|
|
|
"grams": "grams",
|
|
|
|
"joules": "joules",
|
2020-05-28 18:05:16 +03:00
|
|
|
"kelvin": "kelvin", // SI base unit, used in special cases (e.g. color temperature, scientific measurements).
|
2020-04-23 09:09:29 +03:00
|
|
|
"meters": "meters", // Both American and international spelling permitted.
|
|
|
|
"metres": "metres",
|
|
|
|
"seconds": "seconds",
|
|
|
|
"volts": "volts",
|
|
|
|
|
|
|
|
// Non base units.
|
|
|
|
// Time.
|
|
|
|
"minutes": "seconds",
|
|
|
|
"hours": "seconds",
|
|
|
|
"days": "seconds",
|
|
|
|
"weeks": "seconds",
|
|
|
|
// Temperature.
|
2020-05-28 18:05:16 +03:00
|
|
|
"kelvins": "kelvin",
|
2020-04-23 09:09:29 +03:00
|
|
|
"fahrenheit": "celsius",
|
|
|
|
"rankine": "celsius",
|
|
|
|
// Length.
|
|
|
|
"inches": "meters",
|
|
|
|
"yards": "meters",
|
|
|
|
"miles": "meters",
|
|
|
|
// Bytes.
|
|
|
|
"bits": "bytes",
|
|
|
|
// Energy.
|
|
|
|
"calories": "joules",
|
|
|
|
// Mass.
|
|
|
|
"pounds": "grams",
|
|
|
|
"ounces": "grams",
|
|
|
|
}
|
|
|
|
|
|
|
|
unitPrefixes = []string{
|
|
|
|
"pico",
|
|
|
|
"nano",
|
|
|
|
"micro",
|
|
|
|
"milli",
|
|
|
|
"centi",
|
|
|
|
"deci",
|
|
|
|
"deca",
|
|
|
|
"hecto",
|
|
|
|
"kilo",
|
|
|
|
"kibi",
|
|
|
|
"mega",
|
|
|
|
"mibi",
|
|
|
|
"giga",
|
|
|
|
"gibi",
|
|
|
|
"tera",
|
|
|
|
"tebi",
|
|
|
|
"peta",
|
|
|
|
"pebi",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Common abbreviations that we'd like to discourage.
|
|
|
|
unitAbbreviations = []string{
|
|
|
|
"s",
|
|
|
|
"ms",
|
|
|
|
"us",
|
|
|
|
"ns",
|
|
|
|
"sec",
|
|
|
|
"b",
|
|
|
|
"kb",
|
|
|
|
"mb",
|
|
|
|
"gb",
|
|
|
|
"tb",
|
|
|
|
"pb",
|
|
|
|
"m",
|
|
|
|
"h",
|
|
|
|
"d",
|
|
|
|
}
|
|
|
|
)
|