khenaidoo | ffe076b | 2019-01-15 16:08:08 -0500 | [diff] [blame^] | 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 | // myVec.With(Labels{"code": "404", "method": "GET"}).Add(42) |
| 29 | // |
| 30 | // The other use-case is the specification of constant label pairs in Opts or to |
| 31 | // create a Desc. |
| 32 | type Labels map[string]string |
| 33 | |
| 34 | // reservedLabelPrefix is a prefix which is not legal in user-supplied |
| 35 | // label names. |
| 36 | const reservedLabelPrefix = "__" |
| 37 | |
| 38 | var errInconsistentCardinality = errors.New("inconsistent label cardinality") |
| 39 | |
| 40 | func makeInconsistentCardinalityError(fqName string, labels, labelValues []string) error { |
| 41 | return fmt.Errorf( |
| 42 | "%s: %q has %d variable labels named %q but %d values %q were provided", |
| 43 | errInconsistentCardinality, fqName, |
| 44 | len(labels), labels, |
| 45 | len(labelValues), labelValues, |
| 46 | ) |
| 47 | } |
| 48 | |
| 49 | func validateValuesInLabels(labels Labels, expectedNumberOfValues int) error { |
| 50 | if len(labels) != expectedNumberOfValues { |
| 51 | return fmt.Errorf( |
| 52 | "%s: expected %d label values but got %d in %#v", |
| 53 | errInconsistentCardinality, expectedNumberOfValues, |
| 54 | len(labels), labels, |
| 55 | ) |
| 56 | } |
| 57 | |
| 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) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | return nil |
| 65 | } |
| 66 | |
| 67 | func validateLabelValues(vals []string, expectedNumberOfValues int) error { |
| 68 | if len(vals) != expectedNumberOfValues { |
| 69 | return fmt.Errorf( |
| 70 | "%s: expected %d label values but got %d in %#v", |
| 71 | errInconsistentCardinality, expectedNumberOfValues, |
| 72 | len(vals), vals, |
| 73 | ) |
| 74 | } |
| 75 | |
| 76 | for _, val := range vals { |
| 77 | if !utf8.ValidString(val) { |
| 78 | return fmt.Errorf("label value %q is not valid UTF-8", val) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return nil |
| 83 | } |
| 84 | |
| 85 | func checkLabelName(l string) bool { |
| 86 | return model.LabelName(l).IsValid() && !strings.HasPrefix(l, reservedLabelPrefix) |
| 87 | } |