From 5485c3fb9ed080927426da4cb20bf3a8c37705b7 Mon Sep 17 00:00:00 2001 From: Goutham Veeramachaneni Date: Wed, 3 Oct 2018 16:45:28 +0530 Subject: [PATCH] Label opentracing op-names using routes declared Signed-off-by: Goutham Veeramachaneni --- Gopkg.lock | 4 ++-- .../weaveworks/common/middleware/http_tracing.go | 16 ++++++++++++++-- .../weaveworks/common/middleware/instrument.go | 14 ++++++++++++-- .../weaveworks/common/server/server.go | 4 +++- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 6eeb8462af..f35732327e 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -936,7 +936,7 @@ [[projects]] branch = "master" - digest = "1:cb82650acf4059a7a4ea11a3206fedc6315564dc93ef53e7cb98fb28a956ede2" + digest = "1:6e4a66a9a768fc71e492bec9fafc4c387698a559af45087c731b25b1599497d3" name = "github.com/weaveworks/common" packages = [ "aws", @@ -956,7 +956,7 @@ "user", ] pruneopts = "UT" - revision = "2a0cb6145a2f541de5ca19437fee675baf6a1985" + revision = "e017e7c69cfdc53d5d3bbd4115eb84aa8a180a82" [[projects]] digest = "1:efac30de93ca1ff38050f46dc34f1338ebc8778de488f919f79ad9e6188719d3" diff --git a/vendor/github.com/weaveworks/common/middleware/http_tracing.go b/vendor/github.com/weaveworks/common/middleware/http_tracing.go index f267623532..8f620aeec9 100644 --- a/vendor/github.com/weaveworks/common/middleware/http_tracing.go +++ b/vendor/github.com/weaveworks/common/middleware/http_tracing.go @@ -1,6 +1,7 @@ package middleware import ( + "fmt" "net/http" "github.com/opentracing-contrib/go-stdlib/nethttp" @@ -10,11 +11,22 @@ import ( ) // Tracer is a middleware which traces incoming requests. -type Tracer struct{} +type Tracer struct { + RouteMatcher RouteMatcher +} // Wrap implements Interface func (t Tracer) Wrap(next http.Handler) http.Handler { - traceHandler := nethttp.Middleware(opentracing.GlobalTracer(), next) + 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/vendor/github.com/weaveworks/common/middleware/instrument.go b/vendor/github.com/weaveworks/common/middleware/instrument.go index a88b3a17f4..ac4edc71b8 100644 --- a/vendor/github.com/weaveworks/common/middleware/instrument.go +++ b/vendor/github.com/weaveworks/common/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/vendor/github.com/weaveworks/common/server/server.go b/vendor/github.com/weaveworks/common/server/server.go index ed11c4ac38..e252f81ca3 100644 --- a/vendor/github.com/weaveworks/common/server/server.go +++ b/vendor/github.com/weaveworks/common/server/server.go @@ -139,7 +139,9 @@ func New(cfg Config) (*Server, error) { RegisterInstrumentation(router) } httpMiddleware := []middleware.Interface{ - middleware.Tracer{}, + middleware.Tracer{ + RouteMatcher: router, + }, middleware.Log{ Log: log, },