Skip to content

Commit 04d7a69

Browse files
[pkg/ottl] fix handling of nested maps within slices in flatten function (#36204)
Co-authored-by: Evan Bradley <[email protected]>
1 parent 5a3c58f commit 04d7a69

File tree

4 files changed

+89
-25
lines changed

4 files changed

+89
-25
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: pkg/ottl
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: fix handling of nested maps within slices in the `flatten` function
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [36162]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

pkg/ottl/e2e/e2e_test.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,10 @@ func Test_e2e_editors(t *testing.T) {
7171
tCtx.GetLogRecord().Attributes().PutStr("foo.nested.test", "pass")
7272

7373
tCtx.GetLogRecord().Attributes().Remove("things")
74-
m1 := tCtx.GetLogRecord().Attributes().PutEmptyMap("things.0")
75-
m1.PutStr("name", "foo")
76-
m1.PutInt("value", 2)
77-
78-
m2 := tCtx.GetLogRecord().Attributes().PutEmptyMap("things.1")
79-
m2.PutStr("name", "bar")
80-
m2.PutInt("value", 5)
74+
tCtx.GetLogRecord().Attributes().PutStr("things.0.name", "foo")
75+
tCtx.GetLogRecord().Attributes().PutInt("things.0.value", 2)
76+
tCtx.GetLogRecord().Attributes().PutStr("things.1.name", "bar")
77+
tCtx.GetLogRecord().Attributes().PutInt("things.1.value", 5)
8178
},
8279
},
8380
{
@@ -89,20 +86,18 @@ func Test_e2e_editors(t *testing.T) {
8986
m.PutStr("test.http.url", "http://localhost/health")
9087
m.PutStr("test.flags", "A|B|C")
9188
m.PutStr("test.total.string", "123456789")
89+
9290
m.PutStr("test.foo.bar", "pass")
9391
m.PutStr("test.foo.flags", "pass")
9492
m.PutStr("test.foo.bar", "pass")
9593
m.PutStr("test.foo.flags", "pass")
9694
m.PutStr("test.foo.slice.0", "val")
9795
m.PutStr("test.foo.nested.test", "pass")
9896

99-
m1 := m.PutEmptyMap("test.things.0")
100-
m1.PutStr("name", "foo")
101-
m1.PutInt("value", 2)
102-
103-
m2 := m.PutEmptyMap("test.things.1")
104-
m2.PutStr("name", "bar")
105-
m2.PutInt("value", 5)
97+
m.PutStr("test.things.0.name", "foo")
98+
m.PutInt("test.things.0.value", 2)
99+
m.PutStr("test.things.1.name", "bar")
100+
m.PutInt("test.things.1.value", 5)
106101
m.CopyTo(tCtx.GetLogRecord().Attributes())
107102
},
108103
},

pkg/ottl/ottlfuncs/func_flatten.go

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,28 +54,45 @@ func flatten[K any](target ottl.PMapGetter[K], p ottl.Optional[string], d ottl.O
5454
}
5555

5656
result := pcommon.NewMap()
57-
flattenHelper(m, result, prefix, 0, depth)
57+
flattenMap(m, result, prefix, 0, depth)
5858
result.MoveTo(m)
5959

6060
return nil, nil
6161
}, nil
6262
}
6363

64-
func flattenHelper(m pcommon.Map, result pcommon.Map, prefix string, currentDepth, maxDepth int64) {
64+
func flattenMap(m pcommon.Map, result pcommon.Map, prefix string, currentDepth, maxDepth int64) {
6565
if len(prefix) > 0 {
6666
prefix += "."
6767
}
6868
m.Range(func(k string, v pcommon.Value) bool {
69-
switch {
70-
case v.Type() == pcommon.ValueTypeMap && currentDepth < maxDepth:
71-
flattenHelper(v.Map(), result, prefix+k, currentDepth+1, maxDepth)
72-
case v.Type() == pcommon.ValueTypeSlice && currentDepth < maxDepth:
73-
for i := 0; i < v.Slice().Len(); i++ {
69+
return flattenValue(k, v, currentDepth, maxDepth, result, prefix)
70+
})
71+
}
72+
73+
func flattenSlice(s pcommon.Slice, result pcommon.Map, prefix string, currentDepth int64, maxDepth int64) {
74+
for i := 0; i < s.Len(); i++ {
75+
flattenValue(fmt.Sprintf("%d", i), s.At(i), currentDepth+1, maxDepth, result, prefix)
76+
}
77+
}
78+
79+
func flattenValue(k string, v pcommon.Value, currentDepth int64, maxDepth int64, result pcommon.Map, prefix string) bool {
80+
switch {
81+
case v.Type() == pcommon.ValueTypeMap && currentDepth < maxDepth:
82+
flattenMap(v.Map(), result, prefix+k, currentDepth+1, maxDepth)
83+
case v.Type() == pcommon.ValueTypeSlice && currentDepth < maxDepth:
84+
for i := 0; i < v.Slice().Len(); i++ {
85+
switch {
86+
case v.Slice().At(i).Type() == pcommon.ValueTypeMap && currentDepth+1 < maxDepth:
87+
flattenMap(v.Slice().At(i).Map(), result, fmt.Sprintf("%v.%v", prefix+k, i), currentDepth+2, maxDepth)
88+
case v.Slice().At(i).Type() == pcommon.ValueTypeSlice && currentDepth+1 < maxDepth:
89+
flattenSlice(v.Slice().At(i).Slice(), result, fmt.Sprintf("%v.%v", prefix+k, i), currentDepth+2, maxDepth)
90+
default:
7491
v.Slice().At(i).CopyTo(result.PutEmpty(fmt.Sprintf("%v.%v", prefix+k, i)))
7592
}
76-
default:
77-
v.CopyTo(result.PutEmpty(prefix + k))
7893
}
79-
return true
80-
})
94+
default:
95+
v.CopyTo(result.PutEmpty(prefix + k))
96+
}
97+
return true
8198
}

pkg/ottl/ottlfuncs/func_flatten_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,31 @@ func Test_flatten(t *testing.T) {
8585
"occupants.1": "user 2",
8686
},
8787
},
88+
{
89+
name: "combination with mixed nested slices",
90+
target: map[string]any{
91+
"name": "test",
92+
"address": map[string]any{
93+
"street": "first",
94+
"house": int64(1234),
95+
},
96+
"occupants": []any{
97+
"user 1",
98+
map[string]any{
99+
"name": "user 2",
100+
},
101+
},
102+
},
103+
prefix: ottl.Optional[string]{},
104+
depth: ottl.Optional[int64]{},
105+
expected: map[string]any{
106+
"name": "test",
107+
"address.street": "first",
108+
"address.house": int64(1234),
109+
"occupants.0": "user 1",
110+
"occupants.1.name": "user 2",
111+
},
112+
},
88113
{
89114
name: "deep nesting",
90115
target: map[string]any{

0 commit comments

Comments
 (0)