10 | 10 |
"testing"
|
11 | 11 |
|
12 | 12 |
"golang.org/x/net/context"
|
|
13 |
"google.golang.org/grpc/metadata"
|
13 | 14 |
|
14 | 15 |
"github.com/go-kit/kit/endpoint"
|
15 | 16 |
"github.com/go-kit/kit/log"
|
|
59 | 60 |
}
|
60 | 61 |
}
|
61 | 62 |
|
|
63 |
func TestToGRPCContext(t *testing.T) {
|
|
64 |
const (
|
|
65 |
hostport = "5.5.5.5:5555"
|
|
66 |
serviceName = "foo-service"
|
|
67 |
methodName = "foo-method"
|
|
68 |
traceID int64 = 12
|
|
69 |
spanID int64 = 34
|
|
70 |
parentSpanID int64 = 56
|
|
71 |
)
|
|
72 |
|
|
73 |
md := metadata.MD{
|
|
74 |
"x-b3-traceid": []string{strconv.FormatInt(traceID, 16)},
|
|
75 |
"x-b3-spanid": []string{strconv.FormatInt(spanID, 16)},
|
|
76 |
"x-b3-parentspanid": []string{strconv.FormatInt(parentSpanID, 16)},
|
|
77 |
}
|
|
78 |
|
|
79 |
newSpan := zipkin.MakeNewSpanFunc(hostport, serviceName, methodName)
|
|
80 |
toContext := zipkin.ToGRPCContext(newSpan, log.NewLogfmtLogger(ioutil.Discard))
|
|
81 |
|
|
82 |
ctx := toContext(context.Background(), &md)
|
|
83 |
val := ctx.Value(zipkin.SpanContextKey)
|
|
84 |
if val == nil {
|
|
85 |
t.Fatalf("%s returned no value", zipkin.SpanContextKey)
|
|
86 |
}
|
|
87 |
span, ok := val.(*zipkin.Span)
|
|
88 |
if !ok {
|
|
89 |
t.Fatalf("%s was not a Span object", zipkin.SpanContextKey)
|
|
90 |
}
|
|
91 |
|
|
92 |
for want, haveFunc := range map[int64]func() int64{
|
|
93 |
traceID: span.TraceID,
|
|
94 |
spanID: span.SpanID,
|
|
95 |
parentSpanID: span.ParentSpanID,
|
|
96 |
} {
|
|
97 |
if have := haveFunc(); want != have {
|
|
98 |
name := runtime.FuncForPC(reflect.ValueOf(haveFunc).Pointer()).Name()
|
|
99 |
name = strings.Split(name, "ยท")[0]
|
|
100 |
toks := strings.Split(name, ".")
|
|
101 |
name = toks[len(toks)-1]
|
|
102 |
t.Errorf("%s: want %d, have %d", name, want, have)
|
|
103 |
}
|
|
104 |
}
|
|
105 |
}
|
|
106 |
|
62 | 107 |
func TestToRequest(t *testing.T) {
|
63 | 108 |
const (
|
64 | 109 |
hostport = "5.5.5.5:5555"
|
|
86 | 131 |
}
|
87 | 132 |
}
|
88 | 133 |
|
|
134 |
func TestToGRPCRequest(t *testing.T) {
|
|
135 |
const (
|
|
136 |
hostport = "5.5.5.5:5555"
|
|
137 |
serviceName = "foo-service"
|
|
138 |
methodName = "foo-method"
|
|
139 |
traceID int64 = 20
|
|
140 |
spanID int64 = 40
|
|
141 |
parentSpanID int64 = 90
|
|
142 |
)
|
|
143 |
|
|
144 |
newSpan := zipkin.MakeNewSpanFunc(hostport, serviceName, methodName)
|
|
145 |
span := newSpan(traceID, spanID, parentSpanID)
|
|
146 |
ctx := context.WithValue(context.Background(), zipkin.SpanContextKey, span)
|
|
147 |
md := &metadata.MD{}
|
|
148 |
ctx = zipkin.ToGRPCRequest(newSpan)(ctx, md)
|
|
149 |
|
|
150 |
for header, wantInt := range map[string]int64{
|
|
151 |
"x-b3-traceid": traceID,
|
|
152 |
"x-b3-spanid": spanID,
|
|
153 |
"x-b3-parentspanid": parentSpanID,
|
|
154 |
} {
|
|
155 |
if want, have := strconv.FormatInt(wantInt, 16), (*md)[header][0]; want != have {
|
|
156 |
t.Errorf("%s: want %q, have %q", header, want, have)
|
|
157 |
}
|
|
158 |
}
|
|
159 |
}
|
|
160 |
|
89 | 161 |
func TestAnnotateServer(t *testing.T) {
|
90 | 162 |
if err := testAnnotate(zipkin.AnnotateServer, zipkin.ServerReceive, zipkin.ServerSend); err != nil {
|
91 | 163 |
t.Fatal(err)
|