Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Commit

Permalink
Make no propagation the default for ochttp.Handler
Browse files Browse the repository at this point in the history
By default, when no propagation formats are specificed, make Handler
not read any headers. Previously, we assumed B3 by default, but this
does not give the user any way to use the plugin but disable propagation
(for example if they directly expost the handler on a public-facing server).
  • Loading branch information
Ramon Nogueira committed Mar 12, 2018
1 parent ac82455 commit f883749
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
5 changes: 4 additions & 1 deletion plugin/ochttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package ochttp
import (
"net/http"

"go.opencensus.io/plugin/ochttp/propagation/b3"
"go.opencensus.io/trace"
"go.opencensus.io/trace/propagation"
)
Expand Down Expand Up @@ -50,14 +51,16 @@ type Transport struct {
// TODO: Implement tag propagation for HTTP.
}

var defaultClientFormat propagation.HTTPFormat = &b3.HTTPFormat{}

// RoundTrip implements http.RoundTripper, delegating to Base and recording stats and traces for the request.
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
rt := t.base()
// TODO: remove excessive nesting of http.RoundTrippers here.
if !t.NoTrace {
format := t.Propagation
if format == nil {
format = defaultFormat
format = defaultClientFormat
}
rt = &traceTransport{
base: rt,
Expand Down
11 changes: 8 additions & 3 deletions plugin/ochttp/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ func ExampleTransport() {
}

client := &http.Client{
Transport: &ochttp.Transport{},
Transport: &ochttp.Transport{
Propagation: &b3.HTTPFormat{},
},
}
_ = client // use client to perform requests
}
Expand All @@ -52,9 +54,12 @@ var usersHandler http.Handler

func ExampleHandler() {
// Enables OpenCensus for the default serve mux.
// By default, B3 propagation is used.
http.Handle("/users", usersHandler)
log.Fatal(http.ListenAndServe("localhost:8080", &ochttp.Handler{}))
log.Fatal(http.ListenAndServe("localhost:8080", &ochttp.Handler{
// Specify a propagation format; without this, a new root span will be
// started for each request:
Propagation: &b3.HTTPFormat{},
}))
}

func ExampleHandler_mux() {
Expand Down
26 changes: 19 additions & 7 deletions plugin/ochttp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,16 @@ type Handler struct {
// NoTrace may be set to disable recording of traces.
NoTrace bool

// Propagation defines how traces are propagated. If unspecified,
// B3 propagation will be used.
// Propagation defines the header convention used to read tracing information
// from incoming requests.
//
// If not specified, no tracing information will be read from the incoming
// request. In this case, a new root span will be created around the
// server-side processing of each request if the Sampler samples this request.
//
// You should only set this to a non-default value if you trust the caller
// of this service. For example, this is safe if the current service will
// only be called by other services that you control.
Propagation propagation.HTTPFormat

// Handler is the handler used to handle the incoming request.
Expand Down Expand Up @@ -73,14 +81,11 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

func (h *Handler) startTrace(w http.ResponseWriter, r *http.Request) (*http.Request, func()) {
name := spanNameFromURL("Recv", r.URL)
p := h.Propagation
if p == nil {
p = defaultFormat
}
ctx := r.Context()
var span *trace.Span
if sc, ok := p.SpanContextFromRequest(r); ok {
if sc, ok := h.extractSpanContext(r); ok {
span = trace.NewSpanWithRemoteParent(name, sc, trace.StartOptions{})
ctx = trace.WithSpan(ctx, span)
} else {
span = trace.NewSpan(name, nil, trace.StartOptions{})
}
Expand All @@ -89,6 +94,13 @@ func (h *Handler) startTrace(w http.ResponseWriter, r *http.Request) (*http.Requ
return r.WithContext(trace.WithSpan(r.Context(), span)), span.End
}

func (h *Handler) extractSpanContext(r *http.Request) (trace.SpanContext, bool) {
if h.Propagation == nil {
return trace.SpanContext{}, false
}
return h.Propagation.SpanContextFromRequest(r)
}

func (h *Handler) startStats(w http.ResponseWriter, r *http.Request) (http.ResponseWriter, func()) {
ctx, _ := tag.New(r.Context(),
tag.Upsert(Host, r.URL.Host),
Expand Down
3 changes: 0 additions & 3 deletions plugin/ochttp/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@ import (
"net/url"
"sync"

"go.opencensus.io/plugin/ochttp/propagation/b3"
"go.opencensus.io/trace"
"go.opencensus.io/trace/propagation"
)

// TODO(jbd): Add godoc examples.

var defaultFormat propagation.HTTPFormat = &b3.HTTPFormat{}

// Attributes recorded on the span for the requests.
// Only trace exporters will need them.
const (
Expand Down
4 changes: 3 additions & 1 deletion plugin/ochttp/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"testing"
"time"

"go.opencensus.io/plugin/ochttp/propagation/b3"
"go.opencensus.io/trace"
)

Expand Down Expand Up @@ -202,7 +203,7 @@ func TestEndToEnd(t *testing.T) {

rt := &Transport{
NoStats: true,
Propagation: defaultFormat,
Propagation: &b3.HTTPFormat{},
Base: http.DefaultTransport,
}
resp, err := rt.RoundTrip(req)
Expand Down Expand Up @@ -283,6 +284,7 @@ func serveHTTP(done chan struct{}, wait chan time.Time) string {
io.WriteString(w, "expected-response")
close(done)
}),
Propagation: &b3.HTTPFormat{},
}

server := httptest.NewServer(handler)
Expand Down

0 comments on commit f883749

Please sign in to comment.