From c8a9e88a0e1ac122ef1f35fbb23f96a10f21757e Mon Sep 17 00:00:00 2001 From: Ruben van Staden Date: Sun, 18 Aug 2024 21:21:02 -0400 Subject: [PATCH] set event message to error message if empty --- model/modelprocessor/errormessage.go | 3 + model/modelprocessor/errormessage_test.go | 76 +++++++++++++++++++---- 2 files changed, 67 insertions(+), 12 deletions(-) diff --git a/model/modelprocessor/errormessage.go b/model/modelprocessor/errormessage.go index 6094bbe9..1d4134e4 100644 --- a/model/modelprocessor/errormessage.go +++ b/model/modelprocessor/errormessage.go @@ -33,6 +33,9 @@ func (s SetErrorMessage) ProcessBatch(ctx context.Context, b *modelpb.Batch) err event := (*b)[i] if event.Error != nil { event.Error.Message = s.setErrorMessage(event) + if event.GetMessage() == "" { + event.Message = event.Error.Message + } } } return nil diff --git a/model/modelprocessor/errormessage_test.go b/model/modelprocessor/errormessage_test.go index dd94d82e..c03170c4 100644 --- a/model/modelprocessor/errormessage_test.go +++ b/model/modelprocessor/errormessage_test.go @@ -27,38 +27,90 @@ import ( "github.com/elastic/apm-data/model/modelprocessor" ) +// Copy the error message to the event message if event message is empty. func TestSetErrorMessage(t *testing.T) { tests := []struct { - input *modelpb.Error - message string + input *modelpb.Error + expected string }{{ - input: &modelpb.Error{}, - message: "", + input: &modelpb.Error{}, + expected: "", }, { - input: &modelpb.Error{Log: &modelpb.ErrorLog{Message: "log_message"}}, - message: "log_message", + input: &modelpb.Error{Log: &modelpb.ErrorLog{Message: "log_message"}}, + expected: "log_message", }, { - input: &modelpb.Error{Exception: &modelpb.Exception{Message: "exception_message"}}, - message: "exception_message", + input: &modelpb.Error{Exception: &modelpb.Exception{Message: "exception_message"}}, + expected: "exception_message", }, { input: &modelpb.Error{ Log: &modelpb.ErrorLog{}, Exception: &modelpb.Exception{Message: "exception_message"}, }, - message: "exception_message", + expected: "exception_message", }, { input: &modelpb.Error{ Log: &modelpb.ErrorLog{Message: "log_message"}, Exception: &modelpb.Exception{Message: "exception_message"}, }, - message: "log_message", + expected: "log_message", }} - for _, test := range tests { batch := modelpb.Batch{{Error: test.input}} processor := modelprocessor.SetErrorMessage{} err := processor.ProcessBatch(context.Background(), &batch) assert.NoError(t, err) - assert.Equal(t, test.message, batch[0].Error.Message) + assert.Equal(t, test.expected, batch[0].Message) + assert.Equal(t, test.expected, batch[0].Error.Message) + } +} + +// If event message is non-empty, do not override with error message. +func TestSetEventMessage(t *testing.T) { + tests := []struct { + input *modelpb.APMEvent + expected string + }{{ + input: &modelpb.APMEvent{ + Message: "message", + }, + expected: "message", + }, { + input: &modelpb.APMEvent{ + Error: &modelpb.Error{Log: &modelpb.ErrorLog{Message: "log_message"}}, + Message: "message", + }, + expected: "message", + }, { + input: &modelpb.APMEvent{ + Error: &modelpb.Error{Exception: &modelpb.Exception{Message: "exception_message"}}, + Message: "message", + }, + expected: "message", + }, { + input: &modelpb.APMEvent{ + Error: &modelpb.Error{ + Log: &modelpb.ErrorLog{}, + Exception: &modelpb.Exception{Message: "exception_message"}, + }, + Message: "message", + }, + expected: "message", + }, { + input: &modelpb.APMEvent{ + Error: &modelpb.Error{ + Log: &modelpb.ErrorLog{Message: "log_message"}, + Exception: &modelpb.Exception{Message: "exception_message"}, + }, + Message: "message", + }, + expected: "message", + }} + + for _, test := range tests { + batch := modelpb.Batch{test.input} + processor := modelprocessor.SetErrorMessage{} + err := processor.ProcessBatch(context.Background(), &batch) + assert.NoError(t, err) + assert.Equal(t, test.expected, batch[0].Message) } }