1 package opentracing_test
9 "github.com/opentracing/opentracing-go"
10 "github.com/opentracing/opentracing-go/ext"
11 "github.com/opentracing/opentracing-go/mocktracer"
13 "github.com/go-kit/kit/log"
14 kitot "github.com/go-kit/kit/tracing/opentracing"
17 func TestTraceHTTPRequestRoundtrip(t *testing.T) {
18 logger := log.NewNopLogger()
19 tracer := mocktracer.New()
21 // Initialize the ctx with a Span to inject.
22 beforeSpan := tracer.StartSpan("to_inject").(*mocktracer.MockSpan)
23 defer beforeSpan.Finish()
24 beforeSpan.SetBaggageItem("baggage", "check")
25 beforeCtx := opentracing.ContextWithSpan(context.Background(), beforeSpan)
27 toHTTPFunc := kitot.ContextToHTTP(tracer, logger)
28 req, _ := http.NewRequest("GET", "http://test.biz/path", nil)
29 // Call the RequestFunc.
30 afterCtx := toHTTPFunc(beforeCtx, req)
32 // The Span should not have changed.
33 afterSpan := opentracing.SpanFromContext(afterCtx)
34 if beforeSpan != afterSpan {
35 t.Error("Should not swap in a new span")
38 // No spans should have finished yet.
39 finishedSpans := tracer.FinishedSpans()
40 if want, have := 0, len(finishedSpans); want != have {
41 t.Errorf("Want %v span(s), found %v", want, have)
44 // Use HTTPToContext to verify that we can join with the trace given a req.
45 fromHTTPFunc := kitot.HTTPToContext(tracer, "joined", logger)
46 joinCtx := fromHTTPFunc(afterCtx, req)
47 joinedSpan := opentracing.SpanFromContext(joinCtx).(*mocktracer.MockSpan)
49 joinedContext := joinedSpan.Context().(mocktracer.MockSpanContext)
50 beforeContext := beforeSpan.Context().(mocktracer.MockSpanContext)
52 if joinedContext.SpanID == beforeContext.SpanID {
53 t.Error("SpanID should have changed", joinedContext.SpanID, beforeContext.SpanID)
56 // Check that the parent/child relationship is as expected for the joined span.
57 if want, have := beforeContext.SpanID, joinedSpan.ParentID; want != have {
58 t.Errorf("Want ParentID %q, have %q", want, have)
60 if want, have := "joined", joinedSpan.OperationName; want != have {
61 t.Errorf("Want %q, have %q", want, have)
63 if want, have := "check", joinedSpan.BaggageItem("baggage"); want != have {
64 t.Errorf("Want %q, have %q", want, have)
68 func TestContextToHTTPTags(t *testing.T) {
69 tracer := mocktracer.New()
70 span := tracer.StartSpan("to_inject").(*mocktracer.MockSpan)
72 ctx := opentracing.ContextWithSpan(context.Background(), span)
73 req, _ := http.NewRequest("GET", "http://test.biz/path", nil)
75 kitot.ContextToHTTP(tracer, log.NewNopLogger())(ctx, req)
77 expectedTags := map[string]interface{}{
78 string(ext.HTTPMethod): "GET",
79 string(ext.HTTPUrl): "http://test.biz/path",
80 string(ext.PeerHostname): "test.biz",
82 if !reflect.DeepEqual(expectedTags, span.Tags()) {
83 t.Errorf("Want %q, have %q", expectedTags, span.Tags())
87 func TestHTTPToContextTags(t *testing.T) {
88 tracer := mocktracer.New()
89 parentSpan := tracer.StartSpan("to_extract").(*mocktracer.MockSpan)
90 defer parentSpan.Finish()
91 req, _ := http.NewRequest("GET", "http://test.biz/path", nil)
92 tracer.Inject(parentSpan.Context(), opentracing.TextMap, opentracing.HTTPHeadersCarrier(req.Header))
94 ctx := kitot.HTTPToContext(tracer, "op", log.NewNopLogger())(context.Background(), req)
95 opentracing.SpanFromContext(ctx).Finish()
97 childSpan := tracer.FinishedSpans()[0]
98 expectedTags := map[string]interface{}{
99 string(ext.HTTPMethod): "GET",
100 string(ext.HTTPUrl): "http://test.biz/path",
101 string(ext.SpanKind): ext.SpanKindRPCServerEnum,
103 if !reflect.DeepEqual(expectedTags, childSpan.Tags()) {
104 t.Errorf("Want %q, have %q", expectedTags, childSpan.Tags())
106 if want, have := "op", childSpan.OperationName; want != have {
107 t.Errorf("Want %q, have %q", want, have)