...
Source file src/runtime/pprof/label.go
1
2
3
4
5 package pprof
6
7 import (
8 "context"
9 )
10
11 type label struct {
12 key string
13 value string
14 }
15
16
17 type LabelSet struct {
18 list []label
19 }
20
21
22 type labelContextKey struct{}
23
24 func labelValue(ctx context.Context) labelMap {
25 labels, _ := ctx.Value(labelContextKey{}).(*labelMap)
26 if labels == nil {
27 return labelMap(nil)
28 }
29 return *labels
30 }
31
32
33
34
35 type labelMap map[string]string
36
37
38
39 func WithLabels(ctx context.Context, labels LabelSet) context.Context {
40 childLabels := make(labelMap)
41 parentLabels := labelValue(ctx)
42
43
44
45 for k, v := range parentLabels {
46 childLabels[k] = v
47 }
48 for _, label := range labels.list {
49 childLabels[label.key] = label.value
50 }
51 return context.WithValue(ctx, labelContextKey{}, &childLabels)
52 }
53
54
55
56
57
58
59 func Labels(args ...string) LabelSet {
60 if len(args)%2 != 0 {
61 panic("uneven number of arguments to pprof.Labels")
62 }
63 labels := LabelSet{}
64 for i := 0; i+1 < len(args); i += 2 {
65 labels.list = append(labels.list, label{key: args[i], value: args[i+1]})
66 }
67 return labels
68 }
69
70
71
72 func Label(ctx context.Context, key string) (string, bool) {
73 ctxLabels := labelValue(ctx)
74 v, ok := ctxLabels[key]
75 return v, ok
76 }
77
78
79
80 func ForLabels(ctx context.Context, f func(key, value string) bool) {
81 ctxLabels := labelValue(ctx)
82 for k, v := range ctxLabels {
83 if !f(k, v) {
84 break
85 }
86 }
87 }
88
View as plain text