Package list golang-github-go-kit-kit / 142db49
tracing: fixes for new OpenTracing changes - mocktracer.FinishedSpans becomes GetFinishedSpans() - endpoint.Nop is a thing (for tests) - Some idiomaticization of var declaration Peter Bourgon 5 years ago
3 changed file(s) with 29 addition(s) and 33 deletion(s). Raw diff Collapse all Expand all
1717 contextSpan := tracer.StartSpan("").(*mocktracer.MockSpan)
1818 ctx := opentracing.ContextWithSpan(context.Background(), contextSpan)
1919
20 var innerEndpoint endpoint.Endpoint
21 innerEndpoint = func(context.Context, interface{}) (interface{}, error) {
22 return struct{}{}, nil
23 }
24 tracedEndpoint := kitot.TraceServer(tracer, "testOp")(innerEndpoint)
20 tracedEndpoint := kitot.TraceServer(tracer, "testOp")(endpoint.Nop)
2521 if _, err := tracedEndpoint(ctx, struct{}{}); err != nil {
2622 t.Fatal(err)
2723 }
28 if want, have := 1, len(tracer.FinishedSpans); want != have {
24
25 finishedSpans := tracer.GetFinishedSpans()
26 if want, have := 1, len(finishedSpans); want != have {
2927 t.Fatalf("Want %v span(s), found %v", want, have)
3028 }
3129
32 endpointSpan := tracer.FinishedSpans[0]
3330 // Test that the op name is updated
31 endpointSpan := finishedSpans[0]
3432 if want, have := "testOp", endpointSpan.OperationName; want != have {
3533 t.Fatalf("Want %q, have %q", want, have)
3634 }
37 // ... and that the ID is unmodified.
35
36 // ...and that the ID is unmodified.
3837 if want, have := contextSpan.SpanID, endpointSpan.SpanID; want != have {
3938 t.Errorf("Want SpanID %q, have %q", want, have)
4039 }
4342 func TestTraceServerNoContextSpan(t *testing.T) {
4443 tracer := mocktracer.New()
4544
46 var innerEndpoint endpoint.Endpoint
47 innerEndpoint = func(context.Context, interface{}) (interface{}, error) {
48 return struct{}{}, nil
49 }
50 tracedEndpoint := kitot.TraceServer(tracer, "testOp")(innerEndpoint)
51 // Empty/background context:
45 // Empty/background context.
46 tracedEndpoint := kitot.TraceServer(tracer, "testOp")(endpoint.Nop)
5247 if _, err := tracedEndpoint(context.Background(), struct{}{}); err != nil {
5348 t.Fatal(err)
5449 }
55 // tracedEndpoint created a new Span:
56 if want, have := 1, len(tracer.FinishedSpans); want != have {
50
51 // tracedEndpoint created a new Span.
52 finishedSpans := tracer.GetFinishedSpans()
53 if want, have := 1, len(finishedSpans); want != have {
5754 t.Fatalf("Want %v span(s), found %v", want, have)
5855 }
5956
60 endpointSpan := tracer.FinishedSpans[0]
57 endpointSpan := finishedSpans[0]
6158 if want, have := "testOp", endpointSpan.OperationName; want != have {
6259 t.Fatalf("Want %q, have %q", want, have)
6360 }
7168 defer parentSpan.Finish()
7269 ctx := opentracing.ContextWithSpan(context.Background(), parentSpan)
7370
74 var innerEndpoint endpoint.Endpoint
75 innerEndpoint = func(context.Context, interface{}) (interface{}, error) {
76 return struct{}{}, nil
77 }
78 tracedEndpoint := kitot.TraceClient(tracer, "testOp")(innerEndpoint)
71 tracedEndpoint := kitot.TraceClient(tracer, "testOp")(endpoint.Nop)
7972 if _, err := tracedEndpoint(ctx, struct{}{}); err != nil {
8073 t.Fatal(err)
8174 }
82 // tracedEndpoint created a new Span:
83 if want, have := 1, len(tracer.FinishedSpans); want != have {
75
76 // tracedEndpoint created a new Span.
77 finishedSpans := tracer.GetFinishedSpans()
78 if want, have := 1, len(finishedSpans); want != have {
8479 t.Fatalf("Want %v span(s), found %v", want, have)
8580 }
8681
87 endpointSpan := tracer.FinishedSpans[0]
82 endpointSpan := finishedSpans[0]
8883 if want, have := "testOp", endpointSpan.OperationName; want != have {
8984 t.Fatalf("Want %q, have %q", want, have)
9085 }
86
9187 // ... and that the parent ID is set appropriately.
9288 if want, have := parentSpan.SpanID, endpointSpan.ParentID; want != have {
9389 t.Errorf("Want ParentID %q, have %q", want, have)
88 "google.golang.org/grpc/metadata"
99
1010 kitot "github.com/go-kit/kit/tracing/opentracing"
11 "github.com/go-kit/kit/transport/grpc"
1211 )
1312
1413 func TestTraceGRPCRequestRoundtrip(t *testing.T) {
2019 beforeSpan.SetBaggageItem("baggage", "check")
2120 beforeCtx := opentracing.ContextWithSpan(context.Background(), beforeSpan)
2221
23 var toGRPCFunc grpc.RequestFunc = kitot.ToGRPCRequest(tracer, nil)
22 toGRPCFunc := kitot.ToGRPCRequest(tracer, nil)
2423 md := metadata.Pairs()
2524 // Call the RequestFunc.
2625 afterCtx := toGRPCFunc(beforeCtx, &md)
3231 }
3332
3433 // No spans should have finished yet.
35 if want, have := 0, len(tracer.FinishedSpans); want != have {
34 finishedSpans := tracer.GetFinishedSpans()
35 if want, have := 0, len(finishedSpans); want != have {
3636 t.Errorf("Want %v span(s), found %v", want, have)
3737 }
3838
3939 // Use FromGRPCRequest to verify that we can join with the trace given MD.
40 var fromGRPCFunc grpc.RequestFunc = kitot.FromGRPCRequest(tracer, "joined", nil)
40 fromGRPCFunc := kitot.FromGRPCRequest(tracer, "joined", nil)
4141 joinCtx := fromGRPCFunc(afterCtx, &md)
4242 joinedSpan := opentracing.SpanFromContext(joinCtx).(*mocktracer.MockSpan)
4343
88 "golang.org/x/net/context"
99
1010 kitot "github.com/go-kit/kit/tracing/opentracing"
11 kithttp "github.com/go-kit/kit/transport/http"
1211 )
1312
1413 func TestTraceHTTPRequestRoundtrip(t *testing.T) {
2019 beforeSpan.SetBaggageItem("baggage", "check")
2120 beforeCtx := opentracing.ContextWithSpan(context.Background(), beforeSpan)
2221
23 var toHTTPFunc kithttp.RequestFunc = kitot.ToHTTPRequest(tracer, nil)
22 toHTTPFunc := kitot.ToHTTPRequest(tracer, nil)
2423 req, _ := http.NewRequest("GET", "http://test.biz/url", nil)
2524 // Call the RequestFunc.
2625 afterCtx := toHTTPFunc(beforeCtx, req)
3231 }
3332
3433 // No spans should have finished yet.
35 if want, have := 0, len(tracer.FinishedSpans); want != have {
34 finishedSpans := tracer.GetFinishedSpans()
35 if want, have := 0, len(finishedSpans); want != have {
3636 t.Errorf("Want %v span(s), found %v", want, have)
3737 }
3838
3939 // Use FromHTTPRequest to verify that we can join with the trace given a req.
40 var fromHTTPFunc kithttp.RequestFunc = kitot.FromHTTPRequest(tracer, "joined", nil)
40 fromHTTPFunc := kitot.FromHTTPRequest(tracer, "joined", nil)
4141 joinCtx := fromHTTPFunc(afterCtx, req)
4242 joinedSpan := opentracing.SpanFromContext(joinCtx).(*mocktracer.MockSpan)
4343