Skip to content

Commit

Permalink
client/LogRequestDone: handle nil resp
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 a8b92d1 commit 36b70fb
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,30 +264,32 @@ func (cli *Client) RequestStart(req *http.Request) {
}

func (cli *Client) LogRequestDone(req *http.Request, resp *http.Response, handlerErr error, contentLength int, duration time.Duration) {
if cli.ResponseHook != nil {
cli.ResponseHook(req, resp, duration)
}
mime := resp.Header.Get("Content-Type")
length := resp.ContentLength
if length == -1 && contentLength > 0 {
length = int64(contentLength)
}
evt := zerolog.Ctx(req.Context()).Debug().
Str("method", req.Method).
Str("url", req.URL.String()).
Int("status_code", resp.StatusCode).
Int64("response_length", length).
Str("response_mime", mime).
Dur("duration", duration)
if resp != nil {
if cli.ResponseHook != nil {
cli.ResponseHook(req, resp, duration)
}
mime := resp.Header.Get("Content-Type")
length := resp.ContentLength
if length == -1 && contentLength > 0 {
length = int64(contentLength)
}
evt = evt.Int("status_code", resp.StatusCode).
Int64("response_length", length).
Str("response_mime", mime)
if serverRequestID := resp.Header.Get("X-Beeper-Request-ID"); serverRequestID != "" {
evt.Str("beeper_request_id", serverRequestID)
}
}
if body := req.Context().Value(LogBodyContextKey); body != nil {
evt.Interface("body", body)
evt.Interface("req_body", body)
}
if handlerErr != nil {
evt.AnErr("body_parse_err", handlerErr)
}
if serverRequestID := resp.Header.Get("X-Beeper-Request-ID"); serverRequestID != "" {
evt.Str("beeper_request_id", serverRequestID)
}
evt.Msg("Request completed")
}

Expand Down

0 comments on commit 36b70fb

Please sign in to comment.