Debian Codegraph @

prometheus/testutil/promlint/validations/histogram_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 "strings"
19
20 dto "github.com/prometheus/client_model/go"
21 )
22
23 // LintHistogramSummaryReserved detects when other types of metrics use names or labels
24 // reserved for use by histograms and/or summaries.
25 func LintHistogramSummaryReserved(mf *dto.MetricFamily) []error {
26 // These rules do not apply to untyped metrics.
27 t := mf.GetType()
28 if t == dto.MetricType_UNTYPED {
29 return nil
30 }
31
32 var problems []error
33
34 isHistogram := t == dto.MetricType_HISTOGRAM
35 isSummary := t == dto.MetricType_SUMMARY
36
37 n := mf.GetName()
38
39 if !isHistogram && strings.HasSuffix(n, "_bucket") {
40 problems = append(problems, errors.New(`non-histogram metrics should not have "_bucket" suffix`))
41 }
42 if !isHistogram && !isSummary && strings.HasSuffix(n, "_count") {
43 problems = append(problems, errors.New(`non-histogram and non-summary metrics should not have "_count" suffix`))
44 }
45 if !isHistogram && !isSummary && strings.HasSuffix(n, "_sum") {
46 problems = append(problems, errors.New(`non-histogram and non-summary metrics should not have "_sum" suffix`))
47 }
48
49 for _, m := range mf.GetMetric() {
50 for _, l := range m.GetLabel() {
51 ln := l.GetName()
52
53 if !isHistogram && ln == "le" {
54 problems = append(problems, errors.New(`non-histogram metrics should not have "le" label`))
55 }
56 if !isSummary && ln == "quantile" {
57 problems = append(problems, errors.New(`non-summary metrics should not have "quantile" label`))
58 }
59 }
60 }
61
62 return problems
63 }