From e55de3be72a3773902fc72c0470dcad207ddc5d0 Mon Sep 17 00:00:00 2001 From: Goutham Veeramachaneni Date: Fri, 14 Sep 2018 15:30:27 +0530 Subject: [PATCH 1/2] Pass options to tracing middleware Signed-off-by: Goutham Veeramachaneni --- middleware/http_tracing.go | 6 ++++-- server/server.go | 7 ++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/middleware/http_tracing.go b/middleware/http_tracing.go index f2676235..f482e433 100644 --- a/middleware/http_tracing.go +++ b/middleware/http_tracing.go @@ -10,11 +10,13 @@ import ( ) // Tracer is a middleware which traces incoming requests. -type Tracer struct{} +type Tracer struct { + Options []nethttp.MWOption +} // Wrap implements Interface func (t Tracer) Wrap(next http.Handler) http.Handler { - traceHandler := nethttp.Middleware(opentracing.GlobalTracer(), next) + traceHandler := nethttp.Middleware(opentracing.GlobalTracer(), next, t.Options...) return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var maybeTracer http.Handler // Don't try and trace websocket requests because nethttp.Middleware diff --git a/server/server.go b/server/server.go index ed11c4ac..d4554f8d 100644 --- a/server/server.go +++ b/server/server.go @@ -8,6 +8,8 @@ import ( _ "net/http/pprof" // anonymous import to get the pprof handler registered "time" + "github.com/opentracing-contrib/go-stdlib/nethttp" + "github.com/gorilla/mux" "github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc" "github.com/mwitkow/go-grpc-middleware" @@ -42,6 +44,7 @@ type Config struct { GRPCMiddleware []grpc.UnaryServerInterceptor GRPCStreamMiddleware []grpc.StreamServerInterceptor HTTPMiddleware []middleware.Interface + OpentracingOptions []nethttp.MWOption LogLevel logging.Level Log logging.Interface @@ -139,7 +142,9 @@ func New(cfg Config) (*Server, error) { RegisterInstrumentation(router) } httpMiddleware := []middleware.Interface{ - middleware.Tracer{}, + middleware.Tracer{ + Options: cfg.OpentracingOptions, + }, middleware.Log{ Log: log, }, From f91ca84cce4e400559b0cb1f53ccbd52d3fc3bdc Mon Sep 17 00:00:00 2001 From: Goutham Veeramachaneni Date: Fri, 14 Sep 2018 19:14:40 +0530 Subject: [PATCH 2/2] Use the route matcher instead Signed-off-by: Goutham Veeramachaneni --- middleware/http_tracing.go | 14 ++++++++++++-- middleware/instrument.go | 14 ++++++++++++-- server/server.go | 5 +---- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/middleware/http_tracing.go b/middleware/http_tracing.go index f482e433..8f620aee 100644 --- a/middleware/http_tracing.go +++ b/middleware/http_tracing.go @@ -1,6 +1,7 @@ package middleware import ( + "fmt" "net/http" "github.com/opentracing-contrib/go-stdlib/nethttp" @@ -11,12 +12,21 @@ import ( // Tracer is a middleware which traces incoming requests. type Tracer struct { - Options []nethttp.MWOption + RouteMatcher RouteMatcher } // Wrap implements Interface func (t Tracer) Wrap(next http.Handler) http.Handler { - traceHandler := nethttp.Middleware(opentracing.GlobalTracer(), next, t.Options...) + opMatcher := nethttp.OperationNameFunc(func(r *http.Request) string { + op := getRouteName(t.RouteMatcher, r) + if op == "" { + return "HTTP " + r.Method + } + + return fmt.Sprintf("HTTP %s - %s", r.Method, op) + }) + + traceHandler := nethttp.Middleware(opentracing.GlobalTracer(), next, opMatcher) return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var maybeTracer http.Handler // Don't try and trace websocket requests because nethttp.Middleware diff --git a/middleware/instrument.go b/middleware/instrument.go index a88b3a17..ac4edc71 100644 --- a/middleware/instrument.go +++ b/middleware/instrument.go @@ -64,8 +64,17 @@ func (i Instrument) Wrap(next http.Handler) http.Handler { // We do all this as we do not wish to emit high cardinality labels to // prometheus. func (i Instrument) getRouteName(r *http.Request) string { + route := getRouteName(i.RouteMatcher, r) + if route == "" { + route = "other" + } + + return route +} + +func getRouteName(routeMatcher RouteMatcher, r *http.Request) string { var routeMatch mux.RouteMatch - if i.RouteMatcher != nil && i.RouteMatcher.Match(r, &routeMatch) { + if routeMatcher != nil && routeMatcher.Match(r, &routeMatch) { if name := routeMatch.Route.GetName(); name != "" { return name } @@ -73,7 +82,8 @@ func (i Instrument) getRouteName(r *http.Request) string { return MakeLabelValue(tmpl) } } - return "other" + + return "" } var invalidChars = regexp.MustCompile(`[^a-zA-Z0-9]+`) diff --git a/server/server.go b/server/server.go index d4554f8d..e252f81c 100644 --- a/server/server.go +++ b/server/server.go @@ -8,8 +8,6 @@ import ( _ "net/http/pprof" // anonymous import to get the pprof handler registered "time" - "github.com/opentracing-contrib/go-stdlib/nethttp" - "github.com/gorilla/mux" "github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc" "github.com/mwitkow/go-grpc-middleware" @@ -44,7 +42,6 @@ type Config struct { GRPCMiddleware []grpc.UnaryServerInterceptor GRPCStreamMiddleware []grpc.StreamServerInterceptor HTTPMiddleware []middleware.Interface - OpentracingOptions []nethttp.MWOption LogLevel logging.Level Log logging.Interface @@ -143,7 +140,7 @@ func New(cfg Config) (*Server, error) { } httpMiddleware := []middleware.Interface{ middleware.Tracer{ - Options: cfg.OpentracingOptions, + RouteMatcher: router, }, middleware.Log{ Log: log,