/
tracer.go
54 lines (46 loc) · 1.26 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
48
49
50
51
52
53
54
package kube
import (
"fmt"
"net/http"
"net/http/httputil"
"os"
"regexp"
)
// Tracer implements http.RoundTripper. It prints each request and
// response/error to os.Stderr.
type Tracer 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.
func (t *Tracer) 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(t.sanitize(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("---"))
os.Stderr.Write([]byte{'\n'})
return resp, err
}
func (t *Tracer) sanitize(raw []byte) []byte {
s := string(raw)
regExp := regexp.MustCompile("Authorization: Bearer .*\n")
s = regExp.ReplaceAllString(s, "Authorization: Bearer xxx\n")
return []byte(s)
}