| |
| |
| |
|
|
| package pprof |
|
|
| import ( |
| "context" |
| "fmt" |
| "internal/runtime/pprof/label" |
| "slices" |
| "strings" |
| ) |
|
|
| |
| type LabelSet struct { |
| list []label.Label |
| } |
|
|
| |
| type labelContextKey struct{} |
|
|
| func labelValue(ctx context.Context) labelMap { |
| labels, _ := ctx.Value(labelContextKey{}).(*labelMap) |
| if labels == nil { |
| return labelMap{} |
| } |
| return *labels |
| } |
|
|
| |
| |
| |
| type labelMap struct { |
| label.Set |
| } |
|
|
| |
| |
| func (l *labelMap) String() string { |
| if l == nil { |
| return "" |
| } |
| keyVals := make([]string, 0, len(l.Set.List)) |
|
|
| for _, lbl := range l.Set.List { |
| keyVals = append(keyVals, fmt.Sprintf("%q:%q", lbl.Key, lbl.Value)) |
| } |
|
|
| slices.Sort(keyVals) |
| return "{" + strings.Join(keyVals, ", ") + "}" |
| } |
|
|
| |
| |
| func WithLabels(ctx context.Context, labels LabelSet) context.Context { |
| parentLabels := labelValue(ctx) |
| return context.WithValue(ctx, labelContextKey{}, &labelMap{mergeLabelSets(parentLabels.Set, labels)}) |
| } |
|
|
| func mergeLabelSets(left label.Set, right LabelSet) label.Set { |
| if len(left.List) == 0 { |
| return label.NewSet(right.list) |
| } else if len(right.list) == 0 { |
| return left |
| } |
|
|
| lList, rList := left.List, right.list |
| l, r := 0, 0 |
| result := make([]label.Label, 0, len(rList)) |
| for l < len(lList) && r < len(rList) { |
| switch strings.Compare(lList[l].Key, rList[r].Key) { |
| case -1: |
| result = append(result, lList[l]) |
| l++ |
| case 1: |
| result = append(result, rList[r]) |
| r++ |
| case 0: |
| result = append(result, rList[r]) |
| l++ |
| r++ |
| } |
| } |
|
|
| |
| result = append(result, lList[l:]...) |
| result = append(result, rList[r:]...) |
|
|
| return label.NewSet(result) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| func Labels(args ...string) LabelSet { |
| if len(args)%2 != 0 { |
| panic("uneven number of arguments to pprof.Labels") |
| } |
| list := make([]label.Label, 0, len(args)/2) |
| sortedNoDupes := true |
| for i := 0; i+1 < len(args); i += 2 { |
| list = append(list, label.Label{Key: args[i], Value: args[i+1]}) |
| sortedNoDupes = sortedNoDupes && (i < 2 || args[i] > args[i-2]) |
| } |
| if !sortedNoDupes { |
| |
| slices.SortStableFunc(list, func(a, b label.Label) int { |
| return strings.Compare(a.Key, b.Key) |
| }) |
| deduped := make([]label.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} |
| } |
|
|
| |
| |
| func Label(ctx context.Context, key string) (string, bool) { |
| ctxLabels := labelValue(ctx) |
| for _, lbl := range ctxLabels.Set.List { |
| if lbl.Key == key { |
| return lbl.Value, true |
| } |
| } |
| return "", false |
| } |
|
|
| |
| |
| func ForLabels(ctx context.Context, f func(key, value string) bool) { |
| ctxLabels := labelValue(ctx) |
| for _, lbl := range ctxLabels.Set.List { |
| if !f(lbl.Key, lbl.Value) { |
| break |
| } |
| } |
| } |
|
|