Skip to content

Commit

Permalink
Log errors when write fails, not 200
Browse files Browse the repository at this point in the history
See discussion here: cortexproject/cortex#2483 (comment)

Signed-off-by: Goutham Veeramachaneni <gouthamve@gmail.com>
  • Loading branch information
gouthamve committed Apr 20, 2020
1 parent 4f5616c commit d18f518
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
7 changes: 6 additions & 1 deletion middleware/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ func (l Log) Wrap(next http.Handler) http.Handler {
var buf bytes.Buffer
wrapped := newBadResponseLoggingWriter(w, &buf)
next.ServeHTTP(wrapped, r)
statusCode := wrapped.statusCode

statusCode, err := wrapped.statusCode, wrapped.writeError
if err != nil {
l.logWithRequest(r).Warnf("%s %s %s, error: %s ws: %v; %s", r.Method, uri, time.Since(begin), err, IsWSHandshakeRequest(r), headers)
return
}
if 100 <= statusCode && statusCode < 500 || statusCode == http.StatusBadGateway || statusCode == http.StatusServiceUnavailable {
l.logWithRequest(r).Debugf("%s %s (%d) %s", r.Method, uri, statusCode, time.Since(begin))
if l.LogRequestHeaders && headers != nil {
Expand Down
4 changes: 4 additions & 0 deletions middleware/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type badResponseLoggingWriter struct {
logBody bool
bodyBytesLeft int
statusCode int
writeError error // The error returned when downstream Write() fails.
}

// newBadResponseLoggingWriter makes a new badResponseLoggingWriter.
Expand Down Expand Up @@ -50,6 +51,9 @@ func (b *badResponseLoggingWriter) Write(data []byte) (int, error) {
if b.logBody {
b.captureResponseBody(data)
}
if err != nil {
b.writeError = err
}
return n, err
}

Expand Down

0 comments on commit d18f518

Please sign in to comment.