2017-04-19 12:43:09 +00:00
|
|
|
// Copyright 2017 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-12-24 13:01:10 +00:00
|
|
|
package labels
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-12-29 08:27:30 +00:00
|
|
|
"encoding/json"
|
2016-12-24 13:01:10 +00:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
|
2020-10-15 10:31:28 +00:00
|
|
|
"github.com/cespare/xxhash/v2"
|
2016-12-24 13:01:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Well-known label names used by Prometheus components.
|
|
|
|
const (
|
2017-04-04 12:44:39 +00:00
|
|
|
MetricName = "__name__"
|
|
|
|
AlertName = "alertname"
|
|
|
|
BucketLabel = "le"
|
|
|
|
InstanceName = "instance"
|
2020-05-12 21:03:15 +00:00
|
|
|
|
|
|
|
labelSep = '\xfe'
|
2016-12-24 13:01:10 +00:00
|
|
|
)
|
|
|
|
|
2020-10-15 10:31:28 +00:00
|
|
|
var seps = []byte{'\xff'}
|
|
|
|
|
2016-12-24 13:01:10 +00:00
|
|
|
// Label is a key/value pair of strings.
|
|
|
|
type Label struct {
|
|
|
|
Name, Value string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Labels is a sorted set of labels. Order has to be guaranteed upon
|
|
|
|
// instantiation.
|
|
|
|
type Labels []Label
|
|
|
|
|
|
|
|
func (ls Labels) Len() int { return len(ls) }
|
|
|
|
func (ls Labels) Swap(i, j int) { ls[i], ls[j] = ls[j], ls[i] }
|
|
|
|
func (ls Labels) Less(i, j int) bool { return ls[i].Name < ls[j].Name }
|
|
|
|
|
|
|
|
func (ls Labels) String() string {
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
|
|
|
b.WriteByte('{')
|
|
|
|
for i, l := range ls {
|
|
|
|
if i > 0 {
|
|
|
|
b.WriteByte(',')
|
2016-12-29 16:31:14 +00:00
|
|
|
b.WriteByte(' ')
|
2016-12-24 13:01:10 +00:00
|
|
|
}
|
|
|
|
b.WriteString(l.Name)
|
|
|
|
b.WriteByte('=')
|
|
|
|
b.WriteString(strconv.Quote(l.Value))
|
|
|
|
}
|
|
|
|
b.WriteByte('}')
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
2020-05-12 21:03:15 +00:00
|
|
|
// Bytes returns ls as a byte slice.
|
|
|
|
// It uses an byte invalid character as a separator and so should not be used for printing.
|
|
|
|
func (ls Labels) Bytes(buf []byte) []byte {
|
|
|
|
b := bytes.NewBuffer(buf[:0])
|
|
|
|
b.WriteByte(labelSep)
|
|
|
|
for i, l := range ls {
|
|
|
|
if i > 0 {
|
2020-10-15 10:31:28 +00:00
|
|
|
b.WriteByte(seps[0])
|
2020-05-12 21:03:15 +00:00
|
|
|
}
|
|
|
|
b.WriteString(l.Name)
|
2020-10-15 10:31:28 +00:00
|
|
|
b.WriteByte(seps[0])
|
2020-05-12 21:03:15 +00:00
|
|
|
b.WriteString(l.Value)
|
|
|
|
}
|
|
|
|
return b.Bytes()
|
|
|
|
}
|
|
|
|
|
2017-10-23 13:57:30 +00:00
|
|
|
// MarshalJSON implements json.Marshaler.
|
2016-12-29 08:27:30 +00:00
|
|
|
func (ls Labels) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(ls.Map())
|
|
|
|
}
|
|
|
|
|
2017-10-23 13:57:30 +00:00
|
|
|
// UnmarshalJSON implements json.Unmarshaler.
|
2016-12-29 15:53:11 +00:00
|
|
|
func (ls *Labels) UnmarshalJSON(b []byte) error {
|
|
|
|
var m map[string]string
|
|
|
|
|
|
|
|
if err := json.Unmarshal(b, &m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*ls = FromMap(m)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-08 16:29:25 +00:00
|
|
|
// MarshalYAML implements yaml.Marshaler.
|
|
|
|
func (ls Labels) MarshalYAML() (interface{}, error) {
|
|
|
|
return ls.Map(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML implements yaml.Unmarshaler.
|
|
|
|
func (ls *Labels) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
var m map[string]string
|
|
|
|
|
|
|
|
if err := unmarshal(&m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*ls = FromMap(m)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-09 09:17:52 +00:00
|
|
|
// MatchLabels returns a subset of Labels that matches/does not match with the provided label names based on the 'on' boolean.
|
|
|
|
// If on is set to true, it returns the subset of labels that match with the provided label names and its inverse when 'on' is set to false.
|
|
|
|
func (ls Labels) MatchLabels(on bool, names ...string) Labels {
|
|
|
|
matchedLabels := Labels{}
|
|
|
|
|
2022-05-20 08:18:31 +00:00
|
|
|
nameSet := make(map[string]struct{}, len(names))
|
2019-02-09 09:17:52 +00:00
|
|
|
for _, n := range names {
|
|
|
|
nameSet[n] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range ls {
|
2019-12-27 09:32:19 +00:00
|
|
|
if _, ok := nameSet[v.Name]; on == ok && (on || v.Name != MetricName) {
|
2019-02-09 09:17:52 +00:00
|
|
|
matchedLabels = append(matchedLabels, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return matchedLabels
|
|
|
|
}
|
|
|
|
|
2016-12-24 13:01:10 +00:00
|
|
|
// Hash returns a hash value for the label set.
|
|
|
|
func (ls Labels) Hash() uint64 {
|
2020-10-15 10:31:28 +00:00
|
|
|
// Use xxhash.Sum64(b) for fast path as it's faster.
|
2016-12-24 13:01:10 +00:00
|
|
|
b := make([]byte, 0, 1024)
|
2020-10-15 10:31:28 +00:00
|
|
|
for i, v := range ls {
|
|
|
|
if len(b)+len(v.Name)+len(v.Value)+2 >= cap(b) {
|
|
|
|
// If labels entry is 1KB+ do not allocate whole entry.
|
|
|
|
h := xxhash.New()
|
|
|
|
_, _ = h.Write(b)
|
|
|
|
for _, v := range ls[i:] {
|
|
|
|
_, _ = h.WriteString(v.Name)
|
|
|
|
_, _ = h.Write(seps)
|
|
|
|
_, _ = h.WriteString(v.Value)
|
|
|
|
_, _ = h.Write(seps)
|
|
|
|
}
|
|
|
|
return h.Sum64()
|
|
|
|
}
|
2016-12-24 13:01:10 +00:00
|
|
|
|
|
|
|
b = append(b, v.Name...)
|
2020-10-15 10:31:28 +00:00
|
|
|
b = append(b, seps[0])
|
2016-12-24 13:01:10 +00:00
|
|
|
b = append(b, v.Value...)
|
2020-10-15 10:31:28 +00:00
|
|
|
b = append(b, seps[0])
|
2016-12-24 13:01:10 +00:00
|
|
|
}
|
|
|
|
return xxhash.Sum64(b)
|
|
|
|
}
|
|
|
|
|
2018-07-18 03:56:27 +00:00
|
|
|
// HashForLabels returns a hash value for the labels matching the provided names.
|
2019-06-28 12:52:51 +00:00
|
|
|
// 'names' have to be sorted in ascending order.
|
|
|
|
func (ls Labels) HashForLabels(b []byte, names ...string) (uint64, []byte) {
|
|
|
|
b = b[:0]
|
|
|
|
i, j := 0, 0
|
|
|
|
for i < len(ls) && j < len(names) {
|
|
|
|
if names[j] < ls[i].Name {
|
|
|
|
j++
|
|
|
|
} else if ls[i].Name < names[j] {
|
|
|
|
i++
|
|
|
|
} else {
|
|
|
|
b = append(b, ls[i].Name...)
|
2020-10-15 10:31:28 +00:00
|
|
|
b = append(b, seps[0])
|
2019-06-28 12:52:51 +00:00
|
|
|
b = append(b, ls[i].Value...)
|
2020-10-15 10:31:28 +00:00
|
|
|
b = append(b, seps[0])
|
2019-06-28 12:52:51 +00:00
|
|
|
i++
|
|
|
|
j++
|
2018-07-18 03:56:27 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-28 12:52:51 +00:00
|
|
|
return xxhash.Sum64(b), b
|
2018-07-18 03:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HashWithoutLabels returns a hash value for all labels except those matching
|
|
|
|
// the provided names.
|
2019-06-28 12:52:51 +00:00
|
|
|
// 'names' have to be sorted in ascending order.
|
|
|
|
func (ls Labels) HashWithoutLabels(b []byte, names ...string) (uint64, []byte) {
|
|
|
|
b = b[:0]
|
|
|
|
j := 0
|
|
|
|
for i := range ls {
|
|
|
|
for j < len(names) && names[j] < ls[i].Name {
|
|
|
|
j++
|
2018-07-18 03:56:27 +00:00
|
|
|
}
|
2019-06-28 12:52:51 +00:00
|
|
|
if ls[i].Name == MetricName || (j < len(names) && ls[i].Name == names[j]) {
|
|
|
|
continue
|
2018-07-18 03:56:27 +00:00
|
|
|
}
|
2019-06-28 12:52:51 +00:00
|
|
|
b = append(b, ls[i].Name...)
|
2020-10-15 10:31:28 +00:00
|
|
|
b = append(b, seps[0])
|
2019-06-28 12:52:51 +00:00
|
|
|
b = append(b, ls[i].Value...)
|
2020-10-15 10:31:28 +00:00
|
|
|
b = append(b, seps[0])
|
2018-07-18 03:56:27 +00:00
|
|
|
}
|
2019-06-28 12:52:51 +00:00
|
|
|
return xxhash.Sum64(b), b
|
2018-07-18 03:56:27 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 04:38:27 +00:00
|
|
|
// BytesWithLabels is just as Bytes(), but only for labels matching names.
|
2020-05-12 21:03:15 +00:00
|
|
|
// 'names' have to be sorted in ascending order.
|
2022-06-07 04:38:27 +00:00
|
|
|
func (ls Labels) BytesWithLabels(buf []byte, names ...string) []byte {
|
|
|
|
b := bytes.NewBuffer(buf[:0])
|
|
|
|
b.WriteByte(labelSep)
|
2020-05-12 21:03:15 +00:00
|
|
|
i, j := 0, 0
|
|
|
|
for i < len(ls) && j < len(names) {
|
|
|
|
if names[j] < ls[i].Name {
|
|
|
|
j++
|
|
|
|
} else if ls[i].Name < names[j] {
|
|
|
|
i++
|
|
|
|
} else {
|
2022-06-07 04:38:27 +00:00
|
|
|
if b.Len() > 1 {
|
|
|
|
b.WriteByte(seps[0])
|
|
|
|
}
|
|
|
|
b.WriteString(ls[i].Name)
|
|
|
|
b.WriteByte(seps[0])
|
|
|
|
b.WriteString(ls[i].Value)
|
2020-05-12 21:03:15 +00:00
|
|
|
i++
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
}
|
2022-06-07 04:38:27 +00:00
|
|
|
return b.Bytes()
|
2020-05-12 21:03:15 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 04:38:27 +00:00
|
|
|
// BytesWithoutLabels is just as Bytes(), but only for labels not matching names.
|
2020-05-12 21:03:15 +00:00
|
|
|
// 'names' have to be sorted in ascending order.
|
2022-06-07 04:38:27 +00:00
|
|
|
func (ls Labels) BytesWithoutLabels(buf []byte, names ...string) []byte {
|
|
|
|
b := bytes.NewBuffer(buf[:0])
|
|
|
|
b.WriteByte(labelSep)
|
2020-05-12 21:03:15 +00:00
|
|
|
j := 0
|
|
|
|
for i := range ls {
|
|
|
|
for j < len(names) && names[j] < ls[i].Name {
|
|
|
|
j++
|
|
|
|
}
|
2022-06-07 04:38:27 +00:00
|
|
|
if j < len(names) && ls[i].Name == names[j] {
|
2020-05-12 21:03:15 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-06-07 04:38:27 +00:00
|
|
|
if b.Len() > 1 {
|
|
|
|
b.WriteByte(seps[0])
|
|
|
|
}
|
|
|
|
b.WriteString(ls[i].Name)
|
|
|
|
b.WriteByte(seps[0])
|
|
|
|
b.WriteString(ls[i].Value)
|
2020-05-12 21:03:15 +00:00
|
|
|
}
|
2022-06-07 04:38:27 +00:00
|
|
|
return b.Bytes()
|
2020-05-12 21:03:15 +00:00
|
|
|
}
|
|
|
|
|
2016-12-24 23:37:46 +00:00
|
|
|
// Copy returns a copy of the labels.
|
|
|
|
func (ls Labels) Copy() Labels {
|
|
|
|
res := make(Labels, len(ls))
|
|
|
|
copy(res, ls)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2016-12-24 13:01:10 +00:00
|
|
|
// Get returns the value for the label with the given name.
|
|
|
|
// Returns an empty string if the label doesn't exist.
|
|
|
|
func (ls Labels) Get(name string) string {
|
|
|
|
for _, l := range ls {
|
|
|
|
if l.Name == name {
|
|
|
|
return l.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2018-02-15 14:26:24 +00:00
|
|
|
// Has returns true if the label with the given name is present.
|
2018-02-14 17:03:58 +00:00
|
|
|
func (ls Labels) Has(name string) bool {
|
|
|
|
for _, l := range ls {
|
|
|
|
if l.Name == name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-02-19 06:25:44 +00:00
|
|
|
// HasDuplicateLabelNames returns whether ls has duplicate label names.
|
2020-01-20 11:05:27 +00:00
|
|
|
// It assumes that the labelset is sorted.
|
|
|
|
func (ls Labels) HasDuplicateLabelNames() (string, bool) {
|
|
|
|
for i, l := range ls {
|
|
|
|
if i == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if l.Name == ls[i-1].Name {
|
|
|
|
return l.Name, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2019-11-18 19:53:33 +00:00
|
|
|
// WithoutEmpty returns the labelset without empty labels.
|
|
|
|
// May return the same labelset.
|
|
|
|
func (ls Labels) WithoutEmpty() Labels {
|
|
|
|
for _, v := range ls {
|
|
|
|
if v.Value != "" {
|
|
|
|
continue
|
|
|
|
}
|
2020-10-13 07:57:53 +00:00
|
|
|
// Do not copy the slice until it's necessary.
|
2019-11-18 19:53:33 +00:00
|
|
|
els := make(Labels, 0, len(ls)-1)
|
|
|
|
for _, v := range ls {
|
|
|
|
if v.Value != "" {
|
|
|
|
els = append(els, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return els
|
|
|
|
}
|
|
|
|
return ls
|
|
|
|
}
|
|
|
|
|
2016-12-24 23:37:46 +00:00
|
|
|
// Equal returns whether the two label sets are equal.
|
|
|
|
func Equal(ls, o Labels) bool {
|
2016-12-24 13:01:10 +00:00
|
|
|
if len(ls) != len(o) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i, l := range ls {
|
2022-03-14 23:30:04 +00:00
|
|
|
if l != o[i] {
|
2016-12-24 13:01:10 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map returns a string map of the labels.
|
|
|
|
func (ls Labels) Map() map[string]string {
|
|
|
|
m := make(map[string]string, len(ls))
|
|
|
|
for _, l := range ls {
|
|
|
|
m[l.Name] = l.Value
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2022-05-26 14:58:06 +00:00
|
|
|
// EmptyLabels returns n empty Labels value, for convenience.
|
|
|
|
func EmptyLabels() Labels {
|
|
|
|
return Labels{}
|
|
|
|
}
|
|
|
|
|
2016-12-24 13:01:10 +00:00
|
|
|
// New returns a sorted Labels from the given labels.
|
|
|
|
// The caller has to guarantee that all label names are unique.
|
|
|
|
func New(ls ...Label) Labels {
|
|
|
|
set := make(Labels, 0, len(ls))
|
|
|
|
for _, l := range ls {
|
|
|
|
set = append(set, l)
|
|
|
|
}
|
|
|
|
sort.Sort(set)
|
|
|
|
|
|
|
|
return set
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromMap returns new sorted Labels from the given map.
|
|
|
|
func FromMap(m map[string]string) Labels {
|
|
|
|
l := make([]Label, 0, len(m))
|
|
|
|
for k, v := range m {
|
|
|
|
l = append(l, Label{Name: k, Value: v})
|
|
|
|
}
|
|
|
|
return New(l...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromStrings creates new labels from pairs of strings.
|
|
|
|
func FromStrings(ss ...string) Labels {
|
|
|
|
if len(ss)%2 != 0 {
|
|
|
|
panic("invalid number of strings")
|
|
|
|
}
|
2022-05-25 14:22:47 +00:00
|
|
|
res := make(Labels, 0, len(ss)/2)
|
2016-12-24 13:01:10 +00:00
|
|
|
for i := 0; i < len(ss); i += 2 {
|
|
|
|
res = append(res, Label{Name: ss[i], Value: ss[i+1]})
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(res)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare compares the two label sets.
|
|
|
|
// The result will be 0 if a==b, <0 if a < b, and >0 if a > b.
|
|
|
|
func Compare(a, b Labels) int {
|
|
|
|
l := len(a)
|
|
|
|
if len(b) < l {
|
|
|
|
l = len(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < l; i++ {
|
2020-01-01 15:45:01 +00:00
|
|
|
if a[i].Name != b[i].Name {
|
|
|
|
if a[i].Name < b[i].Name {
|
|
|
|
return -1
|
|
|
|
}
|
2020-03-23 14:47:11 +00:00
|
|
|
return 1
|
2016-12-24 13:01:10 +00:00
|
|
|
}
|
2020-01-01 15:45:01 +00:00
|
|
|
if a[i].Value != b[i].Value {
|
|
|
|
if a[i].Value < b[i].Value {
|
|
|
|
return -1
|
|
|
|
}
|
2020-03-23 14:47:11 +00:00
|
|
|
return 1
|
2016-12-24 13:01:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// If all labels so far were in common, the set with fewer labels comes first.
|
|
|
|
return len(a) - len(b)
|
|
|
|
}
|
|
|
|
|
2019-02-01 14:35:32 +00:00
|
|
|
// Builder allows modifying Labels.
|
2016-12-24 13:35:24 +00:00
|
|
|
type Builder struct {
|
2016-12-24 13:01:10 +00:00
|
|
|
base Labels
|
2016-12-24 13:35:24 +00:00
|
|
|
del []string
|
|
|
|
add []Label
|
|
|
|
}
|
|
|
|
|
2019-08-13 10:19:17 +00:00
|
|
|
// NewBuilder returns a new LabelsBuilder.
|
2016-12-24 13:35:24 +00:00
|
|
|
func NewBuilder(base Labels) *Builder {
|
2019-08-13 10:19:17 +00:00
|
|
|
b := &Builder{
|
|
|
|
del: make([]string, 0, 5),
|
|
|
|
add: make([]Label, 0, 5),
|
2016-12-24 13:35:24 +00:00
|
|
|
}
|
2019-08-13 10:19:17 +00:00
|
|
|
b.Reset(base)
|
|
|
|
return b
|
2016-12-24 13:35:24 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 10:19:17 +00:00
|
|
|
// Reset clears all current state for the builder.
|
2019-06-11 08:24:50 +00:00
|
|
|
func (b *Builder) Reset(base Labels) {
|
|
|
|
b.base = base
|
|
|
|
b.del = b.del[:0]
|
|
|
|
b.add = b.add[:0]
|
2019-08-13 10:19:17 +00:00
|
|
|
for _, l := range b.base {
|
|
|
|
if l.Value == "" {
|
|
|
|
b.del = append(b.del, l.Name)
|
|
|
|
}
|
|
|
|
}
|
2019-06-11 08:24:50 +00:00
|
|
|
}
|
|
|
|
|
2016-12-24 13:35:24 +00:00
|
|
|
// Del deletes the label of the given name.
|
|
|
|
func (b *Builder) Del(ns ...string) *Builder {
|
|
|
|
for _, n := range ns {
|
|
|
|
for i, a := range b.add {
|
|
|
|
if a.Name == n {
|
|
|
|
b.add = append(b.add[:i], b.add[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.del = append(b.del, n)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2022-06-07 04:38:27 +00:00
|
|
|
// Keep removes all labels from the base except those with the given names.
|
|
|
|
func (b *Builder) Keep(ns ...string) *Builder {
|
|
|
|
Outer:
|
|
|
|
for _, l := range b.base {
|
|
|
|
for _, n := range ns {
|
|
|
|
if l.Name == n {
|
|
|
|
continue Outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.del = append(b.del, l.Name)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2016-12-24 13:35:24 +00:00
|
|
|
// Set the name/value pair as a label.
|
|
|
|
func (b *Builder) Set(n, v string) *Builder {
|
2019-08-13 10:19:17 +00:00
|
|
|
if v == "" {
|
|
|
|
// Empty labels are the same as missing labels.
|
|
|
|
return b.Del(n)
|
|
|
|
}
|
2016-12-24 13:35:24 +00:00
|
|
|
for i, a := range b.add {
|
|
|
|
if a.Name == n {
|
|
|
|
b.add[i].Value = v
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.add = append(b.add, Label{Name: n, Value: v})
|
|
|
|
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2022-08-19 09:57:52 +00:00
|
|
|
// Labels returns the labels from the builder, adding them to res if non-nil.
|
|
|
|
// Argument res can be the same as b.base, if caller wants to overwrite that slice.
|
|
|
|
// If no modifications were made, the original labels are returned.
|
|
|
|
func (b *Builder) Labels(res Labels) Labels {
|
2016-12-24 13:35:24 +00:00
|
|
|
if len(b.del) == 0 && len(b.add) == 0 {
|
|
|
|
return b.base
|
|
|
|
}
|
|
|
|
|
2022-08-19 09:57:52 +00:00
|
|
|
if res == nil {
|
|
|
|
// In the general case, labels are removed, modified or moved
|
|
|
|
// rather than added.
|
|
|
|
res = make(Labels, 0, len(b.base))
|
|
|
|
} else {
|
|
|
|
res = res[:0]
|
|
|
|
}
|
2016-12-24 13:35:24 +00:00
|
|
|
Outer:
|
2022-08-19 09:57:52 +00:00
|
|
|
// Justification that res can be the same slice as base: in this loop
|
|
|
|
// we move forward through base, and either skip an element or assign
|
|
|
|
// it to res at its current position or an earlier position.
|
2016-12-24 13:35:24 +00:00
|
|
|
for _, l := range b.base {
|
|
|
|
for _, n := range b.del {
|
|
|
|
if l.Name == n {
|
|
|
|
continue Outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, la := range b.add {
|
|
|
|
if l.Name == la.Name {
|
|
|
|
continue Outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res = append(res, l)
|
|
|
|
}
|
2022-06-07 04:38:27 +00:00
|
|
|
if len(b.add) > 0 { // Base is already in order, so we only need to sort if we add to it.
|
|
|
|
res = append(res, b.add...)
|
|
|
|
sort.Sort(res)
|
|
|
|
}
|
2016-12-24 13:35:24 +00:00
|
|
|
return res
|
2016-12-24 13:01:10 +00:00
|
|
|
}
|