Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
gabsn committed Aug 17, 2017
1 parent 7a4d0fe commit 7a84995
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
5 changes: 2 additions & 3 deletions libs/net/http/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
}

func Example() {
mux := http.NewServeMux()
mux := httptrace.NewServeMux("web-service", nil)
mux.HandleFunc("/", handler)

http.ListenAndServe(":8080", httptrace.NewDefaultHandler(mux, "web-service"))
http.ListenAndServe(":8080", mux)
}
4 changes: 3 additions & 1 deletion libs/net/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
"github.com/DataDog/dd-trace-go/tracer/ext"
)

// ServeMux is an HTTP request multiplexer.
// ServeMux is an HTTP request multiplexer that traces all the incoming requests.
type ServeMux struct {
*http.ServeMux
*tracer.Tracer
service string
}

// NewServeMux allocates and returns a new ServeMux.
func NewServeMux(service string, t *tracer.Tracer) *ServeMux {
if t == nil {
t = tracer.DefaultTracer
Expand All @@ -25,6 +26,7 @@ func NewServeMux(service string, t *tracer.Tracer) *ServeMux {

// ServeHTTP dispatches the request to the handler whose
// pattern most closely matches the request URL.
// We only needed to rewrite this method to be able to trace the multiplexer.
func (mux *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// bail out if tracing isn't enabled
if !mux.Tracer.Enabled() {
Expand Down
20 changes: 9 additions & 11 deletions libs/net/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import (
func TestHttpTracerDisabled(t *testing.T) {
assert := assert.New(t)

mux := http.NewServeMux()
testTracer, testTransport := tracertest.GetTestTracer()
testTracer.SetEnabled(false)

mux := NewServeMux("my-service", testTracer)
mux.HandleFunc("/disabled", func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("disabled!"))
assert.Nil(err)
Expand All @@ -24,14 +27,10 @@ func TestHttpTracerDisabled(t *testing.T) {
assert.False(ok)
})

testTracer, testTransport := tracertest.GetTestTracer()
testTracer.SetEnabled(false)
handler := NewHandler(mux, "service", "", testTracer)

// Make the request
r := httptest.NewRequest("GET", "/disabled", nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
mux.ServeHTTP(w, r)
assert.Equal(200, w.Code)
assert.Equal("disabled!", w.Body.String())

Expand Down Expand Up @@ -102,14 +101,13 @@ func TestHttpTracer500(t *testing.T) {
func setup(t *testing.T) (*tracer.Tracer, *tracertest.DummyTransport, http.Handler) {
h200 := handler200(t)
h500 := handler500(t)
mux := http.NewServeMux()
tracer, transport := tracertest.GetTestTracer()

mux := NewServeMux("my-service", tracer)
mux.HandleFunc("/200", h200)
mux.HandleFunc("/500", h500)

tracer, transport := tracertest.GetTestTracer()
traceHandler := NewHandler(mux, "my-service", "", tracer)

return tracer, transport, traceHandler
return tracer, transport, mux
}

func handler200(t *testing.T) http.HandlerFunc {
Expand Down

0 comments on commit 7a84995

Please sign in to comment.