Skip to content

Commit

Permalink
[Heartbeat] Add context to HTTP body read errors (#25499) (#25509)
Browse files Browse the repository at this point in the history
* [Heartbeat] Add context to HTTP body read errors

This change adds clarity to HTTP body read errors, wrapping the error
with a more identifiable message. Prior to this, you'd just get the low
level error, which made it hard to know exactly where it came from.

* more error improvements

* Add changelog

(cherry picked from commit dc4e874)

Co-authored-by: Andrew Cholakian <[email protected]>
  • Loading branch information
mergify[bot] and andrewvc authored Jun 22, 2021
1 parent e394272 commit c3b060f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Fixed excessive memory usage introduced in 7.5 due to over-allocating memory for HTTP checks. {pull}15639[15639]
- Fixed scheduler shutdown issues which would in rare situations cause a panic due to semaphore misuse. {pull}16397[16397]
- Fixed TCP TLS checks to properly validate hostnames, this broke in 7.x and only worked for IP SANs. {pull}17549[17549]
- Add Context to otherwise ambiguous HTTP body read errors. {pull}25499[25499]

*Journalbeat*

Expand Down
9 changes: 8 additions & 1 deletion heartbeat/monitors/active/http/respbody.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package http
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"net/http"
"strings"
Expand Down Expand Up @@ -58,7 +59,7 @@ func processBody(resp *http.Response, config responseConfig, validator multiVali
respBody, bodyLenBytes, bodyHash, respErr := readBody(resp, bufferBodyBytes)
// If we encounter an error while reading the body just fail early
if respErr != nil {
return nil, "", reason.IOFailed(respErr)
return nil, "", reason.IOFailed(fmt.Errorf("failed reading HTTP response body: %w", respErr))
}

// Run any validations
Expand Down Expand Up @@ -114,6 +115,12 @@ func readPrefixAndHash(body io.ReadCloser, maxPrefixSize int) (respSize int, pre
n += m
}

// The ErrUnexpectedEOF message can be confusing to users, so we clarify it here
if err == io.ErrUnexpectedEOF {
return 0, "", "", fmt.Errorf("connection closed unexpectedly: %w", err)
}

// Note that io.EOF is distinct from io.ErrUnexpectedEOF
if err != nil && err != io.EOF {
return 0, "", "", err
}
Expand Down

0 comments on commit c3b060f

Please sign in to comment.