1 | // Copyright 2018 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 prometheus
|
---|
15 |
|
---|
16 | import (
|
---|
17 | "errors"
|
---|
18 | "fmt"
|
---|
19 | "strings"
|
---|
20 | "unicode/utf8"
|
---|
21 |
|
---|
22 | "github.com/prometheus/common/model"
|
---|
23 | )
|
---|
24 |
|
---|
25 | // Labels represents a collection of label name -> value mappings. This type is
|
---|
26 | // commonly used with the With(Labels) and GetMetricWith(Labels) methods of
|
---|
27 | // metric vector Collectors, e.g.:
|
---|
28 | //
|
---|
29 | // myVec.With(Labels{"code": "404", "method": "GET"}).Add(42)
|
---|
30 | //
|
---|
31 | // The other use-case is the specification of constant label pairs in Opts or to
|
---|
32 | // create a Desc.
|
---|
33 | type Labels map[string]string
|
---|
34 |
|
---|
35 | // reservedLabelPrefix is a prefix which is not legal in user-supplied
|
---|
36 | // label names.
|
---|
37 | const reservedLabelPrefix = "__"
|
---|
38 |
|
---|
39 | var errInconsistentCardinality = errors.New("inconsistent label cardinality")
|
---|
40 |
|
---|
41 | func makeInconsistentCardinalityError(fqName string, labels, labelValues []string) error {
|
---|
42 | return fmt.Errorf(
|
---|
43 | "%w: %q has %d variable labels named %q but %d values %q were provided",
|
---|
44 | errInconsistentCardinality, fqName,
|
---|
45 | len(labels), labels,
|
---|
46 | len(labelValues), labelValues,
|
---|
47 | )
|
---|
48 | }
|
---|
49 |
|
---|
50 | func validateValuesInLabels(labels Labels, expectedNumberOfValues int) error {
|
---|
51 | if len(labels) != expectedNumberOfValues {
|
---|
52 | return fmt.Errorf(
|
---|
53 | "%w: expected %d label values but got %d in %#v",
|
---|
54 | errInconsistentCardinality, expectedNumberOfValues,
|
---|
55 | len(labels), labels,
|
---|
56 | )
|
---|
57 | }
|
---|
58 |
|
---|
59 | for name, val := range labels {
|
---|
60 | if !utf8.ValidString(val) {
|
---|
61 | return fmt.Errorf("label %s: value %q is not valid UTF-8", name, val)
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | return nil
|
---|
66 | }
|
---|
67 |
|
---|
68 | func validateLabelValues(vals []string, expectedNumberOfValues int) error {
|
---|
69 | if len(vals) != expectedNumberOfValues {
|
---|
70 | return fmt.Errorf(
|
---|
71 | "%w: expected %d label values but got %d in %#v",
|
---|
72 | errInconsistentCardinality, expectedNumberOfValues,
|
---|
73 | len(vals), vals,
|
---|
74 | )
|
---|
75 | }
|
---|
76 |
|
---|
77 | for _, val := range vals {
|
---|
78 | if !utf8.ValidString(val) {
|
---|
79 | return fmt.Errorf("label value %q is not valid UTF-8", val)
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | return nil
|
---|
84 | }
|
---|
85 |
|
---|
86 | func checkLabelName(l string) bool {
|
---|
87 | return model.LabelName(l).IsValid() && !strings.HasPrefix(l, reservedLabelPrefix)
|
---|
88 | }
|
---|