Skip to content

Commit

Permalink
[propagator] Add injection integration test.
Browse files Browse the repository at this point in the history
  • Loading branch information
TommyCpp committed Sep 30, 2020
1 parent dccda7c commit ea8088a
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 5 deletions.
74 changes: 74 additions & 0 deletions propagators/jaeger/jaeger_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,78 @@ var invalidExtractHeaders = []extractTest{
jaegerHeader + "not": fmt.Sprintf("%s:%s:0:1", traceID32Str, spanIDStr),
},
},
{
name: "empty header value",
headers: map[string]string{
jaegerHeader: "",
},
},
}

type injectTest struct {
name string
sc trace.SpanContext
wantHeaders map[string]string
}

var injectHeaders = []injectTest{
{
name: "sampled",
sc: trace.SpanContext{
TraceID: traceID,
SpanID: spanID,
TraceFlags: trace.FlagsSampled,
},
wantHeaders: map[string]string{
jaegerHeader: fmt.Sprintf("%s:%s:0:1", traceID32Str, spanIDStr),
},
},
{
name: "debug",
sc: trace.SpanContext{
TraceID: traceID,
SpanID: spanID,
TraceFlags: trace.FlagsSampled | trace.FlagsDebug,
},
wantHeaders: map[string]string{
jaegerHeader: fmt.Sprintf("%s:%s:0:3", traceID32Str, spanIDStr),
},
},
{
name: "not sampled",
sc: trace.SpanContext{
TraceID: traceID,
SpanID: spanID,
},
wantHeaders: map[string]string{
jaegerHeader: fmt.Sprintf("%s:%s:0:0", traceID32Str, spanIDStr),
},
},
}

var invalidInjectHeaders = []injectTest{
{
name: "empty",
sc: trace.SpanContext{},
},
{
name: "missing traceID",
sc: trace.SpanContext{
SpanID: spanID,
TraceFlags: trace.FlagsSampled,
},
},
{
name: "missing spanID",
sc: trace.SpanContext{
TraceID: traceID,
TraceFlags: trace.FlagsSampled,
},
},
{
name: "missing both traceID and spanID",
sc: trace.SpanContext{
TraceFlags: trace.FlagsSampled,
},
},
}
55 changes: 55 additions & 0 deletions propagators/jaeger/jaeger_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ import (

"github.com/google/go-cmp/cmp"

mocktracer "go.opentelemetry.io/contrib/internal/trace"
"go.opentelemetry.io/contrib/propagators/jaeger"
"go.opentelemetry.io/otel/api/propagation"
"go.opentelemetry.io/otel/api/trace"
)

var (
mockTracer = mocktracer.NewTracer("")
_, mockSpan = mockTracer.Start(context.Background(), "")
)

func TestExtractJaeger(t *testing.T) {
testGroup := []struct {
name string
Expand Down Expand Up @@ -62,3 +68,52 @@ func TestExtractJaeger(t *testing.T) {
}
}
}

type testSpan struct {
trace.Span
sc trace.SpanContext
}

func (s testSpan) SpanContext() trace.SpanContext {
return s.sc
}

func TestInjectJaeger(t *testing.T) {
testGroup := []struct {
name string
testcases []injectTest
}{
{
name: "valid test case",
testcases: injectHeaders,
},
{
name: "invalid test case",
testcases: invalidInjectHeaders,
},
}

for _, tg := range testGroup {
for _, tc := range tg.testcases {
propagator := jaeger.Jaeger{}
t.Run(tc.name, func(t *testing.T) {
req, _ := http.NewRequest("GET", "http://example.com", nil)
ctx := trace.ContextWithSpan(
context.Background(),
testSpan{
Span: mockSpan,
sc: tc.sc,
},
)
propagator.Inject(ctx, req.Header)

for h, v := range tc.wantHeaders {
result, want := req.Header.Get(h), v
if diff := cmp.Diff(result, want); diff != "" {
t.Errorf("%s: %s, header=%s: -got +want %s", tg.name, tc.name, h, diff)
}
}
})
}
}
}
8 changes: 3 additions & 5 deletions propagators/jaeger/jaeger_propagator.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ func (jaeger Jaeger) Inject(ctx context.Context, supplier propagation.HTTPSuppli
headers = append(headers, fmt.Sprintf("%x", flagsNotSampled))
}

supplier.Set(jaegerHeader, strings.Join(headers, separator))
if len(headers) == 4 {
supplier.Set(jaegerHeader, strings.Join(headers, separator))
}
}

func (jaeger Jaeger) Extract(ctx context.Context, supplier propagation.HTTPSupplier) context.Context {
Expand All @@ -89,10 +91,6 @@ func (jaeger Jaeger) Extract(ctx context.Context, supplier propagation.HTTPSuppl
}

func extract(headerVal string) (trace.SpanContext, error) {
if headerVal == "" {
return empty, errEmptyContext
}

var (
sc = trace.SpanContext{}
err error
Expand Down

0 comments on commit ea8088a

Please sign in to comment.