1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
19 // LabelSet is a set of labels.
20 type LabelSet struct {
24 // labelContextKey is the type of contextKeys used for profiler labels.
25 type labelContextKey struct{}
27 func labelValue(ctx context.Context) labelMap {
28 labels, _ := ctx.Value(labelContextKey{}).(*labelMap)
35 // labelMap is the representation of the label set held in the context type.
36 // This is an initial implementation, but it will be replaced with something
37 // that admits incremental immutable modification more efficiently.
38 type labelMap map[string]string
40 // String satisfies Stringer and returns key, value pairs in a consistent
42 func (l *labelMap) String() string {
46 keyVals := make([]string, 0, len(*l))
48 for k, v := range *l {
49 keyVals = append(keyVals, fmt.Sprintf("%q:%q", k, v))
54 return "{" + strings.Join(keyVals, ", ") + "}"
57 // WithLabels returns a new context.Context with the given labels added.
58 // A label overwrites a prior label with the same key.
59 func WithLabels(ctx context.Context, labels LabelSet) context.Context {
60 parentLabels := labelValue(ctx)
61 childLabels := make(labelMap, len(parentLabels))
62 // TODO(matloob): replace the map implementation with something
63 // more efficient so creating a child context WithLabels doesn't need
65 for k, v := range parentLabels {
68 for _, label := range labels.list {
69 childLabels[label.key] = label.value
71 return context.WithValue(ctx, labelContextKey{}, &childLabels)
74 // Labels takes an even number of strings representing key-value pairs
75 // and makes a LabelSet containing them.
76 // A label overwrites a prior label with the same key.
77 // Currently only the CPU and goroutine profiles utilize any labels
79 // See https://golang.org/issue/23458 for details.
80 func Labels(args ...string) LabelSet {
82 panic("uneven number of arguments to pprof.Labels")
84 list := make([]label, 0, len(args)/2)
85 for i := 0; i+1 < len(args); i += 2 {
86 list = append(list, label{key: args[i], value: args[i+1]})
88 return LabelSet{list: list}
91 // Label returns the value of the label with the given key on ctx, and a boolean indicating
92 // whether that label exists.
93 func Label(ctx context.Context, key string) (string, bool) {
94 ctxLabels := labelValue(ctx)
95 v, ok := ctxLabels[key]
99 // ForLabels invokes f with each label set on the context.
100 // The function f should return true to continue iteration or false to stop iteration early.
101 func ForLabels(ctx context.Context, f func(key, value string) bool) {
102 ctxLabels := labelValue(ctx)
103 for k, v := range ctxLabels {