Debian Codegraph @

prometheus/testutil/promlint/validations/generic_name_validations.go

raw
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 validations
15
16 import (
17 "errors"
18 "fmt"
19 "regexp"
20 "strings"
21
22 dto "github.com/prometheus/client_model/go"
23 )
24
25 var camelCase = regexp.MustCompile(`[a-z][A-Z]`)
26
27 // LintMetricUnits detects issues with metric unit names.
28 func LintMetricUnits(mf *dto.MetricFamily) []error {
29 var problems []error
30
31 unit, base, ok := metricUnits(*mf.Name)
32 if !ok {
33 // No known units detected.
34 return nil
35 }
36
37 // Unit is already a base unit.
38 if unit == base {
39 return nil
40 }
41
42 problems = append(problems, fmt.Errorf("use base unit %q instead of %q", base, unit))
43
44 return problems
45 }
46
47 // LintMetricTypeInName detects when the metric type is included in the metric name.
48 func LintMetricTypeInName(mf *dto.MetricFamily) []error {
49 if mf.GetType() == dto.MetricType_UNTYPED {
50 return nil
51 }
52
53 var problems []error
54
55 n := strings.ToLower(mf.GetName())
56 typename := strings.ToLower(mf.GetType().String())
57
58 if strings.Contains(n, "_"+typename+"_") || strings.HasSuffix(n, "_"+typename) {
59 problems = append(problems, fmt.Errorf(`metric name should not include type '%s'`, typename))
60 }
61
62 return problems
63 }
64
65 // LintReservedChars detects colons in metric names.
66 func LintReservedChars(mf *dto.MetricFamily) []error {
67 var problems []error
68 if strings.Contains(mf.GetName(), ":") {
69 problems = append(problems, errors.New("metric names should not contain ':'"))
70 }
71 return problems
72 }
73
74 // LintCamelCase detects metric names and label names written in camelCase.
75 func LintCamelCase(mf *dto.MetricFamily) []error {
76 var problems []error
77 if camelCase.FindString(mf.GetName()) != "" {
78 problems = append(problems, errors.New("metric names should be written in 'snake_case' not 'camelCase'"))
79 }
80
81 for _, m := range mf.GetMetric() {
82 for _, l := range m.GetLabel() {
83 if camelCase.FindString(l.GetName()) != "" {
84 problems = append(problems, errors.New("label names should be written in 'snake_case' not 'camelCase'"))
85 }
86 }
87 }
88 return problems
89 }
90
91 // LintUnitAbbreviations detects abbreviated units in the metric name.
92 func LintUnitAbbreviations(mf *dto.MetricFamily) []error {
93 var problems []error
94 n := strings.ToLower(mf.GetName())
95 for _, s := range unitAbbreviations {
96 if strings.Contains(n, "_"+s+"_") || strings.HasSuffix(n, "_"+s) {
97 problems = append(problems, errors.New("metric names should not contain abbreviated units"))
98 }
99 }
100 return problems
101 }