This repository has been archived by the owner on Sep 17, 2020. It is now read-only.
forked from instana/go-sensor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpropagation.go
165 lines (143 loc) · 3.33 KB
/
propagation.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package instana
import (
"net/http"
"strconv"
"strings"
ot "github.com/opentracing/opentracing-go"
)
type textMapPropagator struct {
tracer *tracerS
}
// Instana header constants
const (
// FieldT Trace ID header
FieldT = "x-instana-t"
// FieldS Span ID header
FieldS = "x-instana-s"
// FieldL Level header
FieldL = "x-instana-l"
// FieldB OT Baggage header
FieldB = "x-instana-b-"
fieldCount = 2
)
func (r *textMapPropagator) inject(spanContext ot.SpanContext, opaqueCarrier interface{}) error {
sc, ok := spanContext.(SpanContext)
if !ok {
return ot.ErrInvalidSpanContext
}
roCarrier, ok := opaqueCarrier.(ot.TextMapReader)
if !ok {
return ot.ErrInvalidCarrier
}
// Handle pre-existing case-sensitive keys
var (
exstfieldT = FieldT
exstfieldS = FieldS
exstfieldL = FieldL
exstfieldB = FieldB
)
roCarrier.ForeachKey(func(k, v string) error {
switch strings.ToLower(k) {
case FieldT:
exstfieldT = k
case FieldS:
exstfieldS = k
case FieldL:
exstfieldL = k
default:
if strings.HasPrefix(strings.ToLower(k), FieldB) {
exstfieldB = string([]rune(k)[0:len(FieldB)])
}
}
return nil
})
carrier, ok := opaqueCarrier.(ot.TextMapWriter)
if !ok {
return ot.ErrInvalidCarrier
}
hhcarrier, ok := opaqueCarrier.(ot.HTTPHeadersCarrier)
if ok {
// If http.Headers has pre-existing keys, calling Set() like we do
// below will just append to those existing values and break context
// propagation. So defend against that case, we delete any pre-existing
// keys entirely first.
y := http.Header(hhcarrier)
y.Del(exstfieldT)
y.Del(exstfieldS)
y.Del(exstfieldL)
for key := range y {
if strings.HasPrefix(strings.ToLower(key), FieldB) {
y.Del(key)
}
}
}
if instanaTID, err := ID2Header(sc.TraceID); err == nil {
carrier.Set(exstfieldT, instanaTID)
} else {
log.debug(err)
}
if instanaSID, err := ID2Header(sc.SpanID); err == nil {
carrier.Set(exstfieldS, instanaSID)
} else {
log.debug(err)
}
carrier.Set(exstfieldL, strconv.Itoa(1))
for k, v := range sc.Baggage {
carrier.Set(exstfieldB+k, v)
}
return nil
}
func (r *textMapPropagator) extract(opaqueCarrier interface{}) (ot.SpanContext, error) {
carrier, ok := opaqueCarrier.(ot.TextMapReader)
if !ok {
return nil, ot.ErrInvalidCarrier
}
fieldCount := 0
var traceID, spanID int64
var err error
baggage := make(map[string]string)
err = carrier.ForeachKey(func(k, v string) error {
switch strings.ToLower(k) {
case FieldT:
fieldCount++
traceID, err = Header2ID(v)
if err != nil {
return ot.ErrSpanContextCorrupted
}
case FieldS:
fieldCount++
spanID, err = Header2ID(v)
if err != nil {
return ot.ErrSpanContextCorrupted
}
default:
lk := strings.ToLower(k)
if strings.HasPrefix(lk, FieldB) {
baggage[strings.TrimPrefix(lk, FieldB)] = v
}
}
return nil
})
return r.finishExtract(err, fieldCount, traceID, spanID, baggage)
}
func (r *textMapPropagator) finishExtract(err error,
fieldCount int,
traceID int64,
spanID int64,
baggage map[string]string) (ot.SpanContext, error) {
if err != nil {
return nil, err
}
if fieldCount < 2 {
if fieldCount == 0 {
return nil, ot.ErrSpanContextNotFound
}
return nil, ot.ErrSpanContextCorrupted
}
return SpanContext{
TraceID: traceID,
SpanID: spanID,
Sampled: false,
Baggage: baggage,
}, nil
}