-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
loggingMiddleware.go
286 lines (259 loc) · 8.27 KB
/
loggingMiddleware.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package http
import (
"bufio"
"errors"
"fmt"
"net"
"net/http"
"strings"
"time"
"github.com/plgd-dev/go-coap/v3/message"
"github.com/plgd-dev/hub/v2/http-gateway/uri"
"github.com/plgd-dev/hub/v2/pkg/log"
pkgJwt "github.com/plgd-dev/hub/v2/pkg/security/jwt"
"go.opentelemetry.io/otel/trace"
rpcStatus "google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
)
type statusWriter struct {
http.ResponseWriter
status int
err error
}
func (w *statusWriter) WriteHeader(status int) {
w.status = status
w.ResponseWriter.WriteHeader(status)
}
func (w *statusWriter) Write(b []byte) (int, error) {
if w.status == 0 {
w.status = 200
}
if w.status >= 400 && len(b) > 0 && w.err == nil {
switch w.ResponseWriter.Header().Get(uri.ContentTypeHeaderKey) {
case uri.ApplicationProtoJsonContentType:
case message.AppJSON.String():
var s rpcStatus.Status
if err := protojson.Unmarshal(b, &s); err == nil {
w.err = status.ErrorProto(&s)
}
}
if w.err == nil {
errLen := 1024
if len(b) < errLen {
errLen = len(b)
}
w.err = fmt.Errorf("%v", string(b[:errLen]))
}
}
n, err := w.ResponseWriter.Write(b)
return n, err
}
func (w *statusWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
writer, ok := w.ResponseWriter.(http.Hijacker)
if !ok {
return nil, nil, errors.New("not supported by the underlying writer")
}
return writer.Hijack()
}
func (w *statusWriter) Flush() {
f, ok := w.ResponseWriter.(interface{ Flush() })
if ok {
f.Flush()
}
}
var toNil = func(...interface{}) {
// Do nothing because we don't want to log anything
}
func toDebug(logger log.Logger) func(args ...interface{}) {
if logger.Check(log.DebugLevel) {
return logger.Debug
}
return nil
}
func toWarn(logger log.Logger) func(args ...interface{}) {
if logger.Check(log.WarnLevel) {
return logger.Warn
}
return nil
}
func toError(logger log.Logger) func(args ...interface{}) {
if logger.Check(log.ErrorLevel) {
return logger.Error
}
return nil
}
var defaultCodeToLevel = map[int]func(logger log.Logger) func(args ...interface{}){
0: toDebug, // websocket returns https status code with 0
http.StatusContinue: toDebug,
http.StatusSwitchingProtocols: toDebug,
http.StatusProcessing: toDebug,
http.StatusEarlyHints: toDebug,
http.StatusOK: toDebug,
http.StatusCreated: toDebug,
http.StatusAccepted: toDebug,
http.StatusNonAuthoritativeInfo: toDebug,
http.StatusNoContent: toDebug,
http.StatusResetContent: toDebug,
http.StatusPartialContent: toDebug,
http.StatusMultiStatus: toDebug,
http.StatusAlreadyReported: toDebug,
http.StatusIMUsed: toDebug,
http.StatusMultipleChoices: toDebug,
http.StatusMovedPermanently: toDebug,
http.StatusFound: toDebug,
http.StatusSeeOther: toDebug,
http.StatusNotModified: toDebug,
http.StatusUseProxy: toDebug,
http.StatusTemporaryRedirect: toDebug,
http.StatusPermanentRedirect: toDebug,
http.StatusBadRequest: toDebug,
http.StatusUnauthorized: toDebug,
http.StatusPaymentRequired: toDebug,
http.StatusForbidden: toWarn,
http.StatusNotFound: toDebug,
http.StatusMethodNotAllowed: toDebug,
http.StatusNotAcceptable: toDebug,
http.StatusProxyAuthRequired: toDebug,
http.StatusRequestTimeout: toDebug,
http.StatusConflict: toDebug,
http.StatusGone: toDebug,
http.StatusLengthRequired: toDebug,
http.StatusPreconditionFailed: toWarn,
http.StatusRequestEntityTooLarge: toDebug,
http.StatusRequestURITooLong: toDebug,
http.StatusUnsupportedMediaType: toDebug,
http.StatusRequestedRangeNotSatisfiable: toDebug,
http.StatusExpectationFailed: toDebug,
http.StatusTeapot: toDebug,
http.StatusMisdirectedRequest: toDebug,
http.StatusUnprocessableEntity: toDebug,
http.StatusLocked: toDebug,
http.StatusFailedDependency: toDebug,
http.StatusTooEarly: toDebug,
http.StatusUpgradeRequired: toDebug,
http.StatusPreconditionRequired: toDebug,
http.StatusTooManyRequests: toDebug,
http.StatusRequestHeaderFieldsTooLarge: toDebug,
http.StatusUnavailableForLegalReasons: toWarn,
http.StatusInternalServerError: toError,
http.StatusNotImplemented: toError,
http.StatusBadGateway: toDebug,
http.StatusServiceUnavailable: toWarn,
http.StatusGatewayTimeout: toWarn,
http.StatusHTTPVersionNotSupported: toDebug,
http.StatusVariantAlsoNegotiates: toDebug,
http.StatusInsufficientStorage: toDebug,
http.StatusLoopDetected: toError,
http.StatusNotExtended: toDebug,
http.StatusNetworkAuthenticationRequired: toDebug,
}
// DefaultCodeToLevel is the default implementation of gRPC return codes and interceptor log level for server side.
func DefaultCodeToLevel(code int, logger log.Logger) func(args ...interface{}) {
targerLvl, ok := defaultCodeToLevel[code]
if ok {
v := targerLvl(logger)
if v == nil {
return toNil
}
return v
}
return logger.Error
}
// DefaultCodeToLevel is the default implementation of gRPC return codes and interceptor log level for server side.
func WantToLog(code int, logger log.Logger) bool {
targerLvl, ok := defaultCodeToLevel[code]
if ok {
return targerLvl(logger) != nil
}
return true
}
type LogOptions struct {
logger log.Logger
}
func NewLogOptions() *LogOptions {
return &LogOptions{
logger: log.Get(),
}
}
type LogOpt = func(*LogOptions)
func WithLogger(logger log.Logger) LogOpt {
return func(c *LogOptions) {
c.logger = logger
}
}
var (
logDurationKey = log.DurationMSKey
logStartTimeKey = log.StartTimeKey
)
type jwtMember struct {
Sub string `json:"sub,omitempty"`
}
type request struct {
Href string `json:"href,omitempty"`
JWT *jwtMember `json:"jwt,omitempty"`
Method string `json:"method,omitempty"`
}
type response struct {
Code int `json:"code,omitempty"`
Error string `json:"error,omitempty"`
}
func createLogRequest(r *http.Request) *request {
bearer := r.Header.Get("Authorization")
req := request{
Method: r.Method,
Href: r.RequestURI,
}
token := strings.SplitN(bearer, " ", 2)
if len(token) == 2 && strings.ToLower(token[0]) == "bearer" {
claims, err := pkgJwt.ParseToken(token[1])
if err != nil {
return &req
}
subject, err := claims.GetSubject()
if err != nil {
return &req
}
req.JWT = &jwtMember{
Sub: subject,
}
}
return &req
}
func CreateLoggingMiddleware(opts ...LogOpt) func(next http.Handler) http.Handler {
cfg := NewLogOptions()
for _, o := range opts {
o(cfg)
}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
sw := statusWriter{ResponseWriter: w}
next.ServeHTTP(&sw, r)
duration := time.Since(start)
logger := cfg.logger
if !WantToLog(sw.status, logger) {
return
}
req := createLogRequest(r)
errStr := ""
if sw.err != nil {
errStr = sw.err.Error()
}
resp := &response{
Code: sw.status,
Error: errStr,
}
spanCtx := trace.SpanContextFromContext(r.Context())
if spanCtx.HasTraceID() {
logger = logger.With(log.TraceIDKey, spanCtx.TraceID().String())
}
logger = logger.With(logDurationKey, log.DurationToMilliseconds(duration), log.RequestKey, req, log.ResponseKey, resp, logStartTimeKey, start, log.ProtocolKey, "HTTP")
if deadline, ok := r.Context().Deadline(); ok {
logger = logger.With(log.DeadlineKey, deadline)
}
doLog := DefaultCodeToLevel(sw.status, logger)
doLog("finished unary call with status code ", http.StatusText(sw.status))
})
}
}