This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathzipkinserver.go
100 lines (86 loc) · 2.25 KB
/
zipkinserver.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
package zipkinserver
import (
"github.com/davecgh/go-spew/spew"
"github.com/mailru/easyjson"
traceformat "github.com/signalfx/golib/trace/format"
"io"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
)
// ZipkinServer is an embedded Zipkin server
type ZipkinServer struct {
server *httptest.Server
spans traceformat.Trace
lock sync.Mutex
}
// URL of the Zipkin server
func (z *ZipkinServer) URL() string {
return z.server.URL + "/v1/trace"
}
// Stop the embedded Zipkin server
func (z *ZipkinServer) Stop() {
z.server.Close()
}
// Reset received spans
func (z *ZipkinServer) Reset() {
z.lock.Lock()
z.spans = nil
z.lock.Unlock()
}
// WaitForSpans waits for numSpans to become available
func (z *ZipkinServer) WaitForSpans(t *testing.T, numSpans int) traceformat.Trace {
deadline := time.Now().Add(3 * time.Second)
var spans traceformat.Trace
for time.Now().Before(deadline) {
z.lock.Lock()
spans = z.spans
z.lock.Unlock()
switch {
case len(spans) == numSpans:
return spans
case len(spans) > numSpans:
t.Fatalf("received %d spans, expected %d: %s", len(spans), numSpans, spew.Sdump(spans))
return nil
default:
time.Sleep(250 * time.Millisecond)
}
}
t.Fatalf("timed out waiting for spans, received %d while expecting %d: %s", len(spans), numSpans, spew.Sdump(spans))
return nil
}
// Start embedded Zipkin server
func Start() *ZipkinServer {
zipkin := &ZipkinServer{}
zipkin.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1/trace" {
w.WriteHeader(http.StatusNotFound)
return
}
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
if r.Header.Get("content-type") != "application/json" {
w.WriteHeader(http.StatusNotAcceptable)
return
}
var trace traceformat.Trace
if err := easyjson.UnmarshalFromReader(r.Body, &trace); err != nil {
_, err = io.WriteString(w, err.Error())
if err != nil {
// Probably can't successfully write the err to the response so just
// panic since this is used for testing.
panic(err)
}
w.WriteHeader(http.StatusBadRequest)
return
}
zipkin.lock.Lock()
zipkin.spans = append(zipkin.spans, trace...)
zipkin.lock.Unlock()
}))
return zipkin
}