Repository navigation
Expand file tree
/
Copy pathhttp.go
More file actions
210 lines (173 loc) · 4.7 KB
/
Copy pathhttp.go
File metadata and controls
210 lines (173 loc) · 4.7 KB
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
package fly
import (
"bytes"
"context"
"io"
"math"
"mime"
"net/http"
"strings"
"sync"
"time"
"github.com/PuerkitoBio/rehttp"
)
func NewHTTPClient(logger Logger, transport http.RoundTripper) (*http.Client, error) {
retryTransport := rehttp.NewTransport(
transport,
rehttp.RetryAll(
func(attempt rehttp.Attempt) bool {
return !retriesDisabled(attempt.Request.Context())
},
rehttp.RetryMaxRetries(3),
rehttp.RetryAny(
rehttp.RetryTemporaryErr(),
rehttp.RetryStatuses(502, 503),
),
),
rehttp.ExpJitterDelay(100*time.Millisecond, 1*time.Second),
)
if logger != nil {
return &http.Client{
Transport: &LoggingTransport{
InnerTransport: retryTransport,
Logger: logger,
},
}, nil
}
return &http.Client{
Transport: retryTransport,
}, nil
}
type LoggingTransport struct {
InnerTransport http.RoundTripper
Logger Logger
mu sync.Mutex
}
func (t *LoggingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
ctx := context.WithValue(req.Context(), contextKeyRequestStart, time.Now())
req = req.WithContext(ctx)
t.logRequest(req)
resp, err := t.InnerTransport.RoundTrip(req)
if err != nil {
return resp, err
}
t.logResponse(resp)
return resp, err
}
func (t *LoggingTransport) logRequest(req *http.Request) {
t.mu.Lock()
defer t.mu.Unlock()
t.Logger.Debugf("--> %s %s\n", req.Method, req.URL)
if req.Body == nil {
return
}
defer func() { _ = req.Body.Close() }()
data, err := io.ReadAll(req.Body)
if err != nil {
t.Logger.Debug("error reading request body:", err)
} else {
t.Logger.Debug(string(redactJSON(data)))
}
req.Body = io.NopCloser(bytes.NewReader(data))
}
func (t *LoggingTransport) logResponse(resp *http.Response) {
t.mu.Lock()
defer t.mu.Unlock()
ctx := resp.Request.Context()
if start, ok := ctx.Value(contextKeyRequestStart).(time.Time); ok {
t.Logger.Debugf("<-- %d %s (%s)\n", resp.StatusCode, resp.Request.URL, shiftedDuration(time.Since(start), 2))
} else {
t.Logger.Debugf("<-- %d %s\n", resp.StatusCode, resp.Request.URL)
}
// A successful credentials response may contain secrets under fields that
// generic JSON key redaction does not recognize. Suppress its body entirely.
if resp.StatusCode < 300 && hasSensitiveResponseBody(resp.Request.Context()) {
t.Logger.Debugf(" <-- %s: [response body redacted: contains credentials]\n", resp.Request.URL)
return
}
// Wrap the body so reads are logged without buffering what the caller sees or closing early.
// JSON bodies are logged whole once fully read so credential fields can be redacted;
// anything else is logged as it streams.
resp.Body = &loggingReadCloser{
rc: resp.Body,
logger: t.Logger,
requestURL: resp.Request.URL.String(),
json: isJSONContentType(resp.Header.Get("Content-Type")),
}
}
// maxLoggedResponseBody caps how much of a JSON response is held back for redacted logging.
const maxLoggedResponseBody = 1 << 20
// loggingReadCloser logs bytes as they are read from the underlying ReadCloser.
// The caller always receives the bytes immediately; only the log write is deferred for JSON bodies.
type loggingReadCloser struct {
requestURL string
rc io.ReadCloser
logger Logger
json bool
buf bytes.Buffer
truncated bool
logged bool
}
func (l *loggingReadCloser) Read(p []byte) (int, error) {
n, err := l.rc.Read(p)
if n > 0 {
if l.json {
l.buffer(p[:n])
} else {
l.logger.Debugf(" <-- %s: %s", l.requestURL, string(p[:n]))
}
}
if err != nil {
l.flush()
}
return n, err
}
func (l *loggingReadCloser) buffer(data []byte) {
if l.truncated {
return
}
if l.buf.Len()+len(data) > maxLoggedResponseBody {
l.truncated = true
l.buf.Reset()
return
}
l.buf.Write(data)
}
func (l *loggingReadCloser) flush() {
if !l.json || l.logged {
return
}
l.logged = true
switch {
case l.truncated:
l.logger.Debugf(" <-- %s: [body over %d bytes not logged]", l.requestURL, maxLoggedResponseBody)
case l.buf.Len() > 0:
l.logger.Debugf(" <-- %s: %s", l.requestURL, string(redactJSON(l.buf.Bytes())))
}
}
func (l *loggingReadCloser) Close() error {
l.flush()
return l.rc.Close()
}
func isJSONContentType(contentType string) bool {
mediaType, _, err := mime.ParseMediaType(contentType)
if err != nil {
return false
}
return mediaType == "application/json" || strings.HasSuffix(mediaType, "+json")
}
func shiftedDuration(d time.Duration, dicimal int) time.Duration {
shift := int(math.Pow10(dicimal))
units := []time.Duration{time.Second, time.Millisecond, time.Microsecond, time.Nanosecond}
for _, u := range units {
if d > u {
div := u / time.Duration(shift)
if div == 0 {
break
}
d = d / div * div
break
}
}
return d
}