1 // Copyright 2013 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.
23 // A LabelSet is a collection of LabelName and LabelValue pairs. The LabelSet
24 // may be fully-qualified down to the point where it may resolve to a single
25 // Metric in the data store or not. All operations that occur within the realm
26 // of a LabelSet can emit a vector of Metric entities to which the LabelSet may
28 type LabelSet map[LabelName]LabelValue
30 // Validate checks whether all names and values in the label set
32 func (ls LabelSet) Validate() error {
33 for ln, lv := range ls {
35 return fmt.Errorf("invalid name %q", ln)
38 return fmt.Errorf("invalid value %q", lv)
44 // Equal returns true iff both label sets have exactly the same key/value pairs.
45 func (ls LabelSet) Equal(o LabelSet) bool {
46 if len(ls) != len(o) {
49 for ln, lv := range ls {
61 // Before compares the metrics, using the following criteria:
63 // If m has fewer labels than o, it is before o. If it has more, it is not.
65 // If the number of labels is the same, the superset of all label names is
66 // sorted alphanumerically. The first differing label pair found in that order
67 // determines the outcome: If the label does not exist at all in m, then m is
68 // before o, and vice versa. Otherwise the label value is compared
71 // If m and o are equal, the method returns false.
72 func (ls LabelSet) Before(o LabelSet) bool {
80 lns := make(LabelNames, 0, len(ls)+len(o))
87 // It's probably not worth it to de-dup lns.
89 for _, ln := range lns {
108 // Clone returns a copy of the label set.
109 func (ls LabelSet) Clone() LabelSet {
110 lsn := make(LabelSet, len(ls))
111 for ln, lv := range ls {
117 // Merge is a helper function to non-destructively merge two label sets.
118 func (l LabelSet) Merge(other LabelSet) LabelSet {
119 result := make(LabelSet, len(l))
121 for k, v := range l {
125 for k, v := range other {
132 func (l LabelSet) String() string {
133 lstrs := make([]string, 0, len(l))
134 for l, v := range l {
135 lstrs = append(lstrs, fmt.Sprintf("%s=%q", l, v))
139 return fmt.Sprintf("{%s}", strings.Join(lstrs, ", "))
142 // Fingerprint returns the LabelSet's fingerprint.
143 func (ls LabelSet) Fingerprint() Fingerprint {
144 return labelSetToFingerprint(ls)
147 // FastFingerprint returns the LabelSet's Fingerprint calculated by a faster hashing
148 // algorithm, which is, however, more susceptible to hash collisions.
149 func (ls LabelSet) FastFingerprint() Fingerprint {
150 return labelSetToFastFingerprint(ls)
153 // UnmarshalJSON implements the json.Unmarshaler interface.
154 func (l *LabelSet) UnmarshalJSON(b []byte) error {
155 var m map[LabelName]LabelValue
156 if err := json.Unmarshal(b, &m); err != nil {
159 // encoding/json only unmarshals maps of the form map[string]T. It treats
160 // LabelName as a string and does not call its UnmarshalJSON method.
161 // Thus, we have to replicate the behavior here.
163 if !LabelNameRE.MatchString(string(ln)) {
164 return fmt.Errorf("%q is not a valid label name", ln)