/
trace_test.go
104 lines (87 loc) · 2.99 KB
/
trace_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
package trace
import (
"net/http"
"testing"
opentracing "github.com/opentracing/opentracing-go"
mocktracer "github.com/opentracing/opentracing-go/mocktracer"
zipkin "github.com/openzipkin/zipkin-go-opentracing"
zipkintypes "github.com/openzipkin/zipkin-go-opentracing/types"
)
const testServiceName = "TEST-SERVICE"
func TestCreateSpanNoGlobalTracer(t *testing.T) {
opentracing.SetGlobalTracer(nil)
req, _ := http.NewRequest("GET", "http://example.com", nil)
if CreateSpan(req, testServiceName) != nil {
t.Error("CreateSpan returned a non-nil result using a nil global tracer.")
t.Fail()
}
}
func TestCreateSpanWithNoParent(t *testing.T) {
tracer, _ := zipkin.NewTracer(nil)
opentracing.SetGlobalTracer(tracer)
req, _ := http.NewRequest("GET", "http://example.com", nil)
if CreateSpan(req, testServiceName) == nil {
t.Error("Received nil span while a global tracer was set.")
t.FailNow()
}
}
func TestCreateSpanWithParent(t *testing.T) {
mt := mocktracer.New()
opentracing.SetGlobalTracer(mt)
globalTracer := opentracing.GlobalTracer()
parentSpan := globalTracer.StartSpan(testServiceName)
requestIn, _ := http.NewRequest("GET", "http://example.com", nil)
mt.Inject(parentSpan.Context(),
opentracing.HTTPHeaders,
opentracing.HTTPHeadersCarrier(requestIn.Header),
)
if CreateSpan(requestIn, testServiceName+"-child") == nil {
t.Error("Received a nil span while a global tracer was set.")
t.FailNow()
}
}
func TestInjectHeaders(t *testing.T) {
mt := mocktracer.New()
opentracing.SetGlobalTracer(mt)
globalTracer := opentracing.GlobalTracer()
span := globalTracer.StartSpan(testServiceName)
req, _ := http.NewRequest("GET", "http://example.com", nil)
InjectHeaders(span, req)
if req.Header.Get("Mockpfx-Ids-Traceid") == "" {
t.Error("Inject did not set the Traceid in the request.")
t.Fail()
}
if req.Header.Get("Mockpfx-Ids-Spanid") == "" {
t.Error("Inject did not set the Spanid in the request.")
t.Fail()
}
}
func TestInjectHeadersWithParentSpan(t *testing.T) {
parentSpanId := uint64(12345)
parentSpanContext := zipkin.SpanContext{
SpanID: parentSpanId,
TraceID: zipkintypes.TraceID{High: uint64(1234), Low: uint64(4321)},
}
tracer, _ := zipkin.NewTracer(nil)
opentracing.SetGlobalTracer(tracer)
globalTracer := opentracing.GlobalTracer()
childSpan := globalTracer.StartSpan(testServiceName+"-CHILD", opentracing.ChildOf(parentSpanContext))
req, _ := http.NewRequest("GET", "http://example.com", nil)
InjectHeaders(childSpan, req)
if req.Header.Get("X-B3-Traceid") == "" {
t.Error("Inject did not set the Traceid in the request.")
t.Fail()
}
if req.Header.Get("X-B3-Spanid") == "" {
t.Error("Inject did not set the Spanid in the request.")
t.Fail()
}
if req.Header.Get("X-B3-Parentspanid") != "0000000000003039" {
t.Error("Inject did not set the correct Parentspanid in the request.")
t.Fail()
}
if req.Header.Get("x-B3-Traceid") != "00000000000004d200000000000010e1" {
t.Error("Inject did not reuse the Traceid from the parent span")
t.Fail()
}
}