-
Notifications
You must be signed in to change notification settings - Fork 12
/
middleware_grip.go
218 lines (184 loc) · 6.19 KB
/
middleware_grip.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package gimlet
import (
"context"
"net/http"
"time"
"github.com/evergreen-ci/negroni"
"github.com/evergreen-ci/utility"
"github.com/mongodb/grip"
"github.com/mongodb/grip/logging"
"github.com/mongodb/grip/message"
"github.com/mongodb/grip/recovery"
"go.opentelemetry.io/otel/attribute"
)
const (
remoteAddrHeaderName = "X-Forwarded-For"
requestIDAttribute = "gimlet.request.id"
)
// appLogging provides a Negroni-compatible middleware to send all
// logging using the grip packages logging. This defaults to using
// systemd logging, but gracefully falls back to use go standard
// library logging, with some additional helpers and configurations to
// support configurable level-based logging. This particular
// middlewear resembles the basic tracking provided by Negroni's
// standard logging system.
type appLogging struct {
grip.Journaler
}
// NewAppLogger creates an logging middlear instance suitable for use
// with Negroni. Sets the logging configuration to be the same as the
// default global grip logging object.
func NewAppLogger() Middleware { return &appLogging{logging.MakeGrip(grip.GetSender())} }
func setServiceLogger(r *http.Request, logger grip.Journaler) *http.Request {
return r.WithContext(context.WithValue(r.Context(), loggerKey, logger))
}
type loggingAnnotations map[string]interface{}
// AddLoggingAnnotation adds a key-value pair to be added to logging
// messages used by the application logging information, overwriting
// any previous value for the key.
// This will noop if the logger hasn't already been set up.
func AddLoggingAnnotation(r *http.Request, key string, value interface{}) {
annotations := getLoggingAnnotations(r.Context())
if annotations == nil {
return
}
annotations[key] = value
}
func setLoggingAnnotations(r *http.Request, annotations loggingAnnotations) *http.Request {
return r.WithContext(context.WithValue(r.Context(), loggingAnnotationsKey, annotations))
}
func getLoggingAnnotations(ctx context.Context) loggingAnnotations {
rv := ctx.Value(loggingAnnotationsKey)
if rv == nil {
return nil
}
a, ok := rv.(loggingAnnotations)
if !ok {
return nil
}
return a
}
func setStartAtTime(r *http.Request, startAt time.Time) *http.Request {
return r.WithContext(context.WithValue(r.Context(), startAtKey, startAt))
}
func getRequestStartAt(ctx context.Context) time.Time {
if rv := ctx.Value(startAtKey); rv != nil {
if t, ok := rv.(time.Time); ok {
return t
}
}
return time.Time{}
}
// GetLogger produces a special logger attached to the request. If no
// request is attached, GetLogger returns a logger instance wrapping
// the global sender.
func GetLogger(ctx context.Context) grip.Journaler {
if rv := ctx.Value(loggerKey); rv != nil {
if l, ok := rv.(grip.Journaler); ok {
return l
}
}
return logging.MakeGrip(grip.GetSender())
}
// Logs the request path, the beginning of every request as well as
// the duration upon completion and the status of the response.
func (l *appLogging) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
r = setupLogger(l.Journaler, r)
next(rw, r)
res := rw.(negroni.ResponseWriter)
finishLogger(l.Journaler, r, res)
}
func setupLogger(logger grip.Journaler, r *http.Request) *http.Request {
r = setServiceLogger(r, logger)
remote := r.Header.Get(remoteAddrHeaderName)
if remote != "" {
r.RemoteAddr = remote
}
id := getNumber()
r = setRequestID(r, id)
r = r.WithContext(utility.ContextWithAttributes(r.Context(), []attribute.KeyValue{attribute.Int(requestIDAttribute, id)}))
startAt := time.Now()
r = setStartAtTime(r, startAt)
r = setLoggingAnnotations(r, loggingAnnotations{})
return r
}
func finishLogger(logger grip.Journaler, r *http.Request, res negroni.ResponseWriter) {
ctx := r.Context()
startAt := getRequestStartAt(ctx)
dur := time.Since(startAt)
m := message.Fields{
"action": "completed",
"method": r.Method,
"remote": r.RemoteAddr,
"request": GetRequestID(ctx),
"path": r.URL.Path,
"duration_ms": int64(dur / time.Millisecond),
"status": res.Status(),
}
if len(r.URL.Query()) > 0 {
m["params"] = r.URL.Query()
}
a := getLoggingAnnotations(ctx)
if a != nil {
for key, val := range a {
m[key] = val
}
}
logger.Info(m)
}
// This is largely duplicated from the above, but lets us optionally
type appRecoveryLogger struct {
grip.Journaler
}
// NewRecoveryLogger logs request start, end, and recovers from panics
// (logging the panic as well).
func NewRecoveryLogger(j grip.Journaler) Middleware { return &appRecoveryLogger{Journaler: j} }
// MakeRecoveryLoger constructs a middleware layer that logs request
// start, end, and recovers from panics (logging the panic as well).
//
// This logger uses the default grip logger.
func MakeRecoveryLogger() Middleware {
return &appRecoveryLogger{Journaler: logging.MakeGrip(grip.GetSender())}
}
func (l *appRecoveryLogger) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
r = setupLogger(l.Journaler, r)
ctx := r.Context()
defer func() {
if err := recover(); err != nil {
if rw.Header().Get("Content-Type") == "" {
rw.Header().Set("Content-Type", "application/json; charset=utf-8")
}
rw.WriteHeader(http.StatusInternalServerError)
// handlers panic with http.ErrAbortHandler to abort and suppress logging
// (https://golang.org/pkg/net/http/#Handler)
// log at a lower level
if err == http.ErrAbortHandler {
l.Debug(message.Fields{
"message": "hit suppressed abort panic",
"action": "aborted",
"request": GetRequestID(ctx),
"duration": time.Since(getRequestStartAt(ctx)),
"path": r.URL.Path,
"remote": r.RemoteAddr,
"length": r.ContentLength,
})
} else {
_ = recovery.SendMessageWithPanicError(err, nil, l.Journaler, message.Fields{
"action": "aborted",
"request": GetRequestID(ctx),
"duration": time.Since(getRequestStartAt(ctx)),
"path": r.URL.Path,
"remote": r.RemoteAddr,
"length": r.ContentLength,
})
}
WriteJSONInternalError(rw, ErrorResponse{
StatusCode: http.StatusInternalServerError,
Message: "request aborted",
})
}
}()
next(rw, r)
res := rw.(negroni.ResponseWriter)
finishLogger(l.Journaler, r, res)
}