Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pkg/ottl] adapt mapGetter to handle nested map items within slices #37408

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .chloggen/ottl-nested-map-literals.yaml
Original file line number Diff line number Diff line change
@@ -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: bug_fix

# 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 limitation of map literals within slices not being handled correctly

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [37405]

# (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: []
16 changes: 16 additions & 0 deletions pkg/ottl/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,22 @@ func Test_e2e_converters(t *testing.T) {
m.PutInt("bar", 5)
},
},
{
statement: `set(attributes["test"], {"list":[{"foo":"bar"}]})`,
want: func(tCtx ottllog.TransformContext) {
m := tCtx.GetLogRecord().Attributes().PutEmptyMap("test")
m2 := m.PutEmptySlice("list").AppendEmpty().SetEmptyMap()
m2.PutStr("foo", "bar")
},
},
{
statement: `set(attributes, {"list":[{"foo":"bar"}]})`,
want: func(tCtx ottllog.TransformContext) {
tCtx.GetLogRecord().Attributes().Clear()
m2 := tCtx.GetLogRecord().Attributes().PutEmptySlice("list").AppendEmpty().SetEmptyMap()
m2.PutStr("foo", "bar")
},
},
}

for _, tt := range tests {
Expand Down
31 changes: 23 additions & 8 deletions pkg/ottl/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func (l *listGetter[K]) Get(ctx context.Context, tCtx K) (any, error) {
if err != nil {
return nil, err
}

evaluated[i] = val
}

Expand All @@ -180,23 +181,37 @@ type mapGetter[K any] struct {
}

func (m *mapGetter[K]) Get(ctx context.Context, tCtx K) (any, error) {
evaluated := map[string]any{}
result := pcommon.NewMap()
for k, v := range m.mapValues {
val, err := v.Get(ctx, tCtx)
if err != nil {
return nil, err
}
switch t := val.(type) {
switch typedVal := val.(type) {
case pcommon.Map:
evaluated[k] = t.AsRaw()
target := result.PutEmpty(k).SetEmptyMap()
typedVal.CopyTo(target)
case []any:
target := result.PutEmpty(k).SetEmptySlice()
for _, el := range typedVal {
switch typedEl := el.(type) {
case pcommon.Map:
m := target.AppendEmpty().SetEmptyMap()
typedEl.CopyTo(m)
default:
err := target.AppendEmpty().FromRaw(el)
if err != nil {
return nil, err
}
}
}
default:
evaluated[k] = t
err := result.PutEmpty(k).FromRaw(val)
if err != nil {
return nil, err
}
}
}
result := pcommon.NewMap()
if err := result.FromRaw(evaluated); err != nil {
return nil, err
}
return result, nil
}

Expand Down
12 changes: 11 additions & 1 deletion pkg/ottl/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,16 @@ func Test_newGetter(t *testing.T) {
Int: ottltest.Intp(1),
},
},
{
Map: &mapValue{
Values: []mapItem{
{
Key: ottltest.Strp("stringAttr"),
Value: &value{String: ottltest.Strp("value")},
},
},
},
},
},
},
},
Expand All @@ -660,7 +670,7 @@ func Test_newGetter(t *testing.T) {
"foo": map[string]any{
"test": "value",
},
"listAttr": []any{"test0", int64(1)},
"listAttr": []any{"test0", int64(1), map[string]any{"stringAttr": "value"}},
},
"stringAttr": "value",
"intAttr": int64(3),
Expand Down
16 changes: 15 additions & 1 deletion pkg/ottl/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/alecthomas/participle/v2"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -480,6 +481,19 @@ func (p *Parser[K]) ParseValueExpression(raw string) (*ValueExpression[K], error
}

return &ValueExpression[K]{
getter: getter,
getter: &StandardGetSetter[K]{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we end up stick to this approach, considering the mapGetter is now returning pcommon.Map values, I think we could keep it consistent and remove these changes, so all maps would still be parsed aspcommon.Map instead of raw values. I guess it would be a breaking change, though. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be fine with that, however there are also arguments for returning the raw types here (#37280 (comment)). I think I would also prefer to consistently return pcommon.Map values though. @evan-bradley @TylerHelmuth WDYT?

Getter: func(ctx context.Context, tCtx K) (any, error) {
val, err := getter.Get(ctx, tCtx)
if err != nil {
return nil, err
}
switch v := val.(type) {
case pcommon.Map:
return v.AsRaw(), nil
default:
return v, nil
}
},
},
}, nil
}
9 changes: 3 additions & 6 deletions pkg/ottl/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/pdata/pcommon"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottltest"
)
Expand Down Expand Up @@ -2209,11 +2208,9 @@ func Test_parseValueExpression_full(t *testing.T) {
name: "map",
valueExpression: `{"map": 1}`,
expected: func() any {
m := pcommon.NewMap()
_ = m.FromRaw(map[string]any{
"map": 1,
})
return m
return map[string]any{
"map": int64(1),
}
},
},
{
Expand Down