Skip to content

Commit

Permalink
Add middleware to detect trace context on HTTP
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Cobden committed Aug 23, 2018
1 parent 3431637 commit c452f31
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions prog/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"time"

"github.com/gorilla/mux"
"github.com/opentracing-contrib/go-stdlib/nethttp"
"github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"github.com/tylerb/graceful"
Expand Down Expand Up @@ -70,11 +72,27 @@ func router(collector app.Collector, controlRouter app.ControlRouter, pipeRouter
uiHandler))
router.PathPrefix("/").Name("static").Handler(uiHandler)

instrument := middleware.Instrument{
instrumentDuration := middleware.Instrument{
RouteMatcher: router,
Duration: requestDuration,
}
return instrument.Wrap(router)
instrumentTraces := middleware.Func(func(next http.Handler) http.Handler {
return nethttp.Middleware(opentracing.GlobalTracer(), next)
})

durationInstrumented := instrumentDuration.Wrap(router)
traceInstrumented := instrumentTraces.Wrap(durationInstrumented)
// Don't try and trace websocket requests because nethttp.Middleware
// doesn't support http.Hijack
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var next http.Handler
if middleware.IsWSHandshakeRequest(r) {
next = durationInstrumented
} else {
next = traceInstrumented
}
next.ServeHTTP(w, r)
})
}

func collectorFactory(userIDer multitenant.UserIDer, collectorURL, s3URL, natsHostname string,
Expand Down

0 comments on commit c452f31

Please sign in to comment.