func labelValue(ctx context.Context) labelMap {
labels, _ := ctx.Value(labelContextKey{}).(*labelMap)
if labels == nil {
- return labelMap(nil)
+ return labelMap{}
}
return *labels
}
// labelMap is the representation of the label set held in the context type.
// This is an initial implementation, but it will be replaced with something
// that admits incremental immutable modification more efficiently.
-type labelMap map[string]string
+type labelMap struct {
+ LabelSet
+}
// String satisfies Stringer and returns key, value pairs in a consistent
// order.
if l == nil {
return ""
}
- keyVals := make([]string, 0, len(*l))
+ keyVals := make([]string, 0, len(l.list))
- for k, v := range *l {
- keyVals = append(keyVals, fmt.Sprintf("%q:%q", k, v))
+ for _, lbl := range l.list {
+ keyVals = append(keyVals, fmt.Sprintf("%q:%q", lbl.key, lbl.value))
}
slices.Sort(keyVals)
-
return "{" + strings.Join(keyVals, ", ") + "}"
}
// A label overwrites a prior label with the same key.
func WithLabels(ctx context.Context, labels LabelSet) context.Context {
parentLabels := labelValue(ctx)
- childLabels := make(labelMap, len(parentLabels))
- // TODO(matloob): replace the map implementation with something
- // more efficient so creating a child context WithLabels doesn't need
- // to clone the map.
- for k, v := range parentLabels {
- childLabels[k] = v
+ return context.WithValue(ctx, labelContextKey{}, &labelMap{mergeLabelSets(parentLabels.LabelSet, labels)})
+}
+
+func mergeLabelSets(left, right LabelSet) LabelSet {
+ if len(left.list) == 0 {
+ return right
+ } else if len(right.list) == 0 {
+ return left
}
- for _, label := range labels.list {
- childLabels[label.key] = label.value
+
+ l, r := 0, 0
+ result := make([]label, 0, len(right.list))
+ for l < len(left.list) && r < len(right.list) {
+ switch strings.Compare(left.list[l].key, right.list[r].key) {
+ case -1: // left key < right key
+ result = append(result, left.list[l])
+ l++
+ case 1: // right key < left key
+ result = append(result, right.list[r])
+ r++
+ case 0: // keys are equal, right value overwrites left value
+ result = append(result, right.list[r])
+ l++
+ r++
+ }
}
- return context.WithValue(ctx, labelContextKey{}, &childLabels)
+
+ // Append the remaining elements
+ result = append(result, left.list[l:]...)
+ result = append(result, right.list[r:]...)
+
+ return LabelSet{list: result}
}
// Labels takes an even number of strings representing key-value pairs
panic("uneven number of arguments to pprof.Labels")
}
list := make([]label, 0, len(args)/2)
+ sortedNoDupes := true
for i := 0; i+1 < len(args); i += 2 {
list = append(list, label{key: args[i], value: args[i+1]})
+ sortedNoDupes = sortedNoDupes && (i < 2 || args[i] > args[i-2])
+ }
+ if !sortedNoDupes {
+ // slow path: keys are unsorted, contain duplicates, or both
+ slices.SortStableFunc(list, func(a, b label) int {
+ return strings.Compare(a.key, b.key)
+ })
+ deduped := make([]label, 0, len(list))
+ for i, lbl := range list {
+ if i == 0 || lbl.key != list[i-1].key {
+ deduped = append(deduped, lbl)
+ } else {
+ deduped[len(deduped)-1] = lbl
+ }
+ }
+ list = deduped
}
return LabelSet{list: list}
}
// whether that label exists.
func Label(ctx context.Context, key string) (string, bool) {
ctxLabels := labelValue(ctx)
- v, ok := ctxLabels[key]
- return v, ok
+ for _, lbl := range ctxLabels.list {
+ if lbl.key == key {
+ return lbl.value, true
+ }
+ }
+ return "", false
}
// ForLabels invokes f with each label set on the context.
// The function f should return true to continue iteration or false to stop iteration early.
func ForLabels(ctx context.Context, f func(key, value string) bool) {
ctxLabels := labelValue(ctx)
- for k, v := range ctxLabels {
- if !f(k, v) {
+ for _, lbl := range ctxLabels.list {
+ if !f(lbl.key, lbl.value) {
break
}
}