From a15bb19d37781980441963fcb3b37f53867c6fc8 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Thu, 15 Aug 2019 16:17:46 -0700 Subject: [PATCH] Allow audit logging to work while tracing (#8077) It is observed that when `mc admin trace` is being used due to ResponseWriter wrapper, we loose information about statusCode,statusText for audit logging. This PR fixes this behavior --- cmd/http-tracer.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/cmd/http-tracer.go b/cmd/http-tracer.go index c904ed6e4..b0a33e007 100644 --- a/cmd/http-tracer.go +++ b/cmd/http-tracer.go @@ -29,6 +29,7 @@ import ( "strings" "time" + "github.com/minio/minio/cmd/logger" trace "github.com/minio/minio/pkg/trace" ) @@ -126,7 +127,7 @@ func (r *recordResponseWriter) Write(p []byte) (n int, err error) { r.writeHeaders(&r.headers, http.StatusOK, r.ResponseWriter.Header()) r.headersLogged = true } - if (r.statusCode != http.StatusOK && r.statusCode != http.StatusPartialContent && r.statusCode != 0) || r.logBody { + if r.statusCode >= http.StatusBadRequest || r.logBody { // Always logging error responses. r.body.Write(p) } @@ -146,7 +147,7 @@ func (r *recordResponseWriter) Flush() { func (r *recordResponseWriter) Body() []byte { // If there was an error response or body logging is enabled // then we return the body contents - if r.statusCode >= 400 || r.logBody { + if r.statusCode >= http.StatusBadRequest || r.logBody { return r.body.Bytes() } // ... otherwise we return the place holder @@ -206,7 +207,7 @@ func Trace(f http.HandlerFunc, logBody bool, w http.ResponseWriter, r *http.Requ // Setup a http response body recorder respBodyRecorder := &recordResponseWriter{ResponseWriter: w, logBody: logBody} - f(respBodyRecorder, r) + f(logger.NewResponseWriter(respBodyRecorder), r) rs := trace.ResponseInfo{ Time: time.Now().UTC(), @@ -222,6 +223,10 @@ func Trace(f http.HandlerFunc, logBody bool, w http.ResponseWriter, r *http.Requ t.ReqInfo = rq t.RespInfo = rs - t.CallStats = trace.CallStats{Latency: rs.Time.Sub(rq.Time), InputBytes: reqBodyRecorder.Size(), OutputBytes: respBodyRecorder.Size()} + t.CallStats = trace.CallStats{ + Latency: rs.Time.Sub(rq.Time), + InputBytes: reqBodyRecorder.Size(), + OutputBytes: respBodyRecorder.Size(), + } return t }