Skip to content

Commit

Permalink
fix: gracefully handle empty request body (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
kruskall authored Oct 23, 2023
1 parent 82201b9 commit 39e2a3c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
8 changes: 8 additions & 0 deletions input/elasticapm/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import (
var (
errUnrecognizedObject = errors.New("did not recognize object type")

errEmptyBody = errors.New("empty body")

// ErrQueueFull may be returned by HandleStream when the internal
// queue is full.
ErrQueueFull = errors.New("queue is full")
Expand Down Expand Up @@ -114,6 +116,9 @@ func (p *Processor) readMetadata(reader *streamReader, out *modelpb.APMEvent) er
body, err := reader.ReadAhead()
if err != nil {
if err == io.EOF {
if len(reader.LatestLine()) == 0 {
return errEmptyBody
}
return &InvalidInputError{
Message: "EOF while reading metadata",
Document: string(reader.LatestLine()),
Expand Down Expand Up @@ -274,6 +279,9 @@ func (p *Processor) HandleStream(

// The first item is the metadata object.
if err := p.readMetadata(sr, baseEvent); err != nil {
if err == errEmptyBody {
return nil
}
// no point in continuing if we couldn't read the metadata
if _, ok := err.(*InvalidInputError); ok {
return err
Expand Down
2 changes: 2 additions & 0 deletions input/elasticapm/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ func TestHandleStreamErrors(t *testing.T) {
Document: invalidEventType,
},
},
}, {
name: "EmptyEvent",
}, {
name: "TooLargeEvent",
payload: validMetadata + "\n" + tooLargeEvent + "\n",
Expand Down

0 comments on commit 39e2a3c

Please sign in to comment.