diff --git a/trace/trace_test.go b/trace/trace_test.go index 46cffdc0f..15eefe784 100644 --- a/trace/trace_test.go +++ b/trace/trace_test.go @@ -1,54 +1,65 @@ package trace import ( - opentracing "github.com/opentracing/opentracing-go" - "github.com/opentracing/opentracing-go/ext" - mocktracer "github.com/opentracing/opentracing-go/mocktracer" "net/http" "testing" + + opentracing "github.com/opentracing/opentracing-go" + mocktracer "github.com/opentracing/opentracing-go/mocktracer" + zipkin "github.com/openzipkin/zipkin-go-opentracing" + zipkintypes "github.com/openzipkin/zipkin-go-opentracing/types" ) -func mimicTracerInject(req *http.Request) { - // TODO maybe replace this will a call to opentracing.GlobalTracer().Inject() - req.Header.Add("X-B3-TraceId", "1234562345678") - req.Header.Add("X-B3-SpanId", "123456789") - req.Header.Add("X-B3-ParentSpanId", "123456789") - req.Header.Add("X-B3-Flags", "1") -} +const testServiceName = "TEST-SERVICE" -// go test -v ./trace func TestInjectHeaders(t *testing.T) { - serviceName := "TESTING" - - req, err := http.NewRequest("GET", "http://example.com", nil) - if err != nil { - t.Error("Error when creating new request.") - t.Fail() - } - mimicTracerInject(req) - mt := mocktracer.New() opentracing.SetGlobalTracer(mt) globalTracer := opentracing.GlobalTracer() + span := globalTracer.StartSpan(testServiceName) - var span opentracing.Span - if globalTracer != nil { - spanCtx, err := globalTracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header)) - if err != nil { - span = globalTracer.StartSpan(serviceName) - } else { - span = globalTracer.StartSpan(serviceName, ext.RPCServerOption(spanCtx)) - } + req, _ := http.NewRequest("GET", "http://example.com", nil) + InjectHeaders(span, req) + + if req.Header.Get("Mockpfx-Ids-Traceid") == "" { + t.Error("Inject did not set the Traceid in the request.") + t.Fail() } + if req.Header.Get("Mockpfx-Ids-Spanid") == "" { + t.Error("Inject did not set the Spanid in the request.") + t.Fail() + } +} - InjectHeaders(span, req) +func TestInjectHeadersWithParentSpan(t *testing.T) { + parentSpanId := uint64(12345) + parentSpanContext := zipkin.SpanContext{ + SpanID: parentSpanId, + TraceID: zipkintypes.TraceID{High: uint64(1234), Low: uint64(4321)}, + } + + tracer, _ := zipkin.NewTracer(nil) + opentracing.SetGlobalTracer(tracer) + globalTracer := opentracing.GlobalTracer() + childSpan := globalTracer.StartSpan(testServiceName+"-CHILD", opentracing.ChildOf(parentSpanContext)) + + req, _ := http.NewRequest("GET", "http://example.com", nil) + InjectHeaders(childSpan, req) if req.Header.Get("X-B3-Traceid") == "" { - t.Error("Zipkin headers not set in request.") + t.Error("Inject did not set the Traceid in the request.") + t.Fail() + } + if req.Header.Get("X-B3-Spanid") == "" { + t.Error("Inject did not set the Spanid in the request.") + t.Fail() + } + if req.Header.Get("X-B3-Parentspanid") != "0000000000003039" { + t.Error("Inject did not set the correct Parentspanid in the request.") t.Fail() } - if req.Header.Get("X-B3-Traceid") != "1234562345678" { - t.Error("Zipkin headers do not match the values set.") + if req.Header.Get("x-B3-Traceid") != "00000000000004d200000000000010e1" { + t.Error("Inject did not reuse the Traceid from the parent span") t.Fail() } }