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_test.go
188 lines (154 loc) · 4.79 KB
/
propagation_test.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package instana_test
import (
"fmt"
"net/http"
"testing"
"time"
"github.com/instana/go-sensor"
opentracing "github.com/opentracing/opentracing-go"
"github.com/stretchr/testify/assert"
)
func TestSpanPropagator(t *testing.T) {
const op = "test"
opts := instana.Options{LogLevel: instana.Debug}
recorder := instana.NewTestRecorder()
tracer := instana.NewTracerWithEverything(&opts, recorder)
sp := tracer.StartSpan(op)
sp.SetBaggageItem("foo", "bar")
tmc := opentracing.HTTPHeadersCarrier(http.Header{})
tests := []struct {
typ, carrier interface{}
}{
{opentracing.HTTPHeaders, tmc},
{opentracing.TextMap, tmc},
}
for i, test := range tests {
if err := tracer.Inject(sp.Context(), test.typ, test.carrier); err != nil {
t.Fatalf("%d: %v", i, err)
}
injectedContext, err := tracer.Extract(test.typ, test.carrier)
if err != nil {
t.Fatalf("%d: %v", i, err)
}
child := tracer.StartSpan(
op,
opentracing.ChildOf(injectedContext))
child.Finish()
}
sp.Finish()
spans := recorder.GetQueuedSpans()
if a, e := len(spans), len(tests)+1; a != e {
t.Fatalf("expected %d spans, got %d", e, a)
}
// The last span is the original one.
exp, spans := spans[len(spans)-1], spans[:len(spans)-1]
exp.Duration = uint64(time.Duration(123))
// exp.Timestamp = uint64(time.Time{}.Add(1))
for i, span := range spans {
if a, e := *span.ParentID, exp.SpanID; a != e {
t.Fatalf("%d: ParentID %d does not match expectation %d", i, a, e)
} else {
// Prepare for comparison.
span.SpanID, span.ParentID = exp.SpanID, nil
span.Duration, span.Timestamp = exp.Duration, exp.Timestamp
}
if a, e := span.TraceID, exp.TraceID; a != e {
t.Fatalf("%d: TraceID changed from %d to %d", i, e, a)
}
}
}
func TestCaseSensitiveHeaderPropagation(t *testing.T) {
var (
op = "test"
spanParentIDBase64 = int64(4884)
spanParentIDString = "1314"
)
opts := instana.Options{LogLevel: instana.Debug}
recorder := instana.NewTestRecorder()
tracer := instana.NewTracerWithEverything(&opts, recorder)
// Simulate an existing root span
metadata := make(map[string]string)
metadata["X-Instana-T"] = spanParentIDString
metadata["X-Instana-S"] = spanParentIDString
metadata["X-Instana-L"] = "1"
metadata["X-Instana-B-Foo"] = "bar"
tmc1 := opentracing.TextMapCarrier(metadata)
tmc2 := opentracing.TextMapCarrier(make(map[string]string))
for k, v := range tmc1 {
tmc2[k] = v
}
tests := []struct {
typ, carrier interface{}
}{
{opentracing.HTTPHeaders, tmc1},
{opentracing.TextMap, tmc2},
}
for i, test := range tests {
// Extract the existing context
injectedContext, err := tracer.Extract(test.typ, test.carrier)
if err != nil {
t.Fatalf("%d: %v", i, err)
}
// Start a new child span and overwrite the baggage key
child := tracer.StartSpan(
op,
opentracing.ChildOf(injectedContext))
child.SetBaggageItem("foo", "baz")
// Inject the context into the metadata
if err := tracer.Inject(child.Context(), test.typ, test.carrier); err != nil {
t.Fatalf("%d: %v", i, err)
}
child.Finish()
assert.Equal(t, child.BaggageItem("foo"), "baz")
}
for _, s := range recorder.GetQueuedSpans() {
assert.Equal(t, spanParentIDBase64, *s.ParentID)
assert.NotEqual(t, spanParentIDBase64, s.SpanID)
}
}
func TestSingleHeaderPropagation(t *testing.T) {
var (
op = "test"
spanParentIDBase64 = int64(4884)
spanParentIDString = "1314"
)
opts := instana.Options{LogLevel: instana.Debug}
recorder := instana.NewTestRecorder()
tracer := instana.NewTracerWithEverything(&opts, recorder)
// Simulate an existing root span
metadata := make(http.Header)
metadata.Set("X-Instana-T", spanParentIDString)
metadata.Set("X-Instana-S", spanParentIDString)
metadata.Set("X-Instana-L", "1")
metadata.Set("X-Instana-B-Foo", "bar")
tmc1 := opentracing.HTTPHeadersCarrier(metadata)
tests := []struct {
typ, carrier interface{}
}{
{opentracing.HTTPHeaders, tmc1},
}
for i, test := range tests {
// Extract the existing context
injectedContext, err := tracer.Extract(test.typ, test.carrier)
if err != nil {
t.Fatalf("%d: %v", i, err)
}
// Start a new child span and overwrite the baggage key
child := tracer.StartSpan(
op,
opentracing.ChildOf(injectedContext))
child.SetBaggageItem("foo", "baz")
// Inject the context into the metadata
if err := tracer.Inject(child.Context(), test.typ, test.carrier); err != nil {
t.Fatalf("%d: %v", i, err)
}
child.Finish()
s := recorder.GetQueuedSpans()[0]
assert.Equal(t, child.BaggageItem("foo"), "baz")
assert.Equal(t, []string{fmt.Sprintf("%x", s.SpanID)}, http.Header(test.carrier.(opentracing.HTTPHeadersCarrier))["X-Instana-S"])
}
for _, s := range recorder.GetQueuedSpans() {
assert.Equal(t, spanParentIDBase64, *s.ParentID)
assert.NotEqual(t, spanParentIDBase64, s.SpanID)
}
}