forked from opentracing/basictracer-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
62 lines (51 loc) · 1.41 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package basictracer
import (
"sync"
"github.com/opentracing/opentracing-go"
)
// SpanContext holds the basic Span metadata.
type SpanContext struct {
// A probabilistically unique identifier for a [multi-span] trace.
TraceID uint64
// A probabilistically unique identifier for a span.
SpanID uint64
// Whether the trace is sampled.
Sampled bool
// The span's associated baggage.
baggageLock sync.Mutex
Baggage map[string]string // initialized on first use
}
// BaggageItem belongs to the opentracing.SpanContext interface
func (c *SpanContext) BaggageItem(key string) string {
// TODO: if we want to support onBaggage, need a pointer to the bt.Span.
// s.onBaggage(canonicalKey, val)
// if s.trim() {
// return s
// }
c.baggageLock.Lock()
defer c.baggageLock.Unlock()
if c.Baggage == nil {
return ""
}
return c.Baggage[key]
}
// SetBaggageItem belongs to the opentracing.SpanContext interface
func (c *SpanContext) SetBaggageItem(key, val string) opentracing.SpanContext {
c.baggageLock.Lock()
defer c.baggageLock.Unlock()
if c.Baggage == nil {
c.Baggage = make(map[string]string)
}
c.Baggage[key] = val
return c
}
// ForeachBaggageItem belongs to the opentracing.SpanContext interface
func (c *SpanContext) ForeachBaggageItem(handler func(k, v string) bool) {
c.baggageLock.Lock()
defer c.baggageLock.Unlock()
for k, v := range c.Baggage {
if !handler(k, v) {
break
}
}
}