Skip to content

Commit

Permalink
Add recover
Browse files Browse the repository at this point in the history
  • Loading branch information
ImDevinC committed Nov 22, 2023
1 parent e9289f4 commit 011087f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
7 changes: 7 additions & 0 deletions processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ func (jlp *jsonlogflattenerProcessor) Shutdown(context.Context) error {
}

func (jlp *jsonlogflattenerProcessor) flatten(ld *plog.Logs) error {
defer func() {
// if we paniced for some reason, recover from the panic and log
// the error message so we don't break the collector workflow
if r := recover(); r != nil {
jlp.logger.Error(r.(error).Error())
}
}()
var rootErr error
for i := 0; i < ld.ResourceLogs().Len(); i++ {
for j := 0; j < ld.ResourceLogs().At(i).ScopeLogs().Len(); j++ {
Expand Down
4 changes: 3 additions & 1 deletion processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestFlatten(t *testing.T) {
logs := tln.AllLogs()
require.Len(t, logs, 1)
attrs := logs[0].ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0).Attributes()
require.Equal(t, attrs.Len(), 6)
require.Equal(t, attrs.Len(), 7)
fooString, exists := attrs.Get("foo-string")
require.True(t, exists)
require.Equal(t, fooString.Str(), "bar")
Expand Down Expand Up @@ -94,6 +94,8 @@ func buildLogs(count int) plog.Logs {
log.Attributes().PutDouble("foo-double", 1.0)
log.Attributes().PutInt("foo-int", 1)
log.Attributes().PutBool("foo-bool", true)
// Add an empty key to make sure empty attributes don't cause a panic
log.Attributes().PutEmpty("test")
inputFooMap := log.Attributes().PutEmptyMap("foo-map")
inputFooMap.PutStr("foo-map-string", "bar")
inputFooMap.PutDouble("foo-map-double", 1.0)
Expand Down

0 comments on commit 011087f

Please sign in to comment.