X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/49030c3161523bc427e0aaa9d3edb517fc4c296c..d13a369065b4e72047c8c267d6ceaa5c84d50c3d:/services/keepstore/logging_router.go diff --git a/services/keepstore/logging_router.go b/services/keepstore/logging_router.go index e30df87632..bfd006ee8d 100644 --- a/services/keepstore/logging_router.go +++ b/services/keepstore/logging_router.go @@ -4,48 +4,118 @@ package main // LoggingResponseWriter import ( - "github.com/gorilla/mux" - "log" + "context" + "fmt" "net/http" "strings" + "time" + + "git.curoverse.com/arvados.git/sdk/go/httpserver" + log "github.com/Sirupsen/logrus" ) +// LoggingResponseWriter has anonymous fields ResponseWriter and ResponseBody type LoggingResponseWriter struct { Status int Length int http.ResponseWriter ResponseBody string + sentHdr time.Time } -func (loggingWriter *LoggingResponseWriter) WriteHeader(code int) { - loggingWriter.Status = code - loggingWriter.ResponseWriter.WriteHeader(code) +// CloseNotify implements http.CloseNotifier. +func (resp *LoggingResponseWriter) CloseNotify() <-chan bool { + wrapped, ok := resp.ResponseWriter.(http.CloseNotifier) + if !ok { + // If upstream doesn't implement CloseNotifier, we can + // satisfy the interface by returning a channel that + // never sends anything (the interface doesn't + // guarantee that anything will ever be sent on the + // channel even if the client disconnects). + return nil + } + return wrapped.CloseNotify() } -func (loggingWriter *LoggingResponseWriter) Write(data []byte) (int, error) { - loggingWriter.Length += len(data) - if loggingWriter.Status >= 400 { - loggingWriter.ResponseBody += string(data) +// WriteHeader writes header to ResponseWriter +func (resp *LoggingResponseWriter) WriteHeader(code int) { + if resp.sentHdr == zeroTime { + resp.sentHdr = time.Now() } - return loggingWriter.ResponseWriter.Write(data) + resp.Status = code + resp.ResponseWriter.WriteHeader(code) } -type LoggingRESTRouter struct { - router *mux.Router +var zeroTime time.Time + +func (resp *LoggingResponseWriter) Write(data []byte) (int, error) { + if resp.Length == 0 && len(data) > 0 && resp.sentHdr == zeroTime { + resp.sentHdr = time.Now() + } + resp.Length += len(data) + if resp.Status >= 400 { + resp.ResponseBody += string(data) + } + return resp.ResponseWriter.Write(data) } -func MakeLoggingRESTRouter() *LoggingRESTRouter { - router := MakeRESTRouter() - return (&LoggingRESTRouter{router}) +// LoggingRESTRouter is used to add logging capabilities to mux.Router +type LoggingRESTRouter struct { + router http.Handler + idGenerator httpserver.IDGenerator } -func (loggingRouter *LoggingRESTRouter) ServeHTTP(resp http.ResponseWriter, req *http.Request) { - loggingWriter := LoggingResponseWriter{200, 0, resp, ""} - loggingRouter.router.ServeHTTP(&loggingWriter, req) - statusText := "OK" - if loggingWriter.Status >= 400 { - statusText = strings.Replace(loggingWriter.ResponseBody, "\n", "", -1) +func (loggingRouter *LoggingRESTRouter) ServeHTTP(wrappedResp http.ResponseWriter, req *http.Request) { + tStart := time.Now() + + // Attach a requestID-aware logger to the request context. + lgr := log.WithField("RequestID", loggingRouter.idGenerator.Next()) + ctx := context.WithValue(req.Context(), "logger", lgr) + req = req.WithContext(ctx) + + lgr = lgr.WithFields(log.Fields{ + "remoteAddr": req.RemoteAddr, + "reqForwardedFor": req.Header.Get("X-Forwarded-For"), + "reqMethod": req.Method, + "reqPath": req.URL.Path[1:], + "reqBytes": req.ContentLength, + }) + lgr.Debug("request") + + resp := LoggingResponseWriter{http.StatusOK, 0, wrappedResp, "", zeroTime} + loggingRouter.router.ServeHTTP(&resp, req) + tDone := time.Now() + + statusText := http.StatusText(resp.Status) + if resp.Status >= 400 { + statusText = strings.Replace(resp.ResponseBody, "\n", "", -1) } - log.Printf("[%s] %s %s %d %d \"%s\"", req.RemoteAddr, req.Method, req.URL.Path[1:], loggingWriter.Status, loggingWriter.Length, statusText) + if resp.sentHdr == zeroTime { + // Nobody changed status or wrote any data, i.e., we + // returned a 200 response with no body. + resp.sentHdr = tDone + } + + lgr.WithFields(log.Fields{ + "timeTotal": loggedDuration(tDone.Sub(tStart)), + "timeToStatus": loggedDuration(resp.sentHdr.Sub(tStart)), + "timeWriteBody": loggedDuration(tDone.Sub(resp.sentHdr)), + "respStatusCode": resp.Status, + "respStatus": statusText, + "respBytes": resp.Length, + }).Info("response") +} + +type loggedDuration time.Duration + +// MarshalJSON formats a duration as a number of seconds, using +// fixed-point notation with no more than 6 decimal places. +func (d loggedDuration) MarshalJSON() ([]byte, error) { + return []byte(d.String()), nil +} +// String formats a duration as a number of seconds, using +// fixed-point notation with no more than 6 decimal places. +func (d loggedDuration) String() string { + return fmt.Sprintf("%.6f", time.Duration(d).Seconds()) }