18896: review feedback, un-pyramid the logrus.Fields update.
authorWard Vandewege <ward@curii.com>
Mon, 28 Mar 2022 14:14:09 +0000 (10:14 -0400)
committerWard Vandewege <ward@curii.com>
Mon, 28 Mar 2022 14:34:06 +0000 (10:34 -0400)
Arvados-DCO-1.1-Signed-off-by: Ward Vandewege <ward@curii.com>

sdk/go/httpserver/logger.go

index ef2ec417047bfae26c9ea0363c883abf2dacabed..5a46635e9102365bbfd01c9c9c120bd8e23a7026 100644 (file)
@@ -68,18 +68,15 @@ func HandlerWithDeadline(timeout time.Duration, next http.Handler) http.Handler
 }
 
 func SetResponseLogFields(ctx context.Context, fields logrus.Fields) {
-       m := ctx.Value(&mutexContextKey)
-       if mutex, ok := m.(sync.Mutex); ok {
-               mutex.Lock()
-               defer mutex.Unlock()
-               ctxfields := ctx.Value(&responseLogFieldsContextKey)
-               if c, ok := ctxfields.(logrus.Fields); ok {
-                       for k, v := range fields {
-                               c[k] = v
-                       }
-               }
-       } else {
-               // We can't lock, don't set the fields
+       m, _ := ctx.Value(&mutexContextKey).(*sync.Mutex)
+       c, _ := ctx.Value(&responseLogFieldsContextKey).(logrus.Fields)
+       if m == nil || c == nil {
+               return
+       }
+       m.Lock()
+       defer m.Unlock()
+       for k, v := range fields {
+               c[k] = v
        }
 }
 
@@ -101,7 +98,7 @@ func LogRequests(h http.Handler) http.Handler {
                ctx := req.Context()
                ctx = context.WithValue(ctx, &requestTimeContextKey, time.Now())
                ctx = context.WithValue(ctx, &responseLogFieldsContextKey, logrus.Fields{})
-               ctx = context.WithValue(ctx, &mutexContextKey, sync.Mutex{})
+               ctx = context.WithValue(ctx, &mutexContextKey, &sync.Mutex{})
                ctx = ctxlog.Context(ctx, lgr)
                req = req.WithContext(ctx)