forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.go
143 lines (121 loc) · 3.09 KB
/
logging.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
// Copyright 2016 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package runtime
import (
"bytes"
"io"
"net/http"
"net/url"
"sync/atomic"
"time"
"io/ioutil"
"github.com/Sirupsen/logrus"
"github.com/open-policy-agent/opa/server/types"
)
// DebugLogging returns true if log verbosity is high enough to emit debug
// messages.
func DebugLogging() bool {
return logrus.DebugLevel >= logrus.GetLevel()
}
// LoggingHandler returns an http.Handler that will print log messages
// containing the request information as well as response status and latency.
type LoggingHandler struct {
inner http.Handler
requestID uint64
}
// NewLoggingHandler returns a new http.Handler.
func NewLoggingHandler(inner http.Handler) http.Handler {
return &LoggingHandler{inner, uint64(0)}
}
func (h *LoggingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
recorder := newRecorder(w)
t0 := time.Now()
requestID := atomic.AddUint64(&h.requestID, uint64(1))
if DebugLogging() {
var bs []byte
var err error
if r.Body != nil {
bs, r.Body, err = readBody(r.Body)
}
if err == nil {
logrus.WithFields(logrus.Fields{
"client_addr": r.RemoteAddr,
"req_id": requestID,
"req_method": r.Method,
"req_path": r.URL.Path,
"req_params": r.URL.Query(),
"req_body": string(bs),
}).Debug("Received request.")
} else {
logrus.WithFields(logrus.Fields{
"client_addr": r.RemoteAddr,
"req_id": requestID,
"req_method": r.Method,
"req_path": r.URL.Path,
"req_params": r.URL.Query(),
"err": err,
}).Error("Failed to read body.")
}
}
h.inner.ServeHTTP(recorder, r)
dt := time.Since(t0)
statusCode := 200
if recorder.statusCode != 0 {
statusCode = recorder.statusCode
}
if DebugLogging() {
logrus.WithFields(logrus.Fields{
"client_addr": r.RemoteAddr,
"req_id": requestID,
"req_method": r.Method,
"req_path": r.URL.Path,
"resp_status": statusCode,
"resp_bytes": recorder.bytesWritten,
"resp_duration": float64(dt.Nanoseconds()) / 1e6,
}).Debug("Sent response.")
}
}
type recorder struct {
inner http.ResponseWriter
bytesWritten int
statusCode int
}
func newRecorder(w http.ResponseWriter) *recorder {
return &recorder{
inner: w,
}
}
func (r *recorder) Header() http.Header {
return r.inner.Header()
}
func (r *recorder) Write(bs []byte) (int, error) {
r.bytesWritten += len(bs)
return r.inner.Write(bs)
}
func (r *recorder) WriteHeader(s int) {
r.statusCode = s
r.inner.WriteHeader(s)
}
func dropInputParam(u *url.URL) string {
cpy := url.Values{}
for k, v := range u.Query() {
if k != types.ParamInputV1 {
cpy[k] = v
}
}
if len(cpy) == 0 {
return u.Path
}
return u.Path + "?" + cpy.Encode()
}
func readBody(r io.ReadCloser) ([]byte, io.ReadCloser, error) {
if r == http.NoBody {
return nil, r, nil
}
var buf bytes.Buffer
if _, err := buf.ReadFrom(r); err != nil {
return nil, r, err
}
return buf.Bytes(), ioutil.NopCloser(bytes.NewReader(buf.Bytes())), nil
}