Skip to content

Commit

Permalink
Merge pull request #2 from JupiterOne/feat-nil-check
Browse files Browse the repository at this point in the history
Add nil catch to prevent crash
  • Loading branch information
ImDevinC authored Nov 22, 2023
2 parents a53466b + 011087f commit fe9ecd2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 8 additions & 1 deletion processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,19 @@ 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++ {
for k := 0; k < ld.ResourceLogs().At(i).ScopeLogs().At(j).LogRecords().Len(); k++ {
for l, m := range ld.ResourceLogs().At(i).ScopeLogs().At(j).LogRecords().At(k).Attributes().AsRaw() {
if reflect.TypeOf(m).Kind() == reflect.Map || reflect.TypeOf(m).Kind() == reflect.Slice {
if m != nil && (reflect.TypeOf(m).Kind() == reflect.Map || reflect.TypeOf(m).Kind() == reflect.Slice) {
payload, err := json.Marshal(m)
if err != nil {
jlp.logger.Error(err.Error())
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 fe9ecd2

Please sign in to comment.