Skip to content

Commit 0d9bc58

Browse files
authored
Assert that resources is map like before treating as a map (#333)
* Assert that resources is map like before treating as a map * actually make the test case a thing
1 parent e0f58e7 commit 0d9bc58

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

processor/loghouseprocessor/processor.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,14 @@ func promoteResourceAttrs(l *plog.LogRecord, rlogs *plog.ResourceLogs) {
152152
if !ok {
153153
return
154154
}
155-
merged := MergeRawMaps(rlogs.Resource().Attributes().AsRaw(), attributes.Map().AsRaw())
156-
_ = rlogs.Resource().Attributes().FromRaw(merged)
155+
156+
if attributes.Type() != pcommon.ValueTypeMap {
157+
return
158+
}
159+
160+
origAttr := rlogs.Resource().Attributes()
161+
merged := MergeRawMaps(origAttr.AsRaw(), attributes.Map().AsRaw())
162+
_ = origAttr.FromRaw(merged)
157163
}
158164

159165
// MergeRawMaps merges n maps with a later map's keys overriding earlier maps. (copied to avoid dep hell)

processor/loghouseprocessor/processor_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,4 +347,28 @@ func Test_promoteResourceAttrs(t *testing.T) {
347347

348348
})
349349

350+
t.Run("not a map", func(t *testing.T) {
351+
rl := plog.NewResourceLogs()
352+
_ = rl.Resource().Attributes().FromRaw(map[string]any{"r1": "original"})
353+
l1 := plog.NewLogRecord()
354+
_ = l1.Attributes().FromRaw(map[string]any{"resource": "r2"})
355+
356+
promoteResourceAttrs(&l1, &rl)
357+
358+
_, ok := rl.Resource().Attributes().Get("r2")
359+
assert.False(t, ok)
360+
})
361+
362+
t.Run("empty resources", func(t *testing.T) {
363+
rl := plog.NewResourceLogs()
364+
l1 := plog.NewLogRecord()
365+
_ = l1.Attributes().FromRaw(map[string]any{"resource": map[string]any{"r1": "v1"}})
366+
367+
promoteResourceAttrs(&l1, &rl)
368+
369+
v1, ok := rl.Resource().Attributes().Get("r1")
370+
assert.True(t, ok)
371+
assert.Equal(t, "v1", v1.Str())
372+
})
373+
350374
}

0 commit comments

Comments
 (0)