Skip to content

Commit

Permalink
Add HTTP tracing middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Cobden committed Aug 23, 2018
1 parent 80ff076 commit efd9930
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
27 changes: 27 additions & 0 deletions middleware/http_tracing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package middleware

import (
"net/http"

"github.com/opentracing-contrib/go-stdlib/nethttp"
"github.com/opentracing/opentracing-go"
)

// Tracer is a middleware which traces incoming requests.
type Tracer struct{}

// Wrap implements Interface
func (t Tracer) Wrap(next http.Handler) http.Handler {
traceHandler := nethttp.Middleware(opentracing.GlobalTracer(), next)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var maybeTracer http.Handler
// Don't try and trace websocket requests because nethttp.Middleware
// doesn't support http.Hijack yet
if IsWSHandshakeRequest(r) {
maybeTracer = next
} else {
maybeTracer = traceHandler
}
maybeTracer.ServeHTTP(w, r)
})
}
5 changes: 1 addition & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc"
"github.com/mwitkow/go-grpc-middleware"
"github.com/opentracing-contrib/go-stdlib/nethttp"
"github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/net/context"
Expand Down Expand Up @@ -147,9 +146,7 @@ func New(cfg Config) (*Server, error) {
Duration: requestDuration,
RouteMatcher: router,
},
middleware.Func(func(handler http.Handler) http.Handler {
return nethttp.Middleware(opentracing.GlobalTracer(), handler)
}),
middleware.Tracer{},
}
httpMiddleware = append(httpMiddleware, cfg.HTTPMiddleware...)
httpServer := &http.Server{
Expand Down

0 comments on commit efd9930

Please sign in to comment.