Skip to content

Commit

Permalink
Add panic recover to JS script processor (#13746) (#13898)
Browse files Browse the repository at this point in the history
To help debug panics in the script processor add a `recover()` statement that will log the event being processed.

(cherry picked from commit 256e112)
  • Loading branch information
andrewkroh authored Oct 7, 2019
1 parent 14ae397 commit 745af74
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
*Affecting all Beats*

- Fix a race condition with the Kafka pipeline client, it is possible that `Close()` get called before `Connect()` . {issue}11945[11945]
- Recover from panics in the javascript process and log details about the failure to aid in future debugging. {pull}13690[13690]
- Make the script processor concurrency-safe. {issue}13690[13690] {pull}13857[13857]

*Auditbeat*
Expand Down
22 changes: 20 additions & 2 deletions libbeat/processors/script/javascript/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/dop251/goja"
"github.com/pkg/errors"
"go.uber.org/zap"

"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/common"
Expand Down Expand Up @@ -197,8 +198,25 @@ func (s *session) setEvent(b *beat.Event) error {
}

// runProcessFunc executes process() from the JS script.
func (s *session) runProcessFunc(b *beat.Event) (*beat.Event, error) {
var err error
func (s *session) runProcessFunc(b *beat.Event) (out *beat.Event, err error) {
defer func() {
if r := recover(); r != nil {
s.log.Errorw("The javascript processor caused an unexpected panic "+
"while processing an event. Recovering, but please report this.",
"event", common.MapStr{"original": b.Fields.String()},
"panic", r,
zap.Stack("stack"))
if !s.evt.IsCancelled() {
out = b
}
err = errors.Errorf("unexpected panic in javascript processor: %v", r)
if s.tagOnException != "" {
common.AddTags(b.Fields, []string{s.tagOnException})
}
appendString(b.Fields, "error.message", err.Error(), false)
}
}()

if err = s.setEvent(b); err != nil {
// Always return the event even if there was an error.
return b, err
Expand Down

0 comments on commit 745af74

Please sign in to comment.