Codebase list golang-github-go-kit-kit / ad1fd3f
package zipkin: added TestToGRPCRequest and TestToGROCContext for the gRPC transport Bas van Beek 8 years ago
4 changed file(s) with 74 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
217217 logger.Log("msg", "trace ID without span ID") // abnormal
218218 spanIDSlc[pos] = strconv.FormatInt(newID(), 64) // deal with it
219219 }
220 spanID, err := strconv.ParseInt(spanIDSlc[len(spanIDSlc)-1], 16, 64)
220 spanID, err := strconv.ParseInt(spanIDSlc[pos], 16, 64)
221221 if err != nil {
222222 logger.Log(spanIDHTTPHeader, spanIDSlc, "err", err) // abnormal
223223 spanID = newID() // deal with it
1010 "testing"
1111
1212 "golang.org/x/net/context"
13 "google.golang.org/grpc/metadata"
1314
1415 "github.com/go-kit/kit/endpoint"
1516 "github.com/go-kit/kit/log"
5960 }
6061 }
6162
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
62107 func TestToRequest(t *testing.T) {
63108 const (
64109 hostport = "5.5.5.5:5555"
86131 }
87132 }
88133
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
89161 func TestAnnotateServer(t *testing.T) {
90162 if err := testAnnotate(zipkin.AnnotateServer, zipkin.ServerReceive, zipkin.ServerSend); err != nil {
91163 t.Fatal(err)
6464 }
6565 ctx = metadata.NewContext(ctx, *md)
6666
67 err = grpc.Invoke(ctx, c.method, req, c.grpcReply, c.client)
68 if err != nil {
67 if err = grpc.Invoke(ctx, c.method, req, c.grpcReply, c.client); err != nil {
6968 return nil, fmt.Errorf("Invoke: %v", err)
7069 }
7170
7372 if err != nil {
7473 return nil, fmt.Errorf("Decode: %v", err)
7574 }
76
7775 return response, nil
7876 }
7977 }
3838 e: e,
3939 dec: dec,
4040 enc: enc,
41 // errorEncoder: defaultErrorEncoder,
4241 logger: log.NewNopLogger(),
4342 }
4443 for _, option := range options {