1 // Copyright 2017, OpenCensus Authors
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
25 // Tag is a key value pair that can be propagated on wire.
31 // Map is a map of tags. Use New to create a context containing
37 // Value returns the value for the key if a value for the key exists.
38 func (m *Map) Value(k Key) (string, bool) {
46 func (m *Map) String() string {
50 keys := make([]Key, 0, len(m.m))
52 keys = append(keys, k)
54 sort.Slice(keys, func(i, j int) bool { return keys[i].Name() < keys[j].Name() })
56 var buffer bytes.Buffer
57 buffer.WriteString("{ ")
58 for _, k := range keys {
59 buffer.WriteString(fmt.Sprintf("{%v %v}", k.name, m.m[k]))
61 buffer.WriteString(" }")
62 return buffer.String()
65 func (m *Map) insert(k Key, v string) {
66 if _, ok := m.m[k]; ok {
72 func (m *Map) update(k Key, v string) {
73 if _, ok := m.m[k]; ok {
78 func (m *Map) upsert(k Key, v string) {
82 func (m *Map) delete(k Key) {
87 return &Map{m: make(map[Key]string)}
90 // Mutator modifies a tag map.
91 type Mutator interface {
92 Mutate(t *Map) (*Map, error)
95 // Insert returns a mutator that inserts a
96 // value associated with k. If k already exists in the tag map,
97 // mutator doesn't update the value.
98 func Insert(k Key, v string) Mutator {
100 fn: func(m *Map) (*Map, error) {
102 return nil, errInvalidValue
110 // Update returns a mutator that updates the
111 // value of the tag associated with k with v. If k doesn't
112 // exists in the tag map, the mutator doesn't insert the value.
113 func Update(k Key, v string) Mutator {
115 fn: func(m *Map) (*Map, error) {
117 return nil, errInvalidValue
125 // Upsert returns a mutator that upserts the
126 // value of the tag associated with k with v. It inserts the
127 // value if k doesn't exist already. It mutates the value
128 // if k already exists.
129 func Upsert(k Key, v string) Mutator {
131 fn: func(m *Map) (*Map, error) {
133 return nil, errInvalidValue
141 // Delete returns a mutator that deletes
142 // the value associated with k.
143 func Delete(k Key) Mutator {
145 fn: func(m *Map) (*Map, error) {
152 // New returns a new context that contains a tag map
153 // originated from the incoming context and modified
154 // with the provided mutators.
155 func New(ctx context.Context, mutator ...Mutator) (context.Context, error) {
157 orig := FromContext(ctx)
159 for k, v := range orig.m {
160 if !checkKeyName(k.Name()) {
161 return ctx, fmt.Errorf("key:%q: %v", k, errInvalidKeyName)
164 return ctx, fmt.Errorf("key:%q value:%q: %v", k.Name(), v, errInvalidValue)
170 for _, mod := range mutator {
171 m, err = mod.Mutate(m)
176 return NewContext(ctx, m), nil
179 // Do is similar to pprof.Do: a convenience for installing the tags
180 // from the context as Go profiler labels. This allows you to
181 // correlated runtime profiling with stats.
183 // It converts the key/values from the given map to Go profiler labels
184 // and calls pprof.Do.
186 // Do is going to do nothing if your Go version is below 1.9.
187 func Do(ctx context.Context, f func(ctx context.Context)) {
191 type mutator struct {
192 fn func(t *Map) (*Map, error)
195 func (m *mutator) Mutate(t *Map) (*Map, error) {