Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Zheng <[email protected]>
  • Loading branch information
Two-Hearts committed Oct 11, 2024
1 parent 6f81322 commit 1a0186b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
17 changes: 9 additions & 8 deletions internal/trace/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error
var logs strings.Builder
fmt.Fprintf(&logs, "> Request: %q %q\n", req.Method, req.URL)
fmt.Fprintln(&logs, "> Request headers:")
fmt.Fprint(&logs, logHeader(req.Header))
logHeader(req.Header, &logs)

resp, err = t.RoundTripper.RoundTrip(req)
if err != nil {
Expand All @@ -70,26 +70,27 @@ func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error
} else {
fmt.Fprintf(&logs, "< Response status: %q\n", resp.Status)
fmt.Fprintln(&logs, "< Response headers:")
fmt.Fprint(&logs, logHeader(resp.Header))
logHeader(resp.Header, &logs)
e.Debug(logs.String())
}
return resp, err
}

// logHeader returns string of provided header keys and values, with auth header
// logHeader logs the provided header keys and values, with auth header
// scrubbed.
func logHeader(header http.Header) string {
func logHeader(header http.Header, logs *strings.Builder) {
if logs == nil {
return
}
if len(header) > 0 {
var logs strings.Builder
for k, v := range header {
if strings.EqualFold(k, "Authorization") {
v = []string{"*****"}
}
fmt.Fprintf(&logs, " %q: %q\n", k, strings.Join(v, ", "))
fmt.Fprintf(logs, " %q: %q\n", k, strings.Join(v, ", "))
}
return logs.String()
} else {
return " Empty header\n"
fmt.Fprintln(logs, " Empty header")
}
}

Expand Down
4 changes: 4 additions & 0 deletions internal/trace/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,7 @@ func TestTransport_RoundTrip(t *testing.T) {
}
})
}

func TestLogHeaderWithNilLogs(t *testing.T) {
logHeader(http.Header{}, nil)
}

0 comments on commit 1a0186b

Please sign in to comment.