1 package opentracing_test
7 "github.com/opentracing/opentracing-go"
8 "github.com/opentracing/opentracing-go/mocktracer"
10 "github.com/go-kit/kit/endpoint"
11 kitot "github.com/go-kit/kit/tracing/opentracing"
14 func TestTraceServer(t *testing.T) {
15 tracer := mocktracer.New()
17 // Initialize the ctx with a nameless Span.
18 contextSpan := tracer.StartSpan("").(*mocktracer.MockSpan)
19 ctx := opentracing.ContextWithSpan(context.Background(), contextSpan)
21 tracedEndpoint := kitot.TraceServer(tracer, "testOp")(endpoint.Nop)
22 if _, err := tracedEndpoint(ctx, struct{}{}); err != nil {
26 finishedSpans := tracer.FinishedSpans()
27 if want, have := 1, len(finishedSpans); want != have {
28 t.Fatalf("Want %v span(s), found %v", want, have)
31 // Test that the op name is updated
32 endpointSpan := finishedSpans[0]
33 if want, have := "testOp", endpointSpan.OperationName; want != have {
34 t.Fatalf("Want %q, have %q", want, have)
36 contextContext := contextSpan.Context().(mocktracer.MockSpanContext)
37 endpointContext := endpointSpan.Context().(mocktracer.MockSpanContext)
38 // ...and that the ID is unmodified.
39 if want, have := contextContext.SpanID, endpointContext.SpanID; want != have {
40 t.Errorf("Want SpanID %q, have %q", want, have)
44 func TestTraceServerNoContextSpan(t *testing.T) {
45 tracer := mocktracer.New()
47 // Empty/background context.
48 tracedEndpoint := kitot.TraceServer(tracer, "testOp")(endpoint.Nop)
49 if _, err := tracedEndpoint(context.Background(), struct{}{}); err != nil {
53 // tracedEndpoint created a new Span.
54 finishedSpans := tracer.FinishedSpans()
55 if want, have := 1, len(finishedSpans); want != have {
56 t.Fatalf("Want %v span(s), found %v", want, have)
59 endpointSpan := finishedSpans[0]
60 if want, have := "testOp", endpointSpan.OperationName; want != have {
61 t.Fatalf("Want %q, have %q", want, have)
65 func TestTraceClient(t *testing.T) {
66 tracer := mocktracer.New()
68 // Initialize the ctx with a parent Span.
69 parentSpan := tracer.StartSpan("parent").(*mocktracer.MockSpan)
70 defer parentSpan.Finish()
71 ctx := opentracing.ContextWithSpan(context.Background(), parentSpan)
73 tracedEndpoint := kitot.TraceClient(tracer, "testOp")(endpoint.Nop)
74 if _, err := tracedEndpoint(ctx, struct{}{}); err != nil {
78 // tracedEndpoint created a new Span.
79 finishedSpans := tracer.FinishedSpans()
80 if want, have := 1, len(finishedSpans); want != have {
81 t.Fatalf("Want %v span(s), found %v", want, have)
84 endpointSpan := finishedSpans[0]
85 if want, have := "testOp", endpointSpan.OperationName; want != have {
86 t.Fatalf("Want %q, have %q", want, have)
89 parentContext := parentSpan.Context().(mocktracer.MockSpanContext)
90 endpointContext := parentSpan.Context().(mocktracer.MockSpanContext)
92 // ... and that the parent ID is set appropriately.
93 if want, have := parentContext.SpanID, endpointContext.SpanID; want != have {
94 t.Errorf("Want ParentID %q, have %q", want, have)
98 func TestTraceClientNoContextSpan(t *testing.T) {
99 tracer := mocktracer.New()
101 // Empty/background context.
102 tracedEndpoint := kitot.TraceClient(tracer, "testOp")(endpoint.Nop)
103 if _, err := tracedEndpoint(context.Background(), struct{}{}); err != nil {
107 // tracedEndpoint created a new Span.
108 finishedSpans := tracer.FinishedSpans()
109 if want, have := 1, len(finishedSpans); want != have {
110 t.Fatalf("Want %v span(s), found %v", want, have)
113 endpointSpan := finishedSpans[0]
114 if want, have := "testOp", endpointSpan.OperationName; want != have {
115 t.Fatalf("Want %q, have %q", want, have)