From 042d636e3491c6dd4ca6a1e26fdf796d31a1a72d Mon Sep 17 00:00:00 2001 From: Robert Swain Date: Wed, 10 May 2017 15:43:58 +0200 Subject: [PATCH] Update github.com/matrix-org/util for request context fix for logging --- vendor/manifest | 4 +-- vendor/src/github.com/matrix-org/util/json.go | 33 ++++++++++++------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/vendor/manifest b/vendor/manifest index 35d58db8b..c2c83088a 100644 --- a/vendor/manifest +++ b/vendor/manifest @@ -98,7 +98,7 @@ { "importpath": "github.com/matrix-org/util", "repository": "https://github.com/matrix-org/util", - "revision": "bc9d5e2d2f68a2ca279fce0fa2f28a91ecf301ed", + "revision": "53326ed5598b226681112cbd441f59f3cffc9c82", "branch": "master" }, { @@ -212,4 +212,4 @@ "branch": "v2" } ] -} \ No newline at end of file +} diff --git a/vendor/src/github.com/matrix-org/util/json.go b/vendor/src/github.com/matrix-org/util/json.go index a30d73f8e..c02f08fe3 100644 --- a/vendor/src/github.com/matrix-org/util/json.go +++ b/vendor/src/github.com/matrix-org/util/json.go @@ -93,23 +93,32 @@ func Protect(handler http.HandlerFunc) http.HandlerFunc { } } +// RequestWithLogging sets up standard logging for http.Requests. +// http.Requests will have a logger (with a request ID/method/path logged) attached to the Context. +// This can be accessed via GetLogger(Context). +func RequestWithLogging(req *http.Request) *http.Request { + reqID := RandomString(12) + // Set a Logger and request ID on the context + ctx := context.WithValue(req.Context(), ctxValueLogger, log.WithFields(log.Fields{ + "req.method": req.Method, + "req.path": req.URL.Path, + "req.id": reqID, + })) + ctx = context.WithValue(ctx, ctxValueRequestID, reqID) + req = req.WithContext(ctx) + + logger := GetLogger(req.Context()) + logger.Print("Incoming request") + + return req +} + // MakeJSONAPI creates an HTTP handler which always responds to incoming requests with JSON responses. // Incoming http.Requests will have a logger (with a request ID/method/path logged) attached to the Context. // This can be accessed via GetLogger(Context). func MakeJSONAPI(handler JSONRequestHandler) http.HandlerFunc { return Protect(func(w http.ResponseWriter, req *http.Request) { - reqID := RandomString(12) - // Set a Logger and request ID on the context - ctx := context.WithValue(req.Context(), ctxValueLogger, log.WithFields(log.Fields{ - "req.method": req.Method, - "req.path": req.URL.Path, - "req.id": reqID, - })) - ctx = context.WithValue(ctx, ctxValueRequestID, reqID) - req = req.WithContext(ctx) - - logger := GetLogger(req.Context()) - logger.Print("Incoming request") + req = RequestWithLogging(req) if req.Method == "OPTIONS" { SetCORSHeaders(w)