From d158607f8efbe3bec846f37fa834e761bc87f972 Mon Sep 17 00:00:00 2001 From: moon Date: Thu, 28 Oct 2021 00:29:42 +0800 Subject: [PATCH] fix(AuditLog): panic while st is nil (#13510) --- internal/logger/audit.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/internal/logger/audit.go b/internal/logger/audit.go index 625e1c01e..3f39c4ce4 100644 --- a/internal/logger/audit.go +++ b/internal/logger/audit.go @@ -26,6 +26,7 @@ import ( "strconv" "time" + "github.com/klauspost/compress/gzhttp" "github.com/minio/minio/internal/logger/message/audit" ) @@ -183,13 +184,24 @@ func AuditLog(ctx context.Context, w http.ResponseWriter, r *http.Request, reqCl statusCode int timeToResponse time.Duration timeToFirstByte time.Duration + outputBytes int64 = -1 // -1: unknown output bytes ) - st, ok := w.(*ResponseWriter) - if ok { + var st *ResponseWriter + switch v := w.(type) { + case *ResponseWriter: + st = v + case *gzhttp.GzipResponseWriter: + // the writer may be obscured by gzip response writer + if rw, ok := v.ResponseWriter.(*ResponseWriter); ok { + st = rw + } + } + if st != nil { statusCode = st.StatusCode timeToResponse = time.Now().UTC().Sub(st.StartTime) timeToFirstByte = st.TimeToFirstByte + outputBytes = int64(st.Size()) } entry.API.Name = reqInfo.API @@ -198,7 +210,7 @@ func AuditLog(ctx context.Context, w http.ResponseWriter, r *http.Request, reqCl entry.API.Status = http.StatusText(statusCode) entry.API.StatusCode = statusCode entry.API.InputBytes = r.ContentLength - entry.API.OutputBytes = int64(st.Size()) + entry.API.OutputBytes = outputBytes entry.API.TimeToResponse = strconv.FormatInt(timeToResponse.Nanoseconds(), 10) + "ns" entry.Tags = reqInfo.GetTagsMap() // ttfb will be recorded only for GET requests, Ignore such cases where ttfb will be empty.