Skip to content

Commit

Permalink
Add InjectSpanContext ClientOption. (#44)
Browse files Browse the repository at this point in the history
* Add InjectSpanContext ClientOption. Fixes #43.

* Document default behaviour.

* Add TestInjectSpanContext

* Fix doc
  • Loading branch information
nvx authored and yurishkuro committed May 19, 2019
1 parent 2b2d270 commit cf7a6c9
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 6 deletions.
26 changes: 20 additions & 6 deletions nethttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ type Transport struct {
}

type clientOptions struct {
operationName string
componentName string
disableClientTrace bool
spanObserver func(span opentracing.Span, r *http.Request)
operationName string
componentName string
disableClientTrace bool
disableInjectSpanContext bool
spanObserver func(span opentracing.Span, r *http.Request)
}

// ClientOption contols the behavior of TraceRequest.
Expand Down Expand Up @@ -64,6 +65,16 @@ func ClientTrace(enabled bool) ClientOption {
}
}

// InjectSpanContext returns a ClientOption that turns on or off
// injection of the Span context in the request HTTP headers.
// If this option is not used, the default behaviour is to
// inject the span context.
func InjectSpanContext(enabled bool) ClientOption {
return func(options *clientOptions) {
options.disableInjectSpanContext = !enabled
}
}

// ClientSpanObserver returns a ClientOption that observes the span
// for the client-side span.
func ClientSpanObserver(f func(span opentracing.Span, r *http.Request)) ClientOption {
Expand Down Expand Up @@ -151,8 +162,11 @@ func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
ext.HTTPUrl.Set(tracer.sp, req.URL.String())
tracer.opts.spanObserver(tracer.sp, req)

carrier := opentracing.HTTPHeadersCarrier(req.Header)
tracer.sp.Tracer().Inject(tracer.sp.Context(), opentracing.HTTPHeaders, carrier)
if !tracer.opts.disableInjectSpanContext {
carrier := opentracing.HTTPHeadersCarrier(req.Header)
tracer.sp.Tracer().Inject(tracer.sp.Context(), opentracing.HTTPHeaders, carrier)
}

resp, err := rt.RoundTrip(req)

if err != nil {
Expand Down
66 changes: 66 additions & 0 deletions nethttp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,72 @@ func TestTracerFromRequest(t *testing.T) {
}
}

func TestInjectSpanContext(t *testing.T) {
tests := []struct {
name string
expectContextPropagation bool
opts []ClientOption
}{
{name: "Default", expectContextPropagation: true, opts: nil},
{name: "True", expectContextPropagation: true, opts: []ClientOption{InjectSpanContext(true)}},
{name: "False", expectContextPropagation: false, opts: []ClientOption{InjectSpanContext(false)}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var handlerCalled bool
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handlerCalled = true
srvTr := mocktracer.New()
ctx, err := srvTr.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(r.Header))

if err != nil && tt.expectContextPropagation {
t.Fatal(err)
}

if tt.expectContextPropagation {
if err != nil || ctx == nil {
t.Fatal("expected propagation but unable to extract")
}
} else {
// Expect "opentracing: SpanContext not found in Extract carrier" when not injected
// Can't check ctx directly, because it gets set to emptyContext
if err == nil {
t.Fatal("unexpected propagation")
}
}
}))

tr := mocktracer.New()
span := tr.StartSpan("root")

req, err := http.NewRequest("GET", srv.URL, nil)
if err != nil {
t.Fatal(err)
}
req = req.WithContext(opentracing.ContextWithSpan(req.Context(), span))

req, ht := TraceRequest(tr, req, tt.opts...)

client := &http.Client{Transport: &Transport{}}
resp, err := client.Do(req)
if err != nil {
t.Fatal(err)
}
_ = resp.Body.Close()

ht.Finish()
span.Finish()

srv.Close()

if !handlerCalled {
t.Fatal("server handler never called")
}
})
}
}

func makeTags(keyVals ...interface{}) map[string]interface{} {
result := make(map[string]interface{}, len(keyVals)/2)
for i := 0; i < len(keyVals)-1; i += 2 {
Expand Down

0 comments on commit cf7a6c9

Please sign in to comment.