Skip to content

Commit

Permalink
logging: don't log request start, just log end
Browse files Browse the repository at this point in the history
Signed-off-by: Sumner Evans <sumner@beeper.com>
  • Loading branch information
sumnerevans committed Jun 26, 2023
1 parent a29f052 commit c067a37
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,18 +258,10 @@ const (
LogRequestIDContextKey
)

func (cli *Client) LogRequest(req *http.Request) {
func (cli *Client) RequestStart(req *http.Request) {
if cli.RequestHook != nil {
cli.RequestHook(req)
}
evt := zerolog.Ctx(req.Context()).Debug().
Str("method", req.Method).
Str("url", req.URL.String())
body := req.Context().Value(LogBodyContextKey)
if body != nil {
evt.Interface("body", body)
}
evt.Msg("Sending request")
}

func (cli *Client) LogRequestDone(req *http.Request, resp *http.Response, handlerErr error, contentLength int, duration time.Duration) {
Expand All @@ -285,11 +277,15 @@ func (cli *Client) LogRequestDone(req *http.Request, resp *http.Response, handle
path = strings.TrimPrefix(path, "/_matrix/client")
evt := zerolog.Ctx(req.Context()).Debug().
Str("method", req.Method).
Str("url", req.URL.String()).
Str("path", path).
Int("status_code", resp.StatusCode).
Int64("response_length", length).
Str("response_mime", mime).
Dur("duration", duration)
if body := req.Context().Value(LogBodyContextKey); body != nil {
evt.Interface("body", body)
}
if handlerErr != nil {
evt.AnErr("body_parse_err", handlerErr)
}
Expand Down Expand Up @@ -551,7 +547,7 @@ func (cli *Client) shouldRetry(res *http.Response) bool {
}

func (cli *Client) executeCompiledRequest(req *http.Request, retries int, backoff time.Duration, responseJSON interface{}, handler ClientResponseHandler) ([]byte, error) {
cli.LogRequest(req)
cli.RequestStart(req)
startTime := time.Now()
res, err := cli.Client.Do(req)
duration := time.Now().Sub(startTime)
Expand Down Expand Up @@ -1388,14 +1384,18 @@ func (cli *Client) downloadContext(ctx context.Context, mxcURL id.ContentURI) (*
return nil, err
}
req.Header.Set("User-Agent", cli.UserAgent+" (media downloader)")
cli.LogRequest(req)
cli.RequestStart(req)
downloadStart := time.Now()
if resp, err := cli.Client.Do(req); err != nil {
cli.LogRequestDone(req, resp, err, 0, time.Since(downloadStart))
return nil, err
} else if resp.StatusCode < 200 || resp.StatusCode >= 300 {
_, err = ParseErrorResponse(req, resp)
_ = resp.Body.Close()
cli.LogRequestDone(req, resp, err, 0, time.Since(downloadStart))
return nil, err
} else {
cli.LogRequestDone(req, resp, err, -1, time.Since(downloadStart))
return resp, nil
}
}
Expand Down

0 comments on commit c067a37

Please sign in to comment.