You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by ra...@apache.org on 2020/05/18 15:44:00 UTC

[trafficcontrol] branch master updated: Changes to add route id in access logs (#4696)

This is an automated email from the ASF dual-hosted git repository.

rawlin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new 91871ef  Changes to add route id in access logs (#4696)
91871ef is described below

commit 91871efbf15a330c674e335a1181c02084d1ccc6
Author: Srijeet Chatterjee <30...@users.noreply.github.com>
AuthorDate: Mon May 18 09:43:48 2020 -0600

    Changes to add route id in access logs (#4696)
    
    * Changes to add route id in access logs
    
    * Code review changes
    
    * Fix typo
---
 CHANGELOG.md                                              |  1 +
 .../traffic_ops_golang/routing/middleware/wrappers.go     |  5 ++++-
 traffic_ops/traffic_ops_golang/routing/routing.go         | 15 ++++++++-------
 3 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 00ebd02..9e9f915 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -20,6 +20,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
 ### Changed
 - Changed some Traffic Ops Go Client methods to use `DeliveryServiceNullable` inputs and outputs.
 - Changed Traffic Portal to use Traffic Ops API v3
+- Changed the access logs in Traffic Ops to now show the route ID with every API endpoint call. The Route ID is appended to the end of the access log line.
 
 ### Deprecated
 - Deprecated the non-nullable `DeliveryService` Go struct and other structs that use it. `DeliveryServiceNullable` structs should be used instead.
diff --git a/traffic_ops/traffic_ops_golang/routing/middleware/wrappers.go b/traffic_ops/traffic_ops_golang/routing/middleware/wrappers.go
index fcf8d58..8699655 100644
--- a/traffic_ops/traffic_ops_golang/routing/middleware/wrappers.go
+++ b/traffic_ops/traffic_ops_golang/routing/middleware/wrappers.go
@@ -43,6 +43,9 @@ import (
 // This should be used by all Traffic Ops routing, and is recommended for use by plugins.
 const DefaultRequestTimeout = time.Second * time.Duration(60)
 
+// RouteID
+const RouteID = "RouteID"
+
 // ServerName is the name and version of Traffic Ops.
 // Things that print the server application name and version, for example in headers or logs, should use this.
 var ServerName = "traffic_ops_golang" + "/" + about.About.Version
@@ -169,7 +172,7 @@ func WrapAccessLog(secret string, h http.Handler) http.HandlerFunc {
 		}
 		start := time.Now()
 		defer func() {
-			log.EventfRaw(`%s - %s [%s] "%v %v?%v %s" %v %v %v "%v"`, r.RemoteAddr, user, time.Now().Format(AccessLogTimeFormat), r.Method, r.URL.Path, r.URL.RawQuery, r.Proto, iw.Code, iw.ByteCount, int(time.Now().Sub(start)/time.Millisecond), r.UserAgent())
+			log.EventfRaw(`%s - %s [%s] "%v %v?%v %s" %v %v %v "%v" %v`, r.RemoteAddr, user, time.Now().Format(AccessLogTimeFormat), r.Method, r.URL.Path, r.URL.RawQuery, r.Proto, iw.Code, iw.ByteCount, int(time.Now().Sub(start)/time.Millisecond), r.UserAgent(), r.Header.Get(RouteID))
 		}()
 		h.ServeHTTP(iw, r)
 	}
diff --git a/traffic_ops/traffic_ops_golang/routing/routing.go b/traffic_ops/traffic_ops_golang/routing/routing.go
index b9d8b2f..048d3c4 100644
--- a/traffic_ops/traffic_ops_golang/routing/routing.go
+++ b/traffic_ops/traffic_ops_golang/routing/routing.go
@@ -87,6 +87,7 @@ type CompiledRoute struct {
 	Handler http.HandlerFunc
 	Regex   *regexp.Regexp
 	Params  []string
+	ID      int
 }
 
 func getSortedRouteVersions(rs []Route) []api.Version {
@@ -142,6 +143,7 @@ func indexOfApiVersion(versions []api.Version, desiredVersion api.Version) int {
 type PathHandler struct {
 	Path    string
 	Handler http.HandlerFunc
+	ID      int
 }
 
 // CreateRouteMap returns a map of methods to a slice of paths and handlers; wrapping the handlers in the appropriate middleware. Uses Semantic Versioning: routes are added to every subsequent minor version, but not subsequent major versions. For example, a 1.2 route is added to 1.3 but not 2.1. Also truncates '2.0' to '2', creating succinct major versions.
@@ -170,11 +172,11 @@ func CreateRouteMap(rs []Route, rawRoutes []RawRoute, perlRouteIDs, disabledRout
 			middlewares := getRouteMiddleware(r.Middlewares, authBase, r.Authenticated, r.RequiredPrivLevel, requestTimeout)
 
 			if isPerlRoute {
-				m[r.Method] = append(m[r.Method], PathHandler{Path: path, Handler: perlHandler})
+				m[r.Method] = append(m[r.Method], PathHandler{Path: path, Handler: perlHandler, ID: r.ID})
 			} else if isDisabledRoute {
-				m[r.Method] = append(m[r.Method], PathHandler{Path: path, Handler: middleware.WrapAccessLog(authBase.Secret, middleware.DisabledRouteHandler())})
+				m[r.Method] = append(m[r.Method], PathHandler{Path: path, Handler: middleware.WrapAccessLog(authBase.Secret, middleware.DisabledRouteHandler()), ID: r.ID})
 			} else {
-				m[r.Method] = append(m[r.Method], PathHandler{Path: path, Handler: middleware.Use(r.Handler, middlewares)})
+				m[r.Method] = append(m[r.Method], PathHandler{Path: path, Handler: middleware.Use(r.Handler, middlewares), ID: r.ID})
 			}
 			log.Infof("adding route %v %v\n", r.Method, path)
 		}
@@ -189,7 +191,6 @@ func CreateRouteMap(rs []Route, rawRoutes []RawRoute, perlRouteIDs, disabledRout
 	for _, version := range versions {
 		versionSet[version] = struct{}{}
 	}
-
 	return m, versionSet
 }
 
@@ -223,7 +224,8 @@ func CompileRoutes(routes map[string][]PathHandler) map[string][]CompiledRoute {
 				route = route[:open] + `([^/]+)` + route[close+1:]
 			}
 			regex := regexp.MustCompile(route)
-			compiledRoutes[method] = append(compiledRoutes[method], CompiledRoute{Handler: handler, Regex: regex, Params: params})
+			id := pathHandler.ID
+			compiledRoutes[method] = append(compiledRoutes[method], CompiledRoute{Handler: handler, Regex: regex, Params: params, ID: id})
 		}
 	}
 	return compiledRoutes
@@ -270,7 +272,6 @@ func Handler(
 		catchall.ServeHTTP(w, r)
 		return
 	}
-
 	for _, compiledRoute := range mRoutes {
 		match := compiledRoute.Regex.FindStringSubmatch(requested)
 		if len(match) == 0 {
@@ -283,10 +284,10 @@ func Handler(
 
 		routeCtx := context.WithValue(ctx, api.PathParamsKey, params)
 		r = r.WithContext(routeCtx)
+		r.Header.Add(middleware.RouteID, strconv.Itoa(compiledRoute.ID))
 		compiledRoute.Handler(w, r)
 		return
 	}
-
 	if IsRequestAPIAndUnknownVersion(r, versions) {
 		h := middleware.WrapAccessLog(cfg.Secrets[0], middleware.NotImplementedHandler())
 		h.ServeHTTP(w, r)