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 27, 2023
1 parent a29f052 commit a8b92d1
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"net/url"
"os"
"strconv"
"strings"
"sync/atomic"
"time"

Expand Down Expand Up @@ -258,18 +257,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 @@ -281,15 +272,16 @@ func (cli *Client) LogRequestDone(req *http.Request, resp *http.Response, handle
if length == -1 && contentLength > 0 {
length = int64(contentLength)
}
path := strings.TrimPrefix(req.URL.Path, cli.HomeserverURL.Path)
path = strings.TrimPrefix(path, "/_matrix/client")
evt := zerolog.Ctx(req.Context()).Debug().
Str("method", req.Method).
Str("path", path).
Str("url", req.URL.String()).
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 +543,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 +1380,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 a8b92d1

Please sign in to comment.