/
tracer.go
47 lines (40 loc) · 1.17 KB
/
tracer.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
package http
import (
"fmt"
"net/http"
"net/http/httputil"
"os"
)
// verboseTracer implements http.RoundTripper. It prints each request and
// response/error to os.Stderr. WARNING: this may output sensitive information
// including bearer tokens.
type verboseTracer struct {
http.RoundTripper
}
// RoundTrip calls the nested RoundTripper while printing each request and
// response/error to os.Stderr on either side of the nested call. WARNING: this
// may output sensitive information including bearer tokens.
func (t *verboseTracer) RoundTrip(req *http.Request) (*http.Response, error) {
// Dump the request to os.Stderr.
b, err := httputil.DumpRequestOut(req, true)
if err != nil {
return nil, err
}
os.Stderr.Write(b)
os.Stderr.Write([]byte{'\n'})
// Call the nested RoundTripper.
resp, err := t.RoundTripper.RoundTrip(req)
// If an error was returned, dump it to os.Stderr.
if err != nil {
fmt.Fprintln(os.Stderr, err)
return resp, err
}
// Dump the response to os.Stderr.
b, err = httputil.DumpResponse(resp, req.URL.Query().Get("watch") != "true")
if err != nil {
return nil, err
}
os.Stderr.Write(b)
os.Stderr.Write([]byte{'\n'})
return resp, err
}