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 a30f351 commit 124d940
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 2 deletions.
69 changes: 68 additions & 1 deletion propagators/jaeger/jaeger_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package jaeger_test

import (
"fmt"

"go.opentelemetry.io/otel/api/trace"
)

Expand Down Expand Up @@ -193,3 +192,71 @@ var invalidExtractHeaders = []extractTest{
},
},
}

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)
}
}
})
}
}
}
4 changes: 3 additions & 1 deletion 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 Down

0 comments on commit 124d940

Please sign in to comment.