Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTTP tracing middleware #119

Merged
merged 1 commit into from
Aug 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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