Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add elapsed to debuglog #144

Merged
merged 1 commit into from
Mar 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions debuglog/roundtripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"log"
"net/http"
"strings"
"time"
)

// Config is the input data for the logger.
Expand Down Expand Up @@ -46,6 +47,7 @@ type fakeCloser struct {
URL string
Status string
Header http.Header
Elapsed time.Duration
*Config
}

Expand Down Expand Up @@ -75,6 +77,8 @@ func (rt *LoggingRoundTripper) RoundTrip(req *http.Request) (*http.Response, err
defer req.Body.Close()
}

start := time.Now()

resp, err := rt.next.RoundTrip(req)
if err != nil {
if rt.config.Caller != nil {
Expand All @@ -85,12 +89,12 @@ func (rt *LoggingRoundTripper) RoundTrip(req *http.Request) (*http.Response, err
return resp, err //nolint:wrapcheck
}

resp.Body = rt.newFakeCloser(resp, &buf)
resp.Body = rt.newFakeCloser(resp, &buf, start)

return resp, nil
}

func (rt *LoggingRoundTripper) newFakeCloser(resp *http.Response, sent *bytes.Buffer) io.ReadCloser {
func (rt *LoggingRoundTripper) newFakeCloser(resp *http.Response, sent *bytes.Buffer, start time.Time) io.ReadCloser {
var buf bytes.Buffer

return &fakeCloser{
Expand All @@ -102,6 +106,7 @@ func (rt *LoggingRoundTripper) newFakeCloser(resp *http.Response, sent *bytes.Bu
URL: resp.Request.URL.String(),
Sent: sent,
Header: resp.Header,
Elapsed: time.Since(start),
Config: rt.config,
}
}
Expand Down Expand Up @@ -137,11 +142,13 @@ func (f *fakeCloser) logRequest() (int, int) {
}

if sentBytes > 0 {
f.redactLog("Sent (%s) %d bytes to %s: %s\n Response: %s %d bytes\n%s%s)",
f.Method, sentBytes, f.URL, sent, f.Status, rcvdBytes, f.headers(), rcvd)
f.redactLog("Sent (%s) %d bytes to %s in %s: %s\n Response: %s %d bytes\n%s%s)",
f.Method, sentBytes, f.URL, f.Elapsed.Round(time.Millisecond),
sent, f.Status, rcvdBytes, f.headers(), rcvd)
} else {
f.redactLog("Sent (%s) to %s, Response: %s %d bytes\n%s%s",
f.Method, f.URL, f.Status, rcvdBytes, f.headers(), rcvd)
f.redactLog("Sent (%s) to %s in %s, Response: %s %d bytes\n%s%s",
f.Method, f.URL, f.Elapsed.Round(time.Millisecond),
f.Status, rcvdBytes, f.headers(), rcvd)
}

return sentBytes, rcvdBytes
Expand Down
Loading