Skip to content

Commit

Permalink
fix error message propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
rubvs committed Aug 25, 2024
1 parent 09e0748 commit 4c9bd81
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 59 deletions.
3 changes: 1 addition & 2 deletions model/modelprocessor/errormessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ func (s SetErrorMessage) ProcessBatch(ctx context.Context, b *modelpb.Batch) err
for i := range *b {
event := (*b)[i]
if event.Error != nil {
event.Error.Message = s.setErrorMessage(event)
if event.GetMessage() == "" {
event.Message = event.Error.Message
event.Message = s.setErrorMessage(event)
}
}
}
Expand Down
94 changes: 37 additions & 57 deletions model/modelprocessor/errormessage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,90 +27,70 @@ 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
expected string
}{{
input: &modelpb.Error{},
expected: "",
}, {
input: &modelpb.Error{Log: &modelpb.ErrorLog{Message: "log_message"}},
expected: "log_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"},
},
expected: "exception_message",
}, {
input: &modelpb.Error{
Log: &modelpb.ErrorLog{Message: "log_message"},
Exception: &modelpb.Exception{Message: "exception_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.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
desc string
input *modelpb.APMEvent
expectedMessage string
expectedLogMessage string
expectedExceptionMessage string
}{{
desc: "keep event message when no error",
input: &modelpb.APMEvent{
Error: &modelpb.Error{
Log: &modelpb.ErrorLog{},
Exception: &modelpb.Exception{},
},
Message: "message",
},
expected: "message",
}, {
input: &modelpb.APMEvent{
Error: &modelpb.Error{Log: &modelpb.ErrorLog{Message: "log_message"}},
Message: "message",
},
expected: "message",
expectedMessage: "message",
expectedLogMessage: "",
expectedExceptionMessage: "",
}, {
desc: "keep event message when error",
input: &modelpb.APMEvent{
Error: &modelpb.Error{Exception: &modelpb.Exception{Message: "exception_message"}},
Error: &modelpb.Error{
Log: &modelpb.ErrorLog{Message: "log_message"},
Exception: &modelpb.Exception{Message: "exception_message"},
},
Message: "message",
},
expected: "message",
expectedMessage: "message",
expectedLogMessage: "log_message",
expectedExceptionMessage: "exception_message",
}, {
desc: "propagate log error if event message empty",
input: &modelpb.APMEvent{
Error: &modelpb.Error{
Log: &modelpb.ErrorLog{},
Exception: &modelpb.Exception{Message: "exception_message"},
Log: &modelpb.ErrorLog{Message: "log_message"},
Exception: &modelpb.Exception{},
},
Message: "message",
Message: "",
},
expected: "message",
expectedMessage: "log_message",
expectedLogMessage: "log_message",
expectedExceptionMessage: "",
}, {
desc: "propagate exception error if event message is empty",
input: &modelpb.APMEvent{
Error: &modelpb.Error{
Log: &modelpb.ErrorLog{Message: "log_message"},
Log: &modelpb.ErrorLog{},
Exception: &modelpb.Exception{Message: "exception_message"},
},
Message: "message",
Message: "",
},
expected: "message",
expectedMessage: "exception_message",
expectedLogMessage: "",
expectedExceptionMessage: "exception_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)
assert.Equal(t, test.expectedMessage, batch[0].Message)
assert.Equal(t, test.expectedLogMessage, batch[0].Error.Log.Message)
assert.Equal(t, test.expectedExceptionMessage, batch[0].Error.Exception.Message)
}
}

0 comments on commit 4c9bd81

Please sign in to comment.