Skip to content

Commit

Permalink
Add tests for the trace.Inject function.
Browse files Browse the repository at this point in the history
  • Loading branch information
austinhartzheim committed Mar 27, 2018
1 parent 628c3af commit 2e95e4b
Showing 1 changed file with 43 additions and 32 deletions.
75 changes: 43 additions & 32 deletions trace/trace_test.go
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit 2e95e4b

Please sign in to comment.