13697: Cancel request context after API.RequestTimeout.
[arvados.git] / sdk / go / httpserver / logger.go
index 1a4b7c55925b20eb398cc9d9c402004a0d2f779c..1916880963494333ce9d4356f1d1c9d2eba55f3b 100644 (file)
@@ -1,6 +1,6 @@
 // Copyright (C) The Arvados Authors. All rights reserved.
 //
-// SPDX-License-Identifier: AGPL-3.0
+// SPDX-License-Identifier: Apache-2.0
 
 package httpserver
 
@@ -9,25 +9,44 @@ import (
        "net/http"
        "time"
 
-       "git.curoverse.com/arvados.git/sdk/go/stats"
-       "github.com/Sirupsen/logrus"
+       "git.arvados.org/arvados.git/sdk/go/ctxlog"
+       "git.arvados.org/arvados.git/sdk/go/stats"
+       "github.com/sirupsen/logrus"
 )
 
 type contextKey struct {
        name string
 }
 
-var requestTimeContextKey = contextKey{"requestTime"}
+var (
+       requestTimeContextKey = contextKey{"requestTime"}
+)
 
-var Logger logrus.FieldLogger = logrus.StandardLogger()
+// HandlerWithContext returns an http.Handler that changes the request
+// context to ctx (replacing http.Server's default
+// context.Background()), then calls next.
+func HandlerWithContext(ctx context.Context, next http.Handler) http.Handler {
+       return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+               next.ServeHTTP(w, r.WithContext(ctx))
+       })
+}
+
+// HandlerWithDeadline cancels the request context if the request
+// takes longer than the specified timeout.
+func HandlerWithDeadline(timeout time.Duration, next http.Handler) http.Handler {
+       return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+               ctx, cancel := context.WithDeadline(r.Context(), time.Now().Add(timeout))
+               defer cancel()
+               next.ServeHTTP(w, r.WithContext(ctx))
+       })
+}
 
 // LogRequests wraps an http.Handler, logging each request and
-// response via logrus.
+// response.
 func LogRequests(h http.Handler) http.Handler {
        return http.HandlerFunc(func(wrapped http.ResponseWriter, req *http.Request) {
                w := &responseTimer{ResponseWriter: WrapResponseWriter(wrapped)}
-               req = req.WithContext(context.WithValue(req.Context(), &requestTimeContextKey, time.Now()))
-               lgr := Logger.WithFields(logrus.Fields{
+               lgr := ctxlog.FromContext(req.Context()).WithFields(logrus.Fields{
                        "RequestID":       req.Header.Get("X-Request-Id"),
                        "remoteAddr":      req.RemoteAddr,
                        "reqForwardedFor": req.Header.Get("X-Forwarded-For"),
@@ -37,12 +56,32 @@ func LogRequests(h http.Handler) http.Handler {
                        "reqQuery":        req.URL.RawQuery,
                        "reqBytes":        req.ContentLength,
                })
+               ctx := req.Context()
+               ctx = context.WithValue(ctx, &requestTimeContextKey, time.Now())
+               ctx = ctxlog.Context(ctx, lgr)
+               req = req.WithContext(ctx)
+
                logRequest(w, req, lgr)
                defer logResponse(w, req, lgr)
-               h.ServeHTTP(w, req)
+               h.ServeHTTP(rewrapResponseWriter(w, wrapped), req)
        })
 }
 
+// Rewrap w to restore additional interfaces provided by wrapped.
+func rewrapResponseWriter(w http.ResponseWriter, wrapped http.ResponseWriter) http.ResponseWriter {
+       if hijacker, ok := wrapped.(http.Hijacker); ok {
+               return struct {
+                       http.ResponseWriter
+                       http.Hijacker
+               }{w, hijacker}
+       }
+       return w
+}
+
+func Logger(req *http.Request) logrus.FieldLogger {
+       return ctxlog.FromContext(req.Context())
+}
+
 func logRequest(w *responseTimer, req *http.Request, lgr *logrus.Entry) {
        lgr.Info("request")
 }
@@ -50,21 +89,31 @@ func logRequest(w *responseTimer, req *http.Request, lgr *logrus.Entry) {
 func logResponse(w *responseTimer, req *http.Request, lgr *logrus.Entry) {
        if tStart, ok := req.Context().Value(&requestTimeContextKey).(time.Time); ok {
                tDone := time.Now()
+               writeTime := w.writeTime
+               if !w.wrote {
+                       // Empty response body. Header was sent when
+                       // handler exited.
+                       writeTime = tDone
+               }
                lgr = lgr.WithFields(logrus.Fields{
                        "timeTotal":     stats.Duration(tDone.Sub(tStart)),
-                       "timeToStatus":  stats.Duration(w.writeTime.Sub(tStart)),
-                       "timeWriteBody": stats.Duration(tDone.Sub(w.writeTime)),
+                       "timeToStatus":  stats.Duration(writeTime.Sub(tStart)),
+                       "timeWriteBody": stats.Duration(tDone.Sub(writeTime)),
                })
        }
        respCode := w.WroteStatus()
        if respCode == 0 {
                respCode = http.StatusOK
        }
-       lgr.WithFields(logrus.Fields{
+       fields := logrus.Fields{
                "respStatusCode": respCode,
                "respStatus":     http.StatusText(respCode),
                "respBytes":      w.WroteBodyBytes(),
-       }).Info("response")
+       }
+       if respCode >= 400 {
+               fields["respBody"] = string(w.Sniffed())
+       }
+       lgr.WithFields(fields).Info("response")
 }
 
 type responseTimer struct {