Merge branch '18631-shell-login-sync'
[arvados.git] / sdk / go / httpserver / logger.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: Apache-2.0
4
5 package httpserver
6
7 import (
8         "bufio"
9         "context"
10         "net"
11         "net/http"
12         "sync"
13         "time"
14
15         "git.arvados.org/arvados.git/sdk/go/ctxlog"
16         "git.arvados.org/arvados.git/sdk/go/stats"
17         "github.com/sirupsen/logrus"
18 )
19
20 type contextKey struct {
21         name string
22 }
23
24 var (
25         requestTimeContextKey       = contextKey{"requestTime"}
26         responseLogFieldsContextKey = contextKey{"responseLogFields"}
27         mutexContextKey             = contextKey{"mutex"}
28 )
29
30 type hijacker interface {
31         http.ResponseWriter
32         http.Hijacker
33 }
34
35 // hijackNotifier wraps a ResponseWriter, calling the provided
36 // Notify() func if/when the wrapped Hijacker is hijacked.
37 type hijackNotifier struct {
38         hijacker
39         hijacked chan<- bool
40 }
41
42 func (hn hijackNotifier) Hijack() (net.Conn, *bufio.ReadWriter, error) {
43         close(hn.hijacked)
44         return hn.hijacker.Hijack()
45 }
46
47 // HandlerWithDeadline cancels the request context if the request
48 // takes longer than the specified timeout without having its
49 // connection hijacked.
50 func HandlerWithDeadline(timeout time.Duration, next http.Handler) http.Handler {
51         return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
52                 ctx, cancel := context.WithCancel(r.Context())
53                 defer cancel()
54                 nodeadline := make(chan bool)
55                 go func() {
56                         select {
57                         case <-nodeadline:
58                         case <-ctx.Done():
59                         case <-time.After(timeout):
60                                 cancel()
61                         }
62                 }()
63                 if hj, ok := w.(hijacker); ok {
64                         w = hijackNotifier{hj, nodeadline}
65                 }
66                 next.ServeHTTP(w, r.WithContext(ctx))
67         })
68 }
69
70 func SetResponseLogFields(ctx context.Context, fields logrus.Fields) {
71         m, _ := ctx.Value(&mutexContextKey).(*sync.Mutex)
72         c, _ := ctx.Value(&responseLogFieldsContextKey).(logrus.Fields)
73         if m == nil || c == nil {
74                 return
75         }
76         m.Lock()
77         defer m.Unlock()
78         for k, v := range fields {
79                 c[k] = v
80         }
81 }
82
83 // LogRequests wraps an http.Handler, logging each request and
84 // response.
85 func LogRequests(h http.Handler) http.Handler {
86         return http.HandlerFunc(func(wrapped http.ResponseWriter, req *http.Request) {
87                 w := &responseTimer{ResponseWriter: WrapResponseWriter(wrapped)}
88                 lgr := ctxlog.FromContext(req.Context()).WithFields(logrus.Fields{
89                         "RequestID":       req.Header.Get("X-Request-Id"),
90                         "remoteAddr":      req.RemoteAddr,
91                         "reqForwardedFor": req.Header.Get("X-Forwarded-For"),
92                         "reqMethod":       req.Method,
93                         "reqHost":         req.Host,
94                         "reqPath":         req.URL.Path[1:],
95                         "reqQuery":        req.URL.RawQuery,
96                         "reqBytes":        req.ContentLength,
97                 })
98                 ctx := req.Context()
99                 ctx = context.WithValue(ctx, &requestTimeContextKey, time.Now())
100                 ctx = context.WithValue(ctx, &responseLogFieldsContextKey, logrus.Fields{})
101                 ctx = context.WithValue(ctx, &mutexContextKey, &sync.Mutex{})
102                 ctx = ctxlog.Context(ctx, lgr)
103                 req = req.WithContext(ctx)
104
105                 logRequest(w, req, lgr)
106                 defer logResponse(w, req, lgr)
107                 h.ServeHTTP(rewrapResponseWriter(w, wrapped), req)
108         })
109 }
110
111 // Rewrap w to restore additional interfaces provided by wrapped.
112 func rewrapResponseWriter(w http.ResponseWriter, wrapped http.ResponseWriter) http.ResponseWriter {
113         if hijacker, ok := wrapped.(http.Hijacker); ok {
114                 return struct {
115                         http.ResponseWriter
116                         http.Hijacker
117                 }{w, hijacker}
118         }
119         return w
120 }
121
122 func Logger(req *http.Request) logrus.FieldLogger {
123         return ctxlog.FromContext(req.Context())
124 }
125
126 func logRequest(w *responseTimer, req *http.Request, lgr *logrus.Entry) {
127         lgr.Info("request")
128 }
129
130 func logResponse(w *responseTimer, req *http.Request, lgr *logrus.Entry) {
131         if tStart, ok := req.Context().Value(&requestTimeContextKey).(time.Time); ok {
132                 tDone := time.Now()
133                 writeTime := w.writeTime
134                 if !w.wrote {
135                         // Empty response body. Header was sent when
136                         // handler exited.
137                         writeTime = tDone
138                 }
139                 lgr = lgr.WithFields(logrus.Fields{
140                         "timeTotal":     stats.Duration(tDone.Sub(tStart)),
141                         "timeToStatus":  stats.Duration(writeTime.Sub(tStart)),
142                         "timeWriteBody": stats.Duration(tDone.Sub(writeTime)),
143                 })
144         }
145         if responseLogFields, ok := req.Context().Value(&responseLogFieldsContextKey).(logrus.Fields); ok {
146                 lgr = lgr.WithFields(responseLogFields)
147         }
148         respCode := w.WroteStatus()
149         if respCode == 0 {
150                 respCode = http.StatusOK
151         }
152         fields := logrus.Fields{
153                 "respStatusCode": respCode,
154                 "respStatus":     http.StatusText(respCode),
155                 "respBytes":      w.WroteBodyBytes(),
156         }
157         if respCode >= 400 {
158                 fields["respBody"] = string(w.Sniffed())
159         }
160         lgr.WithFields(fields).Info("response")
161 }
162
163 type responseTimer struct {
164         ResponseWriter
165         wrote     bool
166         writeTime time.Time
167 }
168
169 func (rt *responseTimer) CloseNotify() <-chan bool {
170         if cn, ok := rt.ResponseWriter.(http.CloseNotifier); ok {
171                 return cn.CloseNotify()
172         }
173         return nil
174 }
175
176 func (rt *responseTimer) WriteHeader(code int) {
177         if !rt.wrote {
178                 rt.wrote = true
179                 rt.writeTime = time.Now()
180         }
181         rt.ResponseWriter.WriteHeader(code)
182 }
183
184 func (rt *responseTimer) Write(p []byte) (int, error) {
185         if !rt.wrote {
186                 rt.wrote = true
187                 rt.writeTime = time.Now()
188         }
189         return rt.ResponseWriter.Write(p)
190 }