forked from openzipkin-contrib/zipkin-go-opentracing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector-http_test.go
382 lines (322 loc) · 8.81 KB
/
collector-http_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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package zipkintracer
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"sync"
"testing"
"time"
"github.com/apache/thrift/lib/go/thrift"
"github.com/openzipkin/zipkin-go-opentracing/thrift/gen-go/zipkincore"
)
const (
interval = 10 * time.Millisecond
serverSleep = 100 * time.Millisecond
)
func TestHttpCollector(t *testing.T) {
t.Parallel()
port := 10000
server := newHTTPServer(t, port)
c, err := NewHTTPCollector(fmt.Sprintf("http://localhost:%d/api/v1/spans", port))
if err != nil {
t.Fatal(err)
}
var (
serviceName = "service"
methodName = "method"
traceID = int64(123)
spanID = int64(456)
parentSpanID = int64(0)
value = "foo"
)
span := makeNewSpan("1.2.3.4:1234", serviceName, methodName, traceID, spanID, parentSpanID, true)
annotate(span, time.Now(), value, nil)
if err := c.Collect(span); err != nil {
t.Errorf("error during collection: %v", err)
}
if err := c.Close(); err != nil {
t.Fatalf("error during collection: %v", err)
}
if want, have := 1, len(server.spans()); want != have {
t.Fatal("never received a span")
}
gotSpan := server.spans()[0]
if want, have := methodName, gotSpan.GetName(); want != have {
t.Errorf("want %q, have %q", want, have)
}
if want, have := traceID, gotSpan.TraceID; want != have {
t.Errorf("want %d, have %d", want, have)
}
if want, have := spanID, gotSpan.ID; want != have {
t.Errorf("want %d, have %d", want, have)
}
if want, have := parentSpanID, *gotSpan.ParentID; want != have {
t.Errorf("want %d, have %d", want, have)
}
if want, have := 1, len(gotSpan.GetAnnotations()); want != have {
t.Fatalf("want %d, have %d", want, have)
}
gotAnnotation := gotSpan.GetAnnotations()[0]
if want, have := value, gotAnnotation.GetValue(); want != have {
t.Errorf("want %q, have %q", want, have)
}
}
func TestHttpCollector_Batch(t *testing.T) {
t.Parallel()
port := 10001
server := newHTTPServer(t, port)
var (
batchSize = 5
spanTimeout = 100 * time.Millisecond
)
c, err := NewHTTPCollector(fmt.Sprintf("http://localhost:%d/api/v1/spans", port),
HTTPBatchSize(batchSize),
HTTPBatchInterval(time.Duration(2*batchSize)*spanTimeout), // Make sure timeout won't cause this test to pass
)
if err != nil {
t.Fatal(err)
}
for i := 0; i < batchSize-1; i++ {
if err := c.Collect(&zipkincore.Span{}); err != nil {
t.Errorf("error during collection: %v", err)
}
}
err = consistently(func() bool { return len(server.spans()) == 0 }, spanTimeout)
if err != nil {
t.Fatal("Client sent spans before batch size")
}
if err := c.Collect(&zipkincore.Span{}); err != nil {
t.Errorf("error during collection: %v", err)
}
err = eventually(func() bool { return len(server.spans()) != batchSize }, time.Duration(batchSize)*time.Millisecond)
if err != nil {
t.Fatal("Client did not send spans when batch size reached")
}
}
func TestHttpCollector_BatchInterval(t *testing.T) {
t.Parallel()
port := 10002
server := newHTTPServer(t, port)
var (
batchSize = 5
batchInterval = 100 * time.Millisecond
)
start := time.Now()
c, err := NewHTTPCollector(fmt.Sprintf("http://localhost:%d/api/v1/spans", port),
HTTPBatchSize(batchSize), // Make sure batch won't make this test pass
HTTPBatchInterval(batchInterval),
)
if err != nil {
t.Fatal(err)
}
// send less spans than batchSize in the background
lessThanBatchSize := batchSize - 1
go func() {
for i := 0; i < lessThanBatchSize; i++ {
if err := c.Collect(&zipkincore.Span{}); err != nil {
t.Errorf("error during collection: %v", err)
}
}
}()
beforeInterval := batchInterval - (2 * interval) - time.Now().Sub(start)
err = consistently(func() bool { return len(server.spans()) == 0 }, beforeInterval)
if err != nil {
t.Fatal("Client sent spans before timeout")
}
afterInterval := batchInterval * 2
err = eventually(func() bool { return len(server.spans()) == lessThanBatchSize }, afterInterval)
if err != nil {
t.Fatal("Client did not send spans after timeout")
}
}
// TestHttpCollector_NonBlockCollect tests that the Collect
// function is non-blocking, even when the server is slow.
// Use of the /api/v1/sleep endpoint registered in the server.
func TestHttpCollector_NonBlockCollect(t *testing.T) {
t.Parallel()
port := 10003
newHTTPServer(t, port)
c, err := NewHTTPCollector(fmt.Sprintf("http://localhost:%d/api/v1/sleep", port))
if err != nil {
t.Fatal(err)
}
start := time.Now()
if err := c.Collect(&zipkincore.Span{}); err != nil {
t.Errorf("error during collection: %v", err)
}
if time.Now().Sub(start) >= serverSleep {
t.Fatal("Collect is blocking")
}
}
func TestHttpCollector_MaxBatchSize(t *testing.T) {
t.Parallel()
port := 10004
server := newHTTPServer(t, port)
var (
maxBacklog = 5
batchSize = maxBacklog * 2 // make backsize bigger than backlog enable testing backlog disposal
)
c, err := NewHTTPCollector(fmt.Sprintf("http://localhost:%d/api/v1/spans", port),
HTTPMaxBacklog(maxBacklog),
HTTPBatchSize(batchSize),
)
if err != nil {
t.Fatal(err)
}
for i := 0; i < batchSize; i++ {
c.Collect(makeNewSpan("", "", "", 0, int64(i), 0, false))
}
c.Close()
for i, s := range server.spans() {
if want, have := int64(i+maxBacklog), s.ID; want != have {
t.Errorf("Span ID is wrong. want %d, have %d", want, have)
}
}
}
func TestHTTPCollector_RequestCallback(t *testing.T) {
t.Parallel()
var (
err error
port = 10005
server = newHTTPServer(t, port)
hdrKey = "test-key"
hdrValue = "test-value"
)
c, err := NewHTTPCollector(
fmt.Sprintf("http://localhost:%d/api/v1/spans", port),
HTTPRequestCallback(func(r *http.Request) {
r.Header.Add(hdrKey, hdrValue)
}),
)
if err != nil {
t.Fatal(err)
}
if err = c.Collect(&zipkincore.Span{}); err != nil {
t.Fatal(err)
}
if err = c.Close(); err != nil {
t.Fatal(err)
}
if want, have := 1, len(server.spans()); want != have {
t.Fatal("never received a span")
}
headers := server.headers()
if len(headers) == 0 {
t.Fatalf("Collect request was not handled")
}
testHeader := headers.Get(hdrKey)
if !strings.EqualFold(testHeader, hdrValue) {
t.Errorf("Custom header not received. want %s, have %s", testHeader, hdrValue)
}
server.clearHeaders()
}
type httpServer struct {
t *testing.T
zipkinSpans []*zipkincore.Span
zipkinHeader http.Header
mutex sync.RWMutex
}
func (s *httpServer) spans() []*zipkincore.Span {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s.zipkinSpans
}
func (s *httpServer) clearSpans() {
s.mutex.Lock()
defer s.mutex.Unlock()
s.zipkinSpans = s.zipkinSpans[:0]
}
func (s *httpServer) headers() http.Header {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s.zipkinHeader
}
func (s *httpServer) clearHeaders() {
s.mutex.Lock()
defer s.mutex.Unlock()
s.zipkinHeader = make(http.Header, 0)
}
func newHTTPServer(t *testing.T, port int) *httpServer {
server := &httpServer{
t: t,
zipkinSpans: make([]*zipkincore.Span, 0),
mutex: sync.RWMutex{},
}
handler := http.NewServeMux()
handler.HandleFunc("/api/v1/spans", func(w http.ResponseWriter, r *http.Request) {
contextType := r.Header.Get("Content-Type")
if contextType != "application/x-thrift" {
t.Fatalf(
"except Content-Type should be application/x-thrift, but is %s",
contextType)
}
// clone headers from request
headers := make(http.Header, len(r.Header))
for k, vv := range r.Header {
vv2 := make([]string, len(vv))
copy(vv2, vv)
headers[k] = vv2
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
buffer := thrift.NewTMemoryBuffer()
if _, err = buffer.Write(body); err != nil {
t.Error(err)
return
}
transport := thrift.NewTBinaryProtocolTransport(buffer)
_, size, err := transport.ReadListBegin()
if err != nil {
t.Error(err)
return
}
var spans []*zipkincore.Span
for i := 0; i < size; i++ {
zs := &zipkincore.Span{}
if err = zs.Read(transport); err != nil {
t.Error(err)
return
}
spans = append(spans, zs)
}
err = transport.ReadListEnd()
if err != nil {
t.Error(err)
return
}
server.mutex.Lock()
defer server.mutex.Unlock()
server.zipkinSpans = append(server.zipkinSpans, spans...)
server.zipkinHeader = headers
})
handler.HandleFunc("/api/v1/sleep", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(serverSleep)
})
go func() {
http.ListenAndServe(fmt.Sprintf(":%d", port), handler)
}()
return server
}
func consistently(assertion func() bool, atList time.Duration) error {
deadline := time.Now().Add(atList)
for time.Now().Before(deadline) {
if !assertion() {
return fmt.Errorf("failed")
}
time.Sleep(interval)
}
return nil
}
func eventually(assertion func() bool, timeout time.Duration) error {
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
if assertion() {
return nil
}
time.Sleep(interval)
}
return fmt.Errorf("failed")
}