From 011087fbd104dde9388a16c9b5bbb5da4eb11c96 Mon Sep 17 00:00:00 2001 From: Devin Collins Date: Tue, 21 Nov 2023 20:07:06 -0800 Subject: [PATCH] Add recover --- processor.go | 7 +++++++ processor_test.go | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/processor.go b/processor.go index cb25654..c9faad4 100644 --- a/processor.go +++ b/processor.go @@ -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++ { diff --git a/processor_test.go b/processor_test.go index ee70a1b..ff68dba 100644 --- a/processor_test.go +++ b/processor_test.go @@ -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") @@ -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)