Skip to content

Commit

Permalink
set event message to error message if empty
Browse files Browse the repository at this point in the history
  • Loading branch information
rubvs committed Aug 19, 2024
1 parent f8865c4 commit c8a9e88
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 12 deletions.
3 changes: 3 additions & 0 deletions model/modelprocessor/errormessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
76 changes: 64 additions & 12 deletions model/modelprocessor/errormessage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit c8a9e88

Please sign in to comment.