-
Notifications
You must be signed in to change notification settings - Fork 16
/
logging_test.go
380 lines (325 loc) · 8.86 KB
/
logging_test.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package tests
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httputil"
"os"
"path/filepath"
"runtime"
"runtime/pprof"
"strings"
"sync"
"sync/atomic"
"testing"
ravendb "github.com/ravendb/ravendb-go-client"
)
var (
// if true, printing of failed reqeusts is delayed until PrintFailedRequests
// is called.
// can be enabled by setting LOG_FAILED_HTTP_REQUESTS_DELAYED env variable to "true"
logFailedRequestsDelayed = false
// if true, logs all http requests/responses to a file for further inspection
// this is for use in tests so the file has a fixed location:
// logs/trace_${test_name}_go.txt
logAllRequests = false
// if true, we log RavenDB's output to stdout
// can be enabled by setting LOG_RAVEN_SERVER env variable to "true"
ravenServerVerbose = false
// if true, logs summary of all HTTP requests i.e. "GET /foo" to stdout
logRequestSummary = false
// if true, logs request and response of failed http requests (i.e. those returning
// status code >= 400) to stdout
logFailedRequests = false
logTopology = false
// testFileLog is a per-test file logs/trace_${test_name}_go.txt where we log all http requests, responses and
// other stuff
testFileLog io.WriteCloser
// httpFailedRequestsLogger is where we log failed http requests.
// it's either os.Stdout for immediate logging or bytes.Buffer for delayed logging
httpFailedRequestsLogger io.Writer
// httpRequestCount numbers http requests which helps to match http
// traffic from java client with go client
httpRequestCount int32
errLogDisabled int32 // atomic, if > 0, don't log error requests
muLog sync.Mutex
)
func logsLock() {
muLog.Lock()
}
func logsUnlock() {
muLog.Unlock()
}
func logToPerTestFile(format string, args ...interface{}) {
logsLock()
defer logsUnlock()
if testFileLog == nil {
return
}
s := fmt.Sprintf(format, args...)
_, _ = testFileLog.Write([]byte(s))
}
// this logs to both stdout and to a per-test file
func lg(format string, args ...interface{}) {
fmt.Printf(format, args...)
logToPerTestFile(format, args...)
}
func logTestName() {
/* print the name of the calling function, which we assume is a test */
pc := make([]uintptr, 16)
n := runtime.Callers(2, pc)
if n == 0 {
return
}
frames := runtime.CallersFrames(pc[:n])
frame, _ := frames.Next()
fn := frame.Function
parts := strings.Split(fn, ".")
n = len(parts)
if n > 0 {
fn = parts[n-1]
}
lg("Test: %s\n", fn)
}
func setLoggingStateFromEnv() {
if !ravenServerVerbose && isEnvVarTrue("LOG_RAVEN_SERVER") {
ravenServerVerbose = true
fmt.Printf("Setting ravenServerVerbose to true\n")
}
if !logFailedRequestsDelayed && isEnvVarTrue("LOG_FAILED_HTTP_REQUESTS_DELAYED") {
logFailedRequestsDelayed = true
fmt.Printf("Setting logFailedRequestsDelayed to true\n")
}
if !ravendb.LogVerbose && isEnvVarTrue("VERBOSE_LOG") {
ravendb.LogVerbose = true
fmt.Printf("Setting LogVerbose to true\n")
}
if !logRequestSummary && isEnvVarTrue("LOG_HTTP_REQUEST_SUMMARY") {
logRequestSummary = true
fmt.Printf("Setting LogRequestSummary to true\n")
}
if !logFailedRequests && isEnvVarTrue("LOG_FAILED_HTTP_REQUESTS") {
logFailedRequests = true
fmt.Printf("Setting LogFailedRequests to true\n")
}
if !logAllRequests && isEnvVarTrue("LOG_ALL_REQUESTS") {
logAllRequests = true
fmt.Printf("Setting logAllRequests to true\n")
}
if !logTopology && isEnvVarTrue("LOG_TOPOLOGY") {
logTopology = true
fmt.Printf("Setting logTopology to true\n")
}
}
type loggingTransport struct {
originalTransport http.RoundTripper
}
func (t *loggingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
atomic.AddInt32(&httpRequestCount, 1)
maybeLogRequestSummary(req)
maybeCaptureRequestBody(req)
rsp, err := t.originalTransport.RoundTrip(req)
maybeLogFailedResponse(req, rsp, err)
maybeLogHTTPRequest(req, rsp, err)
return rsp, err
}
func httpClientProcessor(c *http.Client) {
t := c.Transport
c.Transport = &loggingTransport{
originalTransport: t,
}
}
func httpLogPathFromTestName(t *testing.T) string {
name := "trace_" + testNameToFileName(t.Name()) + "_go.txt"
return filepath.Join(getLogDir(), name)
}
func logSubscriptionWorker(op string, d []byte) {
logToPerTestFile("SubscriptionWorker: op: %s, data:\n%s\n", op, string(d))
}
func setupLogging(t *testing.T) {
logsLock()
defer logsUnlock()
ravendb.DebugTopology = logTopology
ravendb.HTTPClientPostProcessor = httpClientProcessor
ravendb.LogSubscriptionWorker = logSubscriptionWorker
testFileLog = nil
if logAllRequests {
var err error
path := httpLogPathFromTestName(t)
f, err := os.Create(path)
if err != nil {
fmt.Printf("os.Create('%s') failed with %s\n", path, err)
} else {
fmt.Printf("Logging HTTP traffic to %s\n", path)
testFileLog = f
}
}
httpFailedRequestsLogger = nil
if logFailedRequests {
if logFailedRequestsDelayed {
httpFailedRequestsLogger = bytes.NewBuffer(nil)
} else {
httpFailedRequestsLogger = os.Stdout
}
}
}
func finishLogging() {
logsLock()
defer logsUnlock()
w := testFileLog
if w != nil {
_ = w.Close()
testFileLog = nil
}
}
func isErrLoggingDisabled() bool {
n := atomic.LoadInt32(&errLogDisabled)
return n > 0
}
// for temporarily disabling logging of failed requests (if a given
// test is known to issue failing requests)
// usage: defer disableLogFailedRequests()()
// or:
// restorer := disableLogFailedRequests()
// ...
// restorer()
// this is not perfect in parallel tests because (it might over-disable)
// but we're not doing parallel tests
func disableLogFailedRequests() func() {
atomic.AddInt32(&errLogDisabled, 1)
return func() {
atomic.AddInt32(&errLogDisabled, -1)
}
}
// returns copy of resp.Body but also makes it available for subsequent reads
func getCopyOfResponseBody(resp *http.Response) ([]byte, error) {
if resp == nil {
return nil, nil
}
d, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
resp.Body = ioutil.NopCloser(bytes.NewBuffer(d))
return d, nil
}
func logRequestAndResponseToWriter(w io.Writer, req *http.Request, rsp *http.Response, reqErr error) {
n := atomic.LoadInt32(&httpRequestCount)
fmt.Fprintf(w, "=========== %d:\n", n)
if reqErr != nil {
fmt.Fprintf(w, "%s\n", reqErr)
}
d, err := httputil.DumpRequest(req, false)
if err == nil {
_, _ = w.Write(d)
}
if req.Body != nil {
if cr, ok := req.Body.(*CapturingReadCloser); ok {
body := cr.capturedData.Bytes()
if len(body) > 0 {
fmt.Fprintf(w, "Request body %d bytes:\n%s\n", len(body), maybePrettyPrintJSON(body))
}
} else {
fmt.Fprint(w, "Can't get request body\n")
}
}
if reqErr != nil {
return
}
if rsp == nil {
fmt.Fprint(w, "No response\n")
return
}
fmt.Fprint(w, "--------\n")
d, err = httputil.DumpResponse(rsp, false)
if err == nil {
_, _ = w.Write(d)
}
if d, err := getCopyOfResponseBody(rsp); err != nil {
fmt.Fprintf(w, "Failed to read response body. Error: '%s'\n", err)
} else {
if len(d) > 0 {
fmt.Fprintf(w, "Response body %d bytes:\n%s\n", len(d), maybePrettyPrintJSON(d))
}
}
}
func maybePrintFailedRequestsLog() {
logsLock()
defer logsUnlock()
if logFailedRequests && logFailedRequestsDelayed {
buf := httpFailedRequestsLogger.(*bytes.Buffer)
_, _ = os.Stdout.Write(buf.Bytes())
buf.Reset()
}
}
func maybeLogHTTPRequest(req *http.Request, rsp *http.Response, err error) {
logsLock()
defer logsUnlock()
if testFileLog == nil {
return
}
logRequestAndResponseToWriter(testFileLog, req, rsp, err)
}
func maybeLogFailedResponse(req *http.Request, rsp *http.Response, err error) {
logsLock()
defer logsUnlock()
if !logFailedRequests || isErrLoggingDisabled() {
return
}
if err == nil && rsp.StatusCode < 400 {
// not failed
return
}
logRequestAndResponseToWriter(httpFailedRequestsLogger, req, rsp, err)
}
// to be able to print request body for failed requests, we must replace
// body with one that captures data read from original body.
func maybeCaptureRequestBody(req *http.Request) {
shouldCapture := (logFailedRequests && !isErrLoggingDisabled()) || (testFileLog != nil)
if !shouldCapture {
return
}
switch req.Method {
case http.MethodGet, http.MethodHead, "RESET":
// just in case (probably redundant with req.Bddy != nil check)
return
}
if req.Body != nil {
req.Body = NewCapturingReadCloser(req.Body)
}
}
func maybeLogRequestSummary(req *http.Request) {
if !logRequestSummary {
return
}
method := req.Method
uri := req.URL.String()
lg("%s %s\n", method, uri)
}
// This helps debugging leaking gorutines by dumping stack traces
// of all goroutines to a file
func logGoroutines(file string) {
if file == "" {
file = "goroutines.txt"
}
path := filepath.Join("logs", file)
dir := filepath.Dir(path)
err := os.MkdirAll(dir, 0755)
if err != nil {
return
}
profile := pprof.Lookup("goroutine")
if profile == nil {
return
}
f, err := os.Create(path)
if err != nil {
return
}
defer func() {
_ = f.Close()
}()
_ = profile.WriteTo(f, 2)
}