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
6 // http://www.apache.org/licenses/LICENSE-2.0
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.
22 "github.com/prometheus/common/model"
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 // myVec.With(Labels{"code": "404", "method": "GET"}).Add(42)
30 // The other use-case is the specification of constant label pairs in Opts or to
32 type Labels map[string]string
34 // reservedLabelPrefix is a prefix which is not legal in user-supplied
36 const reservedLabelPrefix = "__"
38 var errInconsistentCardinality = errors.New("inconsistent label cardinality")
40 func makeInconsistentCardinalityError(fqName string, labels, labelValues []string) error {
42 "%s: %q has %d variable labels named %q but %d values %q were provided",
43 errInconsistentCardinality, fqName,
45 len(labelValues), labelValues,
49 func validateValuesInLabels(labels Labels, expectedNumberOfValues int) error {
50 if len(labels) != expectedNumberOfValues {
52 "%s: expected %d label values but got %d in %#v",
53 errInconsistentCardinality, expectedNumberOfValues,
58 for name, val := range labels {
59 if !utf8.ValidString(val) {
60 return fmt.Errorf("label %s: value %q is not valid UTF-8", name, val)
67 func validateLabelValues(vals []string, expectedNumberOfValues int) error {
68 if len(vals) != expectedNumberOfValues {
70 "%s: expected %d label values but got %d in %#v",
71 errInconsistentCardinality, expectedNumberOfValues,
76 for _, val := range vals {
77 if !utf8.ValidString(val) {
78 return fmt.Errorf("label value %q is not valid UTF-8", val)
85 func checkLabelName(l string) bool {
86 return model.LabelName(l).IsValid() && !strings.HasPrefix(l, reservedLabelPrefix)