fix(AuditLog): panic while st is nil (#13510)

This commit is contained in:
moon 2021-10-28 00:29:42 +08:00 committed by GitHub
parent 939fbb3c38
commit d158607f8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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.