-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathspan_test.go
103 lines (77 loc) · 2.89 KB
/
span_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
package instana_test
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/instana/golang-sensor"
ot "github.com/opentracing/opentracing-go"
)
func TestBasicSpan(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.Finish()
spans := recorder.GetSpans()
assert.Equal(t, 1, len(spans))
span := spans[0]
assert.NotZero(t, span.SpanID, "Missing span ID for this span")
assert.NotZero(t, span.TraceID, "Missing trace ID for this span")
assert.NotZero(t, span.Timestamp, "Missing timestamp for this span")
assert.NotNil(t, span.Duration, "Duration is nil")
assert.Equal(t, "sdk", span.Name, "Missing sdk span name")
assert.Equal(t, "test", span.Data.SDK.Name, "Missing span name")
assert.Nil(t, span.Data.SDK.Custom.Tags, "Tags has an unexpected value")
assert.Nil(t, span.Data.SDK.Custom.Baggage, "Baggage has an unexpected value")
}
func TestSpanHeritage(t *testing.T) {
recorder := instana.NewTestRecorder()
tracer := instana.NewTracerWithEverything(&instana.Options{}, recorder)
parentSpan := tracer.StartSpan("parent")
childSpan := tracer.StartSpan("child", ot.ChildOf(parentSpan.Context()))
time.Sleep(2 * time.Millisecond)
childSpan.Finish()
time.Sleep(2 * time.Millisecond)
parentSpan.Finish()
spans := recorder.GetSpans()
assert.Equal(t, len(spans), 2)
cSpan := spans[0]
pSpan := spans[1]
assert.Equal(t, "child", cSpan.Data.SDK.Name, "Child span name doesn't compute")
assert.Equal(t, "parent", pSpan.Data.SDK.Name, "Parent span name doesn't compute")
// Parent should not have a parent
assert.Nil(t, pSpan.ParentID, "ParentID shouldn't have a value")
// Child must have parent ID set as parent
assert.Equal(t, pSpan.SpanID, *cSpan.ParentID, "parentID doesn't match")
// Must be root span
assert.Equal(t, pSpan.TraceID, pSpan.SpanID, "not a root span")
// Trace ID must be consistent across spans
assert.Equal(t, cSpan.TraceID, pSpan.TraceID, "trace IDs don't match")
}
func TestSpanBaggage(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")
sp.Finish()
spans := recorder.GetSpans()
assert.Equal(t, len(spans), 1)
span := spans[0]
assert.NotNil(t, span.Data.SDK.Custom.Baggage, "Missing Baggage")
}
func TestSpanTags(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.SetTag("foo", "bar")
sp.Finish()
spans := recorder.GetSpans()
assert.Equal(t, len(spans), 1)
span := spans[0]
assert.NotNil(t, span.Data.SDK.Custom.Tags, "Missing Tags")
}