From 13bc19062c50d3e23ae3f8342a8f9c5ba6a76490 Mon Sep 17 00:00:00 2001 From: Florian Bacher Date: Tue, 5 Nov 2024 08:47:27 +0100 Subject: [PATCH 1/6] [pkg/ottl] Fix handling of slice values in flatten function by considering depth option Signed-off-by: Florian Bacher --- ...-flatten-fix-top-level-slice-handling.yaml | 27 +++++++++++++++ pkg/ottl/e2e/e2e_test.go | 13 ++------ pkg/ottl/ottlfuncs/func_flatten.go | 2 +- pkg/ottl/ottlfuncs/func_flatten_test.go | 33 +++++++++++++++++++ 4 files changed, 63 insertions(+), 12 deletions(-) create mode 100644 .chloggen/ottl-flatten-fix-top-level-slice-handling.yaml diff --git a/.chloggen/ottl-flatten-fix-top-level-slice-handling.yaml b/.chloggen/ottl-flatten-fix-top-level-slice-handling.yaml new file mode 100644 index 0000000000000..8bed408e63a55 --- /dev/null +++ b/.chloggen/ottl-flatten-fix-top-level-slice-handling.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: pkg/ottl + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Fix handling of slice values in `flatten` function by considering `depth` option + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [36161] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] diff --git a/pkg/ottl/e2e/e2e_test.go b/pkg/ottl/e2e/e2e_test.go index 791550a5a062e..e6f9fef3f31eb 100644 --- a/pkg/ottl/e2e/e2e_test.go +++ b/pkg/ottl/e2e/e2e_test.go @@ -107,16 +107,7 @@ func Test_e2e_editors(t *testing.T) { }, { statement: `flatten(attributes, depth=0)`, - want: func(tCtx ottllog.TransformContext) { - tCtx.GetLogRecord().Attributes().Remove("things") - m1 := tCtx.GetLogRecord().Attributes().PutEmptyMap("things.0") - m1.PutStr("name", "foo") - m1.PutInt("value", 2) - - m2 := tCtx.GetLogRecord().Attributes().PutEmptyMap("things.1") - m2.PutStr("name", "bar") - m2.PutInt("value", 5) - }, + want: func(tCtx ottllog.TransformContext) {}, }, { statement: `flatten(attributes, depth=1)`, @@ -131,7 +122,7 @@ func Test_e2e_editors(t *testing.T) { m.PutStr("foo.flags", "pass") m.PutStr("foo.bar", "pass") m.PutStr("foo.flags", "pass") - m.PutStr("foo.slice.0", "val") + m.PutEmptySlice("foo.slice").AppendEmpty().SetStr("val") m1 := m.PutEmptyMap("things.0") m1.PutStr("name", "foo") diff --git a/pkg/ottl/ottlfuncs/func_flatten.go b/pkg/ottl/ottlfuncs/func_flatten.go index ebe30024612c1..48a9a13abe62d 100644 --- a/pkg/ottl/ottlfuncs/func_flatten.go +++ b/pkg/ottl/ottlfuncs/func_flatten.go @@ -69,7 +69,7 @@ func flattenHelper(m pcommon.Map, result pcommon.Map, prefix string, currentDept switch { case v.Type() == pcommon.ValueTypeMap && currentDepth < maxDepth: flattenHelper(v.Map(), result, prefix+k, currentDepth+1, maxDepth) - case v.Type() == pcommon.ValueTypeSlice: + case v.Type() == pcommon.ValueTypeSlice && currentDepth < maxDepth: for i := 0; i < v.Slice().Len(); i++ { v.Slice().At(i).CopyTo(result.PutEmpty(fmt.Sprintf("%v.%v", prefix+k, i))) } diff --git a/pkg/ottl/ottlfuncs/func_flatten_test.go b/pkg/ottl/ottlfuncs/func_flatten_test.go index 09dfc9648a160..addf5c3cd664e 100644 --- a/pkg/ottl/ottlfuncs/func_flatten_test.go +++ b/pkg/ottl/ottlfuncs/func_flatten_test.go @@ -144,6 +144,39 @@ func Test_flatten(t *testing.T) { }, }, }, + { + name: "zero depth", + target: map[string]any{ + "0": map[string]any{ + "1": map[string]any{ + "2": map[string]any{ + "3": "value", + }, + }, + }, + "1": []any{ + map[string]any{ + "1": "value", + }, + }, + }, + prefix: ottl.Optional[string]{}, + depth: ottl.NewTestingOptional[int64](0), + expected: map[string]any{ + "0": map[string]any{ + "1": map[string]any{ + "2": map[string]any{ + "3": "value", + }, + }, + }, + "1": []any{ + map[string]any{ + "1": "value", + }, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From bcb5cdfcaed21e106f41cc219a9979a7e936716d Mon Sep 17 00:00:00 2001 From: Florian Bacher Date: Tue, 5 Nov 2024 08:54:28 +0100 Subject: [PATCH 2/6] add change type to change log Signed-off-by: Florian Bacher --- .chloggen/ottl-flatten-fix-top-level-slice-handling.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.chloggen/ottl-flatten-fix-top-level-slice-handling.yaml b/.chloggen/ottl-flatten-fix-top-level-slice-handling.yaml index 8bed408e63a55..7875b94791df4 100644 --- a/.chloggen/ottl-flatten-fix-top-level-slice-handling.yaml +++ b/.chloggen/ottl-flatten-fix-top-level-slice-handling.yaml @@ -1,7 +1,7 @@ # Use this changelog template to create an entry for release notes. # One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' -change_type: +change_type: bug_fix # The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) component: pkg/ottl From 6254179eb984789575fbee0661e80db731779e06 Mon Sep 17 00:00:00 2001 From: Florian Bacher Date: Tue, 5 Nov 2024 09:01:09 +0100 Subject: [PATCH 3/6] fix linting Signed-off-by: Florian Bacher --- pkg/ottl/e2e/e2e_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/ottl/e2e/e2e_test.go b/pkg/ottl/e2e/e2e_test.go index e6f9fef3f31eb..86f618c1d74c3 100644 --- a/pkg/ottl/e2e/e2e_test.go +++ b/pkg/ottl/e2e/e2e_test.go @@ -107,7 +107,7 @@ func Test_e2e_editors(t *testing.T) { }, { statement: `flatten(attributes, depth=0)`, - want: func(tCtx ottllog.TransformContext) {}, + want: func(_ ottllog.TransformContext) {}, }, { statement: `flatten(attributes, depth=1)`, From 280fa5d0a12fd40bad05ca113467b6bee1ea48d6 Mon Sep 17 00:00:00 2001 From: Florian Bacher Date: Mon, 11 Nov 2024 07:24:33 +0100 Subject: [PATCH 4/6] verify that depth of flatten function is greater than 0 Signed-off-by: Florian Bacher --- pkg/ottl/e2e/e2e_test.go | 4 ---- pkg/ottl/ottlfuncs/func_flatten.go | 4 ++-- pkg/ottl/ottlfuncs/func_flatten_test.go | 22 +++++++++++----------- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/pkg/ottl/e2e/e2e_test.go b/pkg/ottl/e2e/e2e_test.go index 86f618c1d74c3..f131935b23bb0 100644 --- a/pkg/ottl/e2e/e2e_test.go +++ b/pkg/ottl/e2e/e2e_test.go @@ -105,10 +105,6 @@ func Test_e2e_editors(t *testing.T) { m.CopyTo(tCtx.GetLogRecord().Attributes()) }, }, - { - statement: `flatten(attributes, depth=0)`, - want: func(_ ottllog.TransformContext) {}, - }, { statement: `flatten(attributes, depth=1)`, want: func(tCtx ottllog.TransformContext) { diff --git a/pkg/ottl/ottlfuncs/func_flatten.go b/pkg/ottl/ottlfuncs/func_flatten.go index 48a9a13abe62d..709eb1bef7465 100644 --- a/pkg/ottl/ottlfuncs/func_flatten.go +++ b/pkg/ottl/ottlfuncs/func_flatten.go @@ -37,8 +37,8 @@ func flatten[K any](target ottl.PMapGetter[K], p ottl.Optional[string], d ottl.O depth := int64(math.MaxInt64) if !d.IsEmpty() { depth = d.Get() - if depth < 0 { - return nil, fmt.Errorf("invalid depth for flatten function, %d cannot be negative", depth) + if depth < 1 { + return nil, fmt.Errorf("invalid depth '%d' for flatten function, must be greater than 0", depth) } } diff --git a/pkg/ottl/ottlfuncs/func_flatten_test.go b/pkg/ottl/ottlfuncs/func_flatten_test.go index addf5c3cd664e..5d3163cad16f0 100644 --- a/pkg/ottl/ottlfuncs/func_flatten_test.go +++ b/pkg/ottl/ottlfuncs/func_flatten_test.go @@ -145,7 +145,7 @@ func Test_flatten(t *testing.T) { }, }, { - name: "zero depth", + name: "max depth with slice", target: map[string]any{ "0": map[string]any{ "1": map[string]any{ @@ -154,23 +154,23 @@ func Test_flatten(t *testing.T) { }, }, }, - "1": []any{ - map[string]any{ - "1": "value", + "1": map[string]any{ + "1": []any{ + map[string]any{ + "1": "value", + }, }, }, }, prefix: ottl.Optional[string]{}, - depth: ottl.NewTestingOptional[int64](0), + depth: ottl.NewTestingOptional[int64](1), expected: map[string]any{ - "0": map[string]any{ - "1": map[string]any{ - "2": map[string]any{ - "3": "value", - }, + "0.1": map[string]any{ + "2": map[string]any{ + "3": "value", }, }, - "1": []any{ + "1.1": []any{ map[string]any{ "1": "value", }, From 7a4d0d0458902f2387c811cd1cbde353bbd32ab7 Mon Sep 17 00:00:00 2001 From: Florian Bacher Date: Tue, 12 Nov 2024 07:22:21 +0100 Subject: [PATCH 5/6] Apply suggestions from code review Co-authored-by: Evan Bradley <11745660+evan-bradley@users.noreply.github.com> --- .chloggen/ottl-flatten-fix-top-level-slice-handling.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.chloggen/ottl-flatten-fix-top-level-slice-handling.yaml b/.chloggen/ottl-flatten-fix-top-level-slice-handling.yaml index 7875b94791df4..a625a284e3798 100644 --- a/.chloggen/ottl-flatten-fix-top-level-slice-handling.yaml +++ b/.chloggen/ottl-flatten-fix-top-level-slice-handling.yaml @@ -7,7 +7,7 @@ change_type: bug_fix component: pkg/ottl # A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). -note: Fix handling of slice values in `flatten` function by considering `depth` option +note: Respect the `depth` option when flattening slices using `flatten` # Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. issues: [36161] @@ -15,7 +15,7 @@ issues: [36161] # (Optional) One or more lines of additional information to render under the primary note. # These lines will be padded with 2 spaces and then inserted directly into the document. # Use pipe (|) for multiline entries. -subtext: +subtext: The `depth` option is also now required to be at least `1`. # If your change doesn't affect end users or the exported elements of any package, # you should instead start your pull request title with [chore] or use the "Skip Changelog" label. From 891ccb89513032bcb8c1ecda4d69d78f29b78ef3 Mon Sep 17 00:00:00 2001 From: Florian Bacher Date: Wed, 13 Nov 2024 09:44:46 +0100 Subject: [PATCH 6/6] add test case for depth=0 Signed-off-by: Florian Bacher --- pkg/ottl/ottlfuncs/func_flatten_test.go | 28 ++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/pkg/ottl/ottlfuncs/func_flatten_test.go b/pkg/ottl/ottlfuncs/func_flatten_test.go index 5d3163cad16f0..63c18b3570e07 100644 --- a/pkg/ottl/ottlfuncs/func_flatten_test.go +++ b/pkg/ottl/ottlfuncs/func_flatten_test.go @@ -212,11 +212,29 @@ func Test_flatten_bad_target(t *testing.T) { } func Test_flatten_bad_depth(t *testing.T) { - target := &ottl.StandardPMapGetter[any]{ - Getter: func(_ context.Context, _ any) (any, error) { - return pcommon.NewMap(), nil + tests := []struct { + name string + depth ottl.Optional[int64] + }{ + { + name: "negative depth", + depth: ottl.NewTestingOptional[int64](-1), + }, + { + name: "zero depth", + depth: ottl.NewTestingOptional[int64](0), }, } - _, err := flatten[any](target, ottl.Optional[string]{}, ottl.NewTestingOptional[int64](-1)) - assert.Error(t, err) + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + target := &ottl.StandardPMapGetter[any]{ + Getter: func(_ context.Context, _ any) (any, error) { + return pcommon.NewMap(), nil + }, + } + _, err := flatten[any](target, ottl.Optional[string]{}, tt.depth) + assert.Error(t, err) + }) + } }