forked from fabiolb/fabio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrace_test.go
149 lines (125 loc) · 4.49 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
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
package trace
import (
"net/http"
"testing"
"github.com/fabiolb/fabio/config"
opentracing "github.com/opentracing/opentracing-go"
mocktracer "github.com/opentracing/opentracing-go/mocktracer"
zipkin "github.com/openzipkin-contrib/zipkin-go-opentracing"
zipkintypes "github.com/openzipkin-contrib/zipkin-go-opentracing/types"
)
const testServiceName = "TEST-SERVICE"
func TestCreateCollectorHTTP(t *testing.T) {
if CreateCollector("http", "", "") == nil {
t.Error("CreateCollector returned nil value.")
t.FailNow()
}
}
func TestCreateSpanNoGlobalTracer(t *testing.T) {
opentracing.SetGlobalTracer(nil)
req, _ := http.NewRequest("GET", "http://example.com", nil)
if CreateSpan(req, &config.Tracing{ServiceName: "fabiolb-test", SpanName: "{{.Method}} {{.Path}}"}) != 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, &config.Tracing{ServiceName: "fabiolb-test", SpanName: "{{.Method}} {{.Path}}"}) == 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, &config.Tracing{ServiceName: "fabiolb-test", SpanName: "{{.Method}} {{.Path}}"}) == nil {
t.Error("Received a nil span while a global tracer was set.")
t.FailNow()
}
}
func TestInitializeTracer(t *testing.T) {
opentracing.SetGlobalTracer(nil)
InitializeTracer(&config.Tracing{TracingEnabled: true, CollectorType: "http"})
if opentracing.GlobalTracer() == nil {
t.Error("InitializeTracer set a nil tracer.")
t.FailNow()
}
}
func TestInitializeTracerWhileDisabled(t *testing.T) {
opentracing.SetGlobalTracer(nil)
InitializeTracer(&config.Tracing{TracingEnabled: false, CollectorType: "http"})
if opentracing.GlobalTracer() != nil {
t.Error("InitializeTracer set a tracer while tracing was disabled.")
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()
}
}
func TestSpanName(t *testing.T) {
req, _ := http.NewRequest("GET", "http://example.com/demo", nil)
if spanName("{{.Method}} {{.Host}} {{.Path}}", req) != "GET example.com /demo" {
t.Error("spanName did not properly render the supported template")
t.Fail()
}
if spanName("{{.Invalid", req) != "{{.Invalid" {
t.Error("spanName did not return the unrendered string of the invalid template")
t.Fail()
}
if spanName("{{.Unsupported}}", req) != "{{.Unsupported}}" {
t.Error("spanName did not return the unrendered string of the unsupported template")
t.Fail()
}
}