diff --git a/.chloggen/ottl-validate-key.yaml b/.chloggen/ottl-validate-key.yaml new file mode 100755 index 000000000000..6feb84eb5600 --- /dev/null +++ b/.chloggen/ottl-validate-key.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: breaking + +# 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: Throw an error if keys are used on a path that does not allow them. + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [30162] + +# (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/contexts/internal/map.go b/pkg/ottl/contexts/internal/map.go index a3dea84a0e0c..34e891c032c1 100644 --- a/pkg/ottl/contexts/internal/map.go +++ b/pkg/ottl/contexts/internal/map.go @@ -12,12 +12,12 @@ import ( "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" ) -func GetMapValue[K any](ctx context.Context, tCtx K, m pcommon.Map, key ottl.Key[K]) (any, error) { - if key == nil { - return nil, fmt.Errorf("cannot get map value without key") +func GetMapValue[K any](ctx context.Context, tCtx K, m pcommon.Map, keys []ottl.Key[K]) (any, error) { + if len(keys) == 0 { + return nil, fmt.Errorf("cannot get map value without keys") } - s, err := key.String(ctx, tCtx) + s, err := keys[0].String(ctx, tCtx) if err != nil { return nil, err } @@ -30,15 +30,15 @@ func GetMapValue[K any](ctx context.Context, tCtx K, m pcommon.Map, key ottl.Key return nil, nil } - return getIndexableValue[K](ctx, tCtx, val, key.Next()) + return getIndexableValue[K](ctx, tCtx, val, keys[1:]) } -func SetMapValue[K any](ctx context.Context, tCtx K, m pcommon.Map, key ottl.Key[K], val any) error { - if key == nil { +func SetMapValue[K any](ctx context.Context, tCtx K, m pcommon.Map, keys []ottl.Key[K], val any) error { + if len(keys) == 0 { return fmt.Errorf("cannot set map value without key") } - s, err := key.String(ctx, tCtx) + s, err := keys[0].String(ctx, tCtx) if err != nil { return err } @@ -50,5 +50,5 @@ func SetMapValue[K any](ctx context.Context, tCtx K, m pcommon.Map, key ottl.Key if !ok { currentValue = m.PutEmpty(*s) } - return setIndexableValue[K](ctx, tCtx, currentValue, val, key.Next()) + return setIndexableValue[K](ctx, tCtx, currentValue, val, keys[1:]) } diff --git a/pkg/ottl/contexts/internal/map_test.go b/pkg/ottl/contexts/internal/map_test.go index ca51dfe73a7b..e3095421abe7 100644 --- a/pkg/ottl/contexts/internal/map_test.go +++ b/pkg/ottl/contexts/internal/map_test.go @@ -11,27 +11,32 @@ import ( "github.com/stretchr/testify/assert" "go.opentelemetry.io/collector/pdata/pcommon" + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottltest" ) func Test_GetMapValue_Invalid(t *testing.T) { tests := []struct { name string - keys *TestKey[any] + keys []ottl.Key[any] err error }{ { name: "first key not a string", - keys: &TestKey[any]{ - I: ottltest.Intp(0), + keys: []ottl.Key[any]{ + &TestKey[any]{ + I: ottltest.Intp(0), + }, }, err: fmt.Errorf("non-string indexing is not supported"), }, { name: "index map with int", - keys: &TestKey[any]{ - S: ottltest.Strp("map"), - NextKey: &TestKey[any]{ + keys: []ottl.Key[any]{ + &TestKey[any]{ + S: ottltest.Strp("map"), + }, + &TestKey[any]{ I: ottltest.Intp(0), }, }, @@ -39,10 +44,11 @@ func Test_GetMapValue_Invalid(t *testing.T) { }, { name: "index slice with string", - keys: &TestKey[any]{ - - S: ottltest.Strp("slice"), - NextKey: &TestKey[any]{ + keys: []ottl.Key[any]{ + &TestKey[any]{ + S: ottltest.Strp("slice"), + }, + &TestKey[any]{ S: ottltest.Strp("invalid"), }, }, @@ -50,9 +56,11 @@ func Test_GetMapValue_Invalid(t *testing.T) { }, { name: "index too large", - keys: &TestKey[any]{ - S: ottltest.Strp("slice"), - NextKey: &TestKey[any]{ + keys: []ottl.Key[any]{ + &TestKey[any]{ + S: ottltest.Strp("slice"), + }, + &TestKey[any]{ I: ottltest.Intp(1), }, }, @@ -60,9 +68,11 @@ func Test_GetMapValue_Invalid(t *testing.T) { }, { name: "index too small", - keys: &TestKey[any]{ - S: ottltest.Strp("slice"), - NextKey: &TestKey[any]{ + keys: []ottl.Key[any]{ + &TestKey[any]{ + S: ottltest.Strp("slice"), + }, + &TestKey[any]{ I: ottltest.Intp(-1), }, }, @@ -70,9 +80,11 @@ func Test_GetMapValue_Invalid(t *testing.T) { }, { name: "invalid type", - keys: &TestKey[any]{ - S: ottltest.Strp("string"), - NextKey: &TestKey[any]{ + keys: []ottl.Key[any]{ + &TestKey[any]{ + S: ottltest.Strp("string"), + }, + &TestKey[any]{ S: ottltest.Strp("string"), }, }, @@ -98,13 +110,15 @@ func Test_GetMapValue_Invalid(t *testing.T) { func Test_GetMapValue_MissingKey(t *testing.T) { m := pcommon.NewMap() m.PutEmptyMap("map1").PutEmptyMap("map2") - keys := TestKey[any]{ - S: ottltest.Strp("map1"), - NextKey: &TestKey[any]{ + keys := []ottl.Key[any]{ + &TestKey[any]{ + S: ottltest.Strp("map1"), + }, + &TestKey[any]{ S: ottltest.Strp("unknown key"), }, } - result, err := GetMapValue[any](context.Background(), nil, m, &keys) + result, err := GetMapValue[any](context.Background(), nil, m, keys) assert.Nil(t, err) assert.Nil(t, result) } @@ -117,21 +131,25 @@ func Test_GetMapValue_NilKey(t *testing.T) { func Test_SetMapValue_Invalid(t *testing.T) { tests := []struct { name string - keys *TestKey[any] + keys []ottl.Key[any] err error }{ { name: "first key not a string", - keys: &TestKey[any]{ - I: ottltest.Intp(0), + keys: []ottl.Key[any]{ + &TestKey[any]{ + I: ottltest.Intp(0), + }, }, err: fmt.Errorf("non-string indexing is not supported"), }, { name: "index map with int", - keys: &TestKey[any]{ - S: ottltest.Strp("map"), - NextKey: &TestKey[any]{ + keys: []ottl.Key[any]{ + &TestKey[any]{ + S: ottltest.Strp("map"), + }, + &TestKey[any]{ I: ottltest.Intp(0), }, }, @@ -139,9 +157,11 @@ func Test_SetMapValue_Invalid(t *testing.T) { }, { name: "index slice with string", - keys: &TestKey[any]{ - S: ottltest.Strp("slice"), - NextKey: &TestKey[any]{ + keys: []ottl.Key[any]{ + &TestKey[any]{ + S: ottltest.Strp("slice"), + }, + &TestKey[any]{ S: ottltest.Strp("map"), }, }, @@ -149,9 +169,11 @@ func Test_SetMapValue_Invalid(t *testing.T) { }, { name: "slice index too large", - keys: &TestKey[any]{ - S: ottltest.Strp("slice"), - NextKey: &TestKey[any]{ + keys: []ottl.Key[any]{ + &TestKey[any]{ + S: ottltest.Strp("slice"), + }, + &TestKey[any]{ I: ottltest.Intp(1), }, }, @@ -159,9 +181,11 @@ func Test_SetMapValue_Invalid(t *testing.T) { }, { name: "slice index too small", - keys: &TestKey[any]{ - S: ottltest.Strp("slice"), - NextKey: &TestKey[any]{ + keys: []ottl.Key[any]{ + &TestKey[any]{ + S: ottltest.Strp("slice"), + }, + &TestKey[any]{ I: ottltest.Intp(-1), }, }, @@ -169,9 +193,11 @@ func Test_SetMapValue_Invalid(t *testing.T) { }, { name: "slice index too small", - keys: &TestKey[any]{ - S: ottltest.Strp("string"), - NextKey: &TestKey[any]{ + keys: []ottl.Key[any]{ + &TestKey[any]{ + S: ottltest.Strp("string"), + }, + &TestKey[any]{ S: ottltest.Strp("string"), }, }, @@ -197,16 +223,18 @@ func Test_SetMapValue_Invalid(t *testing.T) { func Test_SetMapValue_AddingNewSubMap(t *testing.T) { m := pcommon.NewMap() m.PutEmptyMap("map1").PutStr("test", "test") - keys := TestKey[any]{ - S: ottltest.Strp("map1"), - NextKey: &TestKey[any]{ + keys := []ottl.Key[any]{ + &TestKey[any]{ + S: ottltest.Strp("map1"), + }, + &TestKey[any]{ S: ottltest.Strp("map2"), - NextKey: &TestKey[any]{ - S: ottltest.Strp("foo"), - }, + }, + &TestKey[any]{ + S: ottltest.Strp("foo"), }, } - err := SetMapValue[any](context.Background(), nil, m, &keys, "bar") + err := SetMapValue[any](context.Background(), nil, m, keys, "bar") assert.Nil(t, err) expected := pcommon.NewMap() @@ -219,16 +247,18 @@ func Test_SetMapValue_AddingNewSubMap(t *testing.T) { func Test_SetMapValue_EmptyMap(t *testing.T) { m := pcommon.NewMap() - keys := TestKey[any]{ - S: ottltest.Strp("map1"), - NextKey: &TestKey[any]{ + keys := []ottl.Key[any]{ + &TestKey[any]{ + S: ottltest.Strp("map1"), + }, + &TestKey[any]{ S: ottltest.Strp("map2"), - NextKey: &TestKey[any]{ - S: ottltest.Strp("foo"), - }, + }, + &TestKey[any]{ + S: ottltest.Strp("foo"), }, } - err := SetMapValue[any](context.Background(), nil, m, &keys, "bar") + err := SetMapValue[any](context.Background(), nil, m, keys, "bar") assert.Nil(t, err) expected := pcommon.NewMap() diff --git a/pkg/ottl/contexts/internal/path.go b/pkg/ottl/contexts/internal/path.go index a3b909719073..244836de5bfe 100644 --- a/pkg/ottl/contexts/internal/path.go +++ b/pkg/ottl/contexts/internal/path.go @@ -13,7 +13,7 @@ var _ ottl.Path[any] = &TestPath[any]{} type TestPath[K any] struct { N string - Keys ottl.Key[K] + KeySlice []ottl.Key[K] NextPath *TestPath[K] } @@ -28,16 +28,15 @@ func (p *TestPath[K]) Next() ottl.Path[K] { return p.NextPath } -func (p *TestPath[K]) Key() ottl.Key[K] { - return p.Keys +func (p *TestPath[K]) Keys() []ottl.Key[K] { + return p.KeySlice } var _ ottl.Key[any] = &TestKey[any]{} type TestKey[K any] struct { - S *string - I *int64 - NextKey *TestKey[K] + S *string + I *int64 } func (k *TestKey[K]) String(_ context.Context, _ K) (*string, error) { @@ -47,10 +46,3 @@ func (k *TestKey[K]) String(_ context.Context, _ K) (*string, error) { func (k *TestKey[K]) Int(_ context.Context, _ K) (*int64, error) { return k.I, nil } - -func (k *TestKey[K]) Next() ottl.Key[K] { - if k.NextKey == nil { - return nil - } - return k.NextKey -} diff --git a/pkg/ottl/contexts/internal/resource.go b/pkg/ottl/contexts/internal/resource.go index 61638996c89e..f7d5a3135ed6 100644 --- a/pkg/ottl/contexts/internal/resource.go +++ b/pkg/ottl/contexts/internal/resource.go @@ -22,10 +22,10 @@ func ResourcePathGetSetter[K ResourceContext](path ottl.Path[K]) (ottl.GetSetter } switch path.Name() { case "attributes": - if path.Key() == nil { + if path.Keys() == nil { return accessResourceAttributes[K](), nil } - return accessResourceAttributesKey[K](path.Key()), nil + return accessResourceAttributesKey[K](path.Keys()), nil case "dropped_attributes_count": return accessResourceDroppedAttributesCount[K](), nil default: @@ -61,7 +61,7 @@ func accessResourceAttributes[K ResourceContext]() ottl.StandardGetSetter[K] { } } -func accessResourceAttributesKey[K ResourceContext](keys ottl.Key[K]) ottl.StandardGetSetter[K] { +func accessResourceAttributesKey[K ResourceContext](keys []ottl.Key[K]) ottl.StandardGetSetter[K] { return ottl.StandardGetSetter[K]{ Getter: func(ctx context.Context, tCtx K) (any, error) { return GetMapValue[K](ctx, tCtx, tCtx.GetResource().Attributes(), keys) diff --git a/pkg/ottl/contexts/internal/resource_test.go b/pkg/ottl/contexts/internal/resource_test.go index a9f689c2c234..2f805b3d98fa 100644 --- a/pkg/ottl/contexts/internal/resource_test.go +++ b/pkg/ottl/contexts/internal/resource_test.go @@ -51,8 +51,10 @@ func TestResourcePathGetSetter(t *testing.T) { name: "attributes string", path: &TestPath[*resourceContext]{ N: "attributes", - Keys: &TestKey[*resourceContext]{ - S: ottltest.Strp("str"), + KeySlice: []ottl.Key[*resourceContext]{ + &TestKey[*resourceContext]{ + S: ottltest.Strp("str"), + }, }, }, orig: "val", @@ -65,8 +67,10 @@ func TestResourcePathGetSetter(t *testing.T) { name: "attributes bool", path: &TestPath[*resourceContext]{ N: "attributes", - Keys: &TestKey[*resourceContext]{ - S: ottltest.Strp("bool"), + KeySlice: []ottl.Key[*resourceContext]{ + &TestKey[*resourceContext]{ + S: ottltest.Strp("bool"), + }, }, }, orig: true, @@ -79,8 +83,10 @@ func TestResourcePathGetSetter(t *testing.T) { name: "attributes int", path: &TestPath[*resourceContext]{ N: "attributes", - Keys: &TestKey[*resourceContext]{ - S: ottltest.Strp("int"), + KeySlice: []ottl.Key[*resourceContext]{ + &TestKey[*resourceContext]{ + S: ottltest.Strp("int"), + }, }, }, orig: int64(10), @@ -93,8 +99,10 @@ func TestResourcePathGetSetter(t *testing.T) { name: "attributes float", path: &TestPath[*resourceContext]{ N: "attributes", - Keys: &TestKey[*resourceContext]{ - S: ottltest.Strp("double"), + KeySlice: []ottl.Key[*resourceContext]{ + &TestKey[*resourceContext]{ + S: ottltest.Strp("double"), + }, }, }, orig: 1.2, @@ -107,8 +115,10 @@ func TestResourcePathGetSetter(t *testing.T) { name: "attributes bytes", path: &TestPath[*resourceContext]{ N: "attributes", - Keys: &TestKey[*resourceContext]{ - S: ottltest.Strp("bytes"), + KeySlice: []ottl.Key[*resourceContext]{ + &TestKey[*resourceContext]{ + S: ottltest.Strp("bytes"), + }, }, }, orig: []byte{1, 3, 2}, @@ -121,8 +131,10 @@ func TestResourcePathGetSetter(t *testing.T) { name: "attributes array empty", path: &TestPath[*resourceContext]{ N: "attributes", - Keys: &TestKey[*resourceContext]{ - S: ottltest.Strp("arr_empty"), + KeySlice: []ottl.Key[*resourceContext]{ + &TestKey[*resourceContext]{ + S: ottltest.Strp("arr_empty"), + }, }, }, orig: func() pcommon.Slice { @@ -138,8 +150,10 @@ func TestResourcePathGetSetter(t *testing.T) { name: "attributes array string", path: &TestPath[*resourceContext]{ N: "attributes", - Keys: &TestKey[*resourceContext]{ - S: ottltest.Strp("arr_str"), + KeySlice: []ottl.Key[*resourceContext]{ + &TestKey[*resourceContext]{ + S: ottltest.Strp("arr_str"), + }, }, }, orig: func() pcommon.Slice { @@ -155,8 +169,10 @@ func TestResourcePathGetSetter(t *testing.T) { name: "attributes array bool", path: &TestPath[*resourceContext]{ N: "attributes", - Keys: &TestKey[*resourceContext]{ - S: ottltest.Strp("arr_bool"), + KeySlice: []ottl.Key[*resourceContext]{ + &TestKey[*resourceContext]{ + S: ottltest.Strp("arr_bool"), + }, }, }, orig: func() pcommon.Slice { @@ -172,8 +188,10 @@ func TestResourcePathGetSetter(t *testing.T) { name: "attributes array int", path: &TestPath[*resourceContext]{ N: "attributes", - Keys: &TestKey[*resourceContext]{ - S: ottltest.Strp("arr_int"), + KeySlice: []ottl.Key[*resourceContext]{ + &TestKey[*resourceContext]{ + S: ottltest.Strp("arr_int"), + }, }, }, orig: func() pcommon.Slice { @@ -189,8 +207,10 @@ func TestResourcePathGetSetter(t *testing.T) { name: "attributes array float", path: &TestPath[*resourceContext]{ N: "attributes", - Keys: &TestKey[*resourceContext]{ - S: ottltest.Strp("arr_float"), + KeySlice: []ottl.Key[*resourceContext]{ + &TestKey[*resourceContext]{ + S: ottltest.Strp("arr_float"), + }, }, }, orig: func() pcommon.Slice { @@ -206,8 +226,10 @@ func TestResourcePathGetSetter(t *testing.T) { name: "attributes array bytes", path: &TestPath[*resourceContext]{ N: "attributes", - Keys: &TestKey[*resourceContext]{ - S: ottltest.Strp("arr_bytes"), + KeySlice: []ottl.Key[*resourceContext]{ + &TestKey[*resourceContext]{ + S: ottltest.Strp("arr_bytes"), + }, }, }, orig: func() pcommon.Slice { @@ -223,13 +245,15 @@ func TestResourcePathGetSetter(t *testing.T) { name: "attributes nested", path: &TestPath[*resourceContext]{ N: "attributes", - Keys: &TestKey[*resourceContext]{ - S: ottltest.Strp("slice"), - NextKey: &TestKey[*resourceContext]{ + KeySlice: []ottl.Key[*resourceContext]{ + &TestKey[*resourceContext]{ + S: ottltest.Strp("slice"), + }, + &TestKey[*resourceContext]{ I: ottltest.Intp(0), - NextKey: &TestKey[*resourceContext]{ - S: ottltest.Strp("map"), - }, + }, + &TestKey[*resourceContext]{ + S: ottltest.Strp("map"), }, }, }, @@ -247,13 +271,15 @@ func TestResourcePathGetSetter(t *testing.T) { name: "attributes nested new values", path: &TestPath[*resourceContext]{ N: "attributes", - Keys: &TestKey[*resourceContext]{ - S: ottltest.Strp("new"), - NextKey: &TestKey[*resourceContext]{ + KeySlice: []ottl.Key[*resourceContext]{ + &TestKey[*resourceContext]{ + S: ottltest.Strp("new"), + }, + &TestKey[*resourceContext]{ I: ottltest.Intp(2), - NextKey: &TestKey[*resourceContext]{ - I: ottltest.Intp(0), - }, + }, + &TestKey[*resourceContext]{ + I: ottltest.Intp(0), }, }, }, diff --git a/pkg/ottl/contexts/internal/scope.go b/pkg/ottl/contexts/internal/scope.go index dc0f44e5dd2d..d7e462cd1aa5 100644 --- a/pkg/ottl/contexts/internal/scope.go +++ b/pkg/ottl/contexts/internal/scope.go @@ -26,7 +26,7 @@ func ScopePathGetSetter[K InstrumentationScopeContext](path ottl.Path[K]) (ottl. case "version": return accessInstrumentationScopeVersion[K](), nil case "attributes": - mapKeys := path.Key() + mapKeys := path.Keys() if mapKeys == nil { return accessInstrumentationScopeAttributes[K](), nil } @@ -66,13 +66,13 @@ func accessInstrumentationScopeAttributes[K InstrumentationScopeContext]() ottl. } } -func accessInstrumentationScopeAttributesKey[K InstrumentationScopeContext](key ottl.Key[K]) ottl.StandardGetSetter[K] { +func accessInstrumentationScopeAttributesKey[K InstrumentationScopeContext](keys []ottl.Key[K]) ottl.StandardGetSetter[K] { return ottl.StandardGetSetter[K]{ Getter: func(ctx context.Context, tCtx K) (any, error) { - return GetMapValue[K](ctx, tCtx, tCtx.GetInstrumentationScope().Attributes(), key) + return GetMapValue[K](ctx, tCtx, tCtx.GetInstrumentationScope().Attributes(), keys) }, Setter: func(ctx context.Context, tCtx K, val any) error { - return SetMapValue[K](ctx, tCtx, tCtx.GetInstrumentationScope().Attributes(), key, val) + return SetMapValue[K](ctx, tCtx, tCtx.GetInstrumentationScope().Attributes(), keys, val) }, } } diff --git a/pkg/ottl/contexts/internal/scope_test.go b/pkg/ottl/contexts/internal/scope_test.go index 5ac20b188e32..04abe5b712c4 100644 --- a/pkg/ottl/contexts/internal/scope_test.go +++ b/pkg/ottl/contexts/internal/scope_test.go @@ -72,8 +72,10 @@ func TestScopePathGetSetter(t *testing.T) { name: "attributes string", path: &TestPath[*instrumentationScopeContext]{ N: "attributes", - Keys: &TestKey[*instrumentationScopeContext]{ - S: ottltest.Strp("str"), + KeySlice: []ottl.Key[*instrumentationScopeContext]{ + &TestKey[*instrumentationScopeContext]{ + S: ottltest.Strp("str"), + }, }, }, orig: "val", @@ -97,8 +99,10 @@ func TestScopePathGetSetter(t *testing.T) { name: "attributes bool", path: &TestPath[*instrumentationScopeContext]{ N: "attributes", - Keys: &TestKey[*instrumentationScopeContext]{ - S: ottltest.Strp("bool"), + KeySlice: []ottl.Key[*instrumentationScopeContext]{ + &TestKey[*instrumentationScopeContext]{ + S: ottltest.Strp("bool"), + }, }, }, orig: true, @@ -111,8 +115,10 @@ func TestScopePathGetSetter(t *testing.T) { name: "attributes int", path: &TestPath[*instrumentationScopeContext]{ N: "attributes", - Keys: &TestKey[*instrumentationScopeContext]{ - S: ottltest.Strp("int"), + KeySlice: []ottl.Key[*instrumentationScopeContext]{ + &TestKey[*instrumentationScopeContext]{ + S: ottltest.Strp("int"), + }, }, }, orig: int64(10), @@ -125,8 +131,10 @@ func TestScopePathGetSetter(t *testing.T) { name: "attributes float", path: &TestPath[*instrumentationScopeContext]{ N: "attributes", - Keys: &TestKey[*instrumentationScopeContext]{ - S: ottltest.Strp("double"), + KeySlice: []ottl.Key[*instrumentationScopeContext]{ + &TestKey[*instrumentationScopeContext]{ + S: ottltest.Strp("double"), + }, }, }, orig: 1.2, @@ -139,8 +147,10 @@ func TestScopePathGetSetter(t *testing.T) { name: "attributes bytes", path: &TestPath[*instrumentationScopeContext]{ N: "attributes", - Keys: &TestKey[*instrumentationScopeContext]{ - S: ottltest.Strp("bytes"), + KeySlice: []ottl.Key[*instrumentationScopeContext]{ + &TestKey[*instrumentationScopeContext]{ + S: ottltest.Strp("bytes"), + }, }, }, orig: []byte{1, 3, 2}, @@ -153,8 +163,10 @@ func TestScopePathGetSetter(t *testing.T) { name: "attributes array empty", path: &TestPath[*instrumentationScopeContext]{ N: "attributes", - Keys: &TestKey[*instrumentationScopeContext]{ - S: ottltest.Strp("arr_empty"), + KeySlice: []ottl.Key[*instrumentationScopeContext]{ + &TestKey[*instrumentationScopeContext]{ + S: ottltest.Strp("arr_empty"), + }, }, }, orig: func() pcommon.Slice { @@ -170,8 +182,10 @@ func TestScopePathGetSetter(t *testing.T) { name: "attributes array string", path: &TestPath[*instrumentationScopeContext]{ N: "attributes", - Keys: &TestKey[*instrumentationScopeContext]{ - S: ottltest.Strp("arr_str"), + KeySlice: []ottl.Key[*instrumentationScopeContext]{ + &TestKey[*instrumentationScopeContext]{ + S: ottltest.Strp("arr_str"), + }, }, }, orig: func() pcommon.Slice { @@ -188,8 +202,10 @@ func TestScopePathGetSetter(t *testing.T) { name: "attributes array bool", path: &TestPath[*instrumentationScopeContext]{ N: "attributes", - Keys: &TestKey[*instrumentationScopeContext]{ - S: ottltest.Strp("arr_bool"), + KeySlice: []ottl.Key[*instrumentationScopeContext]{ + &TestKey[*instrumentationScopeContext]{ + S: ottltest.Strp("arr_bool"), + }, }, }, orig: func() pcommon.Slice { @@ -206,8 +222,10 @@ func TestScopePathGetSetter(t *testing.T) { name: "attributes array int", path: &TestPath[*instrumentationScopeContext]{ N: "attributes", - Keys: &TestKey[*instrumentationScopeContext]{ - S: ottltest.Strp("arr_int"), + KeySlice: []ottl.Key[*instrumentationScopeContext]{ + &TestKey[*instrumentationScopeContext]{ + S: ottltest.Strp("arr_int"), + }, }, }, orig: func() pcommon.Slice { @@ -224,8 +242,10 @@ func TestScopePathGetSetter(t *testing.T) { name: "attributes array float", path: &TestPath[*instrumentationScopeContext]{ N: "attributes", - Keys: &TestKey[*instrumentationScopeContext]{ - S: ottltest.Strp("arr_float"), + KeySlice: []ottl.Key[*instrumentationScopeContext]{ + &TestKey[*instrumentationScopeContext]{ + S: ottltest.Strp("arr_float"), + }, }, }, orig: func() pcommon.Slice { @@ -242,8 +262,10 @@ func TestScopePathGetSetter(t *testing.T) { name: "attributes array bytes", path: &TestPath[*instrumentationScopeContext]{ N: "attributes", - Keys: &TestKey[*instrumentationScopeContext]{ - S: ottltest.Strp("arr_bytes"), + KeySlice: []ottl.Key[*instrumentationScopeContext]{ + &TestKey[*instrumentationScopeContext]{ + S: ottltest.Strp("arr_bytes"), + }, }, }, orig: func() pcommon.Slice { @@ -260,13 +282,15 @@ func TestScopePathGetSetter(t *testing.T) { name: "attributes nested", path: &TestPath[*instrumentationScopeContext]{ N: "attributes", - Keys: &TestKey[*instrumentationScopeContext]{ - S: ottltest.Strp("slice"), - NextKey: &TestKey[*instrumentationScopeContext]{ + KeySlice: []ottl.Key[*instrumentationScopeContext]{ + &TestKey[*instrumentationScopeContext]{ + S: ottltest.Strp("slice"), + }, + &TestKey[*instrumentationScopeContext]{ I: ottltest.Intp(0), - NextKey: &TestKey[*instrumentationScopeContext]{ - S: ottltest.Strp("map"), - }, + }, + &TestKey[*instrumentationScopeContext]{ + S: ottltest.Strp("map"), }, }, }, @@ -284,13 +308,15 @@ func TestScopePathGetSetter(t *testing.T) { name: "attributes nested new values", path: &TestPath[*instrumentationScopeContext]{ N: "attributes", - Keys: &TestKey[*instrumentationScopeContext]{ - S: ottltest.Strp("new"), - NextKey: &TestKey[*instrumentationScopeContext]{ + KeySlice: []ottl.Key[*instrumentationScopeContext]{ + &TestKey[*instrumentationScopeContext]{ + S: ottltest.Strp("new"), + }, + &TestKey[*instrumentationScopeContext]{ I: ottltest.Intp(2), - NextKey: &TestKey[*instrumentationScopeContext]{ - I: ottltest.Intp(0), - }, + }, + &TestKey[*instrumentationScopeContext]{ + I: ottltest.Intp(0), }, }, }, diff --git a/pkg/ottl/contexts/internal/slice.go b/pkg/ottl/contexts/internal/slice.go index ed6c9ae7b322..5a90e281a902 100644 --- a/pkg/ottl/contexts/internal/slice.go +++ b/pkg/ottl/contexts/internal/slice.go @@ -12,12 +12,12 @@ import ( "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" ) -func GetSliceValue[K any](ctx context.Context, tCtx K, s pcommon.Slice, key ottl.Key[K]) (any, error) { - if key == nil { +func GetSliceValue[K any](ctx context.Context, tCtx K, s pcommon.Slice, keys []ottl.Key[K]) (any, error) { + if len(keys) == 0 { return nil, fmt.Errorf("cannot get slice value without key") } - i, err := key.Int(ctx, tCtx) + i, err := keys[0].Int(ctx, tCtx) if err != nil { return nil, err } @@ -31,15 +31,15 @@ func GetSliceValue[K any](ctx context.Context, tCtx K, s pcommon.Slice, key ottl return nil, fmt.Errorf("index %d out of bounds", idx) } - return getIndexableValue[K](ctx, tCtx, s.At(idx), key.Next()) + return getIndexableValue[K](ctx, tCtx, s.At(idx), keys[1:]) } -func SetSliceValue[K any](ctx context.Context, tCtx K, s pcommon.Slice, key ottl.Key[K], val any) error { - if key == nil { +func SetSliceValue[K any](ctx context.Context, tCtx K, s pcommon.Slice, keys []ottl.Key[K], val any) error { + if len(keys) == 0 { return fmt.Errorf("cannot set slice value without key") } - i, err := key.Int(ctx, tCtx) + i, err := keys[0].Int(ctx, tCtx) if err != nil { return err } @@ -53,5 +53,5 @@ func SetSliceValue[K any](ctx context.Context, tCtx K, s pcommon.Slice, key ottl return fmt.Errorf("index %d out of bounds", idx) } - return setIndexableValue[K](ctx, tCtx, s.At(idx), val, key.Next()) + return setIndexableValue[K](ctx, tCtx, s.At(idx), val, keys[1:]) } diff --git a/pkg/ottl/contexts/internal/slice_test.go b/pkg/ottl/contexts/internal/slice_test.go index 85a7b416c2eb..0f238225251a 100644 --- a/pkg/ottl/contexts/internal/slice_test.go +++ b/pkg/ottl/contexts/internal/slice_test.go @@ -18,35 +18,43 @@ import ( func Test_GetSliceValue_Invalid(t *testing.T) { tests := []struct { name string - keys ottl.Key[any] + keys []ottl.Key[any] err error }{ { name: "first key not an integer", - keys: &TestKey[any]{ - S: ottltest.Strp("key"), + keys: []ottl.Key[any]{ + &TestKey[any]{ + S: ottltest.Strp("key"), + }, }, err: fmt.Errorf("non-integer indexing is not supported"), }, { name: "index too large", - keys: &TestKey[any]{ - I: ottltest.Intp(1), + keys: []ottl.Key[any]{ + &TestKey[any]{ + I: ottltest.Intp(1), + }, }, err: fmt.Errorf("index 1 out of bounds"), }, { name: "index too small", - keys: &TestKey[any]{ - I: ottltest.Intp(-1), + keys: []ottl.Key[any]{ + &TestKey[any]{ + I: ottltest.Intp(-1), + }, }, err: fmt.Errorf("index -1 out of bounds"), }, { name: "invalid type", - keys: &TestKey[any]{ - I: ottltest.Intp(0), - NextKey: &TestKey[any]{ + keys: []ottl.Key[any]{ + &TestKey[any]{ + I: ottltest.Intp(0), + }, + &TestKey[any]{ S: ottltest.Strp("string"), }, }, @@ -73,35 +81,43 @@ func Test_GetSliceValue_NilKey(t *testing.T) { func Test_SetSliceValue_Invalid(t *testing.T) { tests := []struct { name string - keys ottl.Key[any] + keys []ottl.Key[any] err error }{ { name: "first key not an integer", - keys: &TestKey[any]{ - S: ottltest.Strp("key"), + keys: []ottl.Key[any]{ + &TestKey[any]{ + S: ottltest.Strp("key"), + }, }, err: fmt.Errorf("non-integer indexing is not supported"), }, { name: "index too large", - keys: &TestKey[any]{ - I: ottltest.Intp(1), + keys: []ottl.Key[any]{ + &TestKey[any]{ + I: ottltest.Intp(1), + }, }, err: fmt.Errorf("index 1 out of bounds"), }, { name: "index too small", - keys: &TestKey[any]{ - I: ottltest.Intp(-1), + keys: []ottl.Key[any]{ + &TestKey[any]{ + I: ottltest.Intp(-1), + }, }, err: fmt.Errorf("index -1 out of bounds"), }, { name: "invalid type", - keys: &TestKey[any]{ - I: ottltest.Intp(0), - NextKey: &TestKey[any]{ + keys: []ottl.Key[any]{ + &TestKey[any]{ + I: ottltest.Intp(0), + }, + &TestKey[any]{ S: ottltest.Strp("string"), }, }, diff --git a/pkg/ottl/contexts/internal/span.go b/pkg/ottl/contexts/internal/span.go index 165ce14777fd..0fb218413aea 100644 --- a/pkg/ottl/contexts/internal/span.go +++ b/pkg/ottl/contexts/internal/span.go @@ -55,7 +55,7 @@ func SpanPathGetSetter[K SpanContext](path ottl.Path[K]) (ottl.GetSetter[K], err return accessSpanID[K](), nil } case "trace_state": - mapKey := path.Key() + mapKey := path.Keys() if mapKey == nil { return accessTraceState[K](), nil } @@ -93,7 +93,7 @@ func SpanPathGetSetter[K SpanContext](path ottl.Path[K]) (ottl.GetSetter[K], err case "end_time": return accessEndTime[K](), nil case "attributes": - mapKeys := path.Key() + mapKeys := path.Keys() if mapKeys == nil { return accessAttributes[K](), nil } @@ -220,14 +220,14 @@ func accessTraceState[K SpanContext]() ottl.StandardGetSetter[K] { } } -func accessTraceStateKey[K SpanContext](keys ottl.Key[K]) (ottl.StandardGetSetter[K], error) { - if keys.Next() != nil { +func accessTraceStateKey[K SpanContext](keys []ottl.Key[K]) (ottl.StandardGetSetter[K], error) { + if len(keys) != 1 { return ottl.StandardGetSetter[K]{}, fmt.Errorf("must provide exactly 1 key when accessing trace_state") } return ottl.StandardGetSetter[K]{ Getter: func(ctx context.Context, tCtx K) (any, error) { if ts, err := trace.ParseTraceState(tCtx.GetSpan().TraceState().AsRaw()); err == nil { - s, err := keys.String(ctx, tCtx) + s, err := keys[0].String(ctx, tCtx) if err != nil { return nil, err } @@ -241,7 +241,7 @@ func accessTraceStateKey[K SpanContext](keys ottl.Key[K]) (ottl.StandardGetSette Setter: func(ctx context.Context, tCtx K, val any) error { if str, ok := val.(string); ok { if ts, err := trace.ParseTraceState(tCtx.GetSpan().TraceState().AsRaw()); err == nil { - s, err := keys.String(ctx, tCtx) + s, err := keys[0].String(ctx, tCtx) if err != nil { return err } @@ -451,7 +451,7 @@ func accessAttributes[K SpanContext]() ottl.StandardGetSetter[K] { } } -func accessAttributesKey[K SpanContext](keys ottl.Key[K]) ottl.StandardGetSetter[K] { +func accessAttributesKey[K SpanContext](keys []ottl.Key[K]) ottl.StandardGetSetter[K] { return ottl.StandardGetSetter[K]{ Getter: func(ctx context.Context, tCtx K) (any, error) { return GetMapValue[K](ctx, tCtx, tCtx.GetSpan().Attributes(), keys) diff --git a/pkg/ottl/contexts/internal/span_test.go b/pkg/ottl/contexts/internal/span_test.go index a0c695944d4d..f55b9a70139e 100644 --- a/pkg/ottl/contexts/internal/span_test.go +++ b/pkg/ottl/contexts/internal/span_test.go @@ -111,8 +111,10 @@ func TestSpanPathGetSetter(t *testing.T) { name: "trace_state key", path: &TestPath[*spanContext]{ N: "trace_state", - Keys: &TestKey[*spanContext]{ - S: ottltest.Strp("key1"), + KeySlice: []ottl.Key[*spanContext]{ + &TestKey[*spanContext]{ + S: ottltest.Strp("key1"), + }, }, }, orig: "val1", @@ -233,8 +235,10 @@ func TestSpanPathGetSetter(t *testing.T) { name: "attributes string", path: &TestPath[*spanContext]{ N: "attributes", - Keys: &TestKey[*spanContext]{ - S: ottltest.Strp("str"), + KeySlice: []ottl.Key[*spanContext]{ + &TestKey[*spanContext]{ + S: ottltest.Strp("str"), + }, }, }, orig: "val", @@ -247,8 +251,10 @@ func TestSpanPathGetSetter(t *testing.T) { name: "attributes bool", path: &TestPath[*spanContext]{ N: "attributes", - Keys: &TestKey[*spanContext]{ - S: ottltest.Strp("bool"), + KeySlice: []ottl.Key[*spanContext]{ + &TestKey[*spanContext]{ + S: ottltest.Strp("bool"), + }, }, }, orig: true, @@ -261,8 +267,10 @@ func TestSpanPathGetSetter(t *testing.T) { name: "attributes int", path: &TestPath[*spanContext]{ N: "attributes", - Keys: &TestKey[*spanContext]{ - S: ottltest.Strp("int"), + KeySlice: []ottl.Key[*spanContext]{ + &TestKey[*spanContext]{ + S: ottltest.Strp("int"), + }, }, }, orig: int64(10), @@ -275,8 +283,10 @@ func TestSpanPathGetSetter(t *testing.T) { name: "attributes float", path: &TestPath[*spanContext]{ N: "attributes", - Keys: &TestKey[*spanContext]{ - S: ottltest.Strp("double"), + KeySlice: []ottl.Key[*spanContext]{ + &TestKey[*spanContext]{ + S: ottltest.Strp("double"), + }, }, }, orig: float64(1.2), @@ -289,8 +299,10 @@ func TestSpanPathGetSetter(t *testing.T) { name: "attributes bytes", path: &TestPath[*spanContext]{ N: "attributes", - Keys: &TestKey[*spanContext]{ - S: ottltest.Strp("bytes"), + KeySlice: []ottl.Key[*spanContext]{ + &TestKey[*spanContext]{ + S: ottltest.Strp("bytes"), + }, }, }, orig: []byte{1, 3, 2}, @@ -303,8 +315,10 @@ func TestSpanPathGetSetter(t *testing.T) { name: "attributes array empty", path: &TestPath[*spanContext]{ N: "attributes", - Keys: &TestKey[*spanContext]{ - S: ottltest.Strp("arr_empty"), + KeySlice: []ottl.Key[*spanContext]{ + &TestKey[*spanContext]{ + S: ottltest.Strp("arr_empty"), + }, }, }, orig: func() pcommon.Slice { @@ -320,8 +334,10 @@ func TestSpanPathGetSetter(t *testing.T) { name: "attributes array string", path: &TestPath[*spanContext]{ N: "attributes", - Keys: &TestKey[*spanContext]{ - S: ottltest.Strp("arr_str"), + KeySlice: []ottl.Key[*spanContext]{ + &TestKey[*spanContext]{ + S: ottltest.Strp("arr_str"), + }, }, }, orig: func() pcommon.Slice { @@ -337,8 +353,10 @@ func TestSpanPathGetSetter(t *testing.T) { name: "attributes array bool", path: &TestPath[*spanContext]{ N: "attributes", - Keys: &TestKey[*spanContext]{ - S: ottltest.Strp("arr_bool"), + KeySlice: []ottl.Key[*spanContext]{ + &TestKey[*spanContext]{ + S: ottltest.Strp("arr_bool"), + }, }, }, orig: func() pcommon.Slice { @@ -354,8 +372,10 @@ func TestSpanPathGetSetter(t *testing.T) { name: "attributes array int", path: &TestPath[*spanContext]{ N: "attributes", - Keys: &TestKey[*spanContext]{ - S: ottltest.Strp("arr_int"), + KeySlice: []ottl.Key[*spanContext]{ + &TestKey[*spanContext]{ + S: ottltest.Strp("arr_int"), + }, }, }, orig: func() pcommon.Slice { @@ -371,8 +391,10 @@ func TestSpanPathGetSetter(t *testing.T) { name: "attributes array float", path: &TestPath[*spanContext]{ N: "attributes", - Keys: &TestKey[*spanContext]{ - S: ottltest.Strp("arr_float"), + KeySlice: []ottl.Key[*spanContext]{ + &TestKey[*spanContext]{ + S: ottltest.Strp("arr_float"), + }, }, }, orig: func() pcommon.Slice { @@ -388,8 +410,10 @@ func TestSpanPathGetSetter(t *testing.T) { name: "attributes array bytes", path: &TestPath[*spanContext]{ N: "attributes", - Keys: &TestKey[*spanContext]{ - S: ottltest.Strp("arr_bytes"), + KeySlice: []ottl.Key[*spanContext]{ + &TestKey[*spanContext]{ + S: ottltest.Strp("arr_bytes"), + }, }, }, orig: func() pcommon.Slice { @@ -405,13 +429,15 @@ func TestSpanPathGetSetter(t *testing.T) { name: "attributes nested", path: &TestPath[*spanContext]{ N: "attributes", - Keys: &TestKey[*spanContext]{ - S: ottltest.Strp("slice"), - NextKey: &TestKey[*spanContext]{ + KeySlice: []ottl.Key[*spanContext]{ + &TestKey[*spanContext]{ + S: ottltest.Strp("slice"), + }, + &TestKey[*spanContext]{ I: ottltest.Intp(0), - NextKey: &TestKey[*spanContext]{ - S: ottltest.Strp("map"), - }, + }, + &TestKey[*spanContext]{ + S: ottltest.Strp("map"), }, }, }, @@ -429,13 +455,15 @@ func TestSpanPathGetSetter(t *testing.T) { name: "attributes nested new values", path: &TestPath[*spanContext]{ N: "attributes", - Keys: &TestKey[*spanContext]{ - S: ottltest.Strp("new"), - NextKey: &TestKey[*spanContext]{ + KeySlice: []ottl.Key[*spanContext]{ + &TestKey[*spanContext]{ + S: ottltest.Strp("new"), + }, + &TestKey[*spanContext]{ I: ottltest.Intp(2), - NextKey: &TestKey[*spanContext]{ - I: ottltest.Intp(0), - }, + }, + &TestKey[*spanContext]{ + I: ottltest.Intp(0), }, }, }, diff --git a/pkg/ottl/contexts/internal/value.go b/pkg/ottl/contexts/internal/value.go index ccd96f2befc2..ce335854eb3a 100644 --- a/pkg/ottl/contexts/internal/value.go +++ b/pkg/ottl/contexts/internal/value.go @@ -68,13 +68,13 @@ func SetValue(value pcommon.Value, val any) error { return err } -func getIndexableValue[K any](ctx context.Context, tCtx K, value pcommon.Value, key ottl.Key[K]) (any, error) { - val, currentKey := value, key +func getIndexableValue[K any](ctx context.Context, tCtx K, value pcommon.Value, keys []ottl.Key[K]) (any, error) { + val := value var ok bool - for currentKey != nil { + for i := 0; i < len(keys); i++ { switch val.Type() { case pcommon.ValueTypeMap: - s, err := currentKey.String(ctx, tCtx) + s, err := keys[i].String(ctx, tCtx) if err != nil { return nil, err } @@ -86,7 +86,7 @@ func getIndexableValue[K any](ctx context.Context, tCtx K, value pcommon.Value, return nil, nil } case pcommon.ValueTypeSlice: - i, err := currentKey.Int(ctx, tCtx) + i, err := keys[i].Int(ctx, tCtx) if err != nil { return nil, err } @@ -100,12 +100,11 @@ func getIndexableValue[K any](ctx context.Context, tCtx K, value pcommon.Value, default: return nil, fmt.Errorf("type %v does not support string indexing", val.Type()) } - currentKey = currentKey.Next() } return ottlcommon.GetValue(val), nil } -func setIndexableValue[K any](ctx context.Context, tCtx K, currentValue pcommon.Value, val any, key ottl.Key[K]) error { +func setIndexableValue[K any](ctx context.Context, tCtx K, currentValue pcommon.Value, val any, keys []ottl.Key[K]) error { var newValue pcommon.Value switch val.(type) { case []string, []bool, []int64, []float64, [][]byte, []any: @@ -118,11 +117,10 @@ func setIndexableValue[K any](ctx context.Context, tCtx K, currentValue pcommon. return err } - currentKey := key - for currentKey != nil { + for i := 0; i < len(keys); i++ { switch currentValue.Type() { case pcommon.ValueTypeMap: - s, err := currentKey.String(ctx, tCtx) + s, err := keys[i].String(ctx, tCtx) if err != nil { return err } @@ -136,7 +134,7 @@ func setIndexableValue[K any](ctx context.Context, tCtx K, currentValue pcommon. currentValue = potentialValue } case pcommon.ValueTypeSlice: - i, err := currentKey.Int(ctx, tCtx) + i, err := keys[i].Int(ctx, tCtx) if err != nil { return err } @@ -148,11 +146,11 @@ func setIndexableValue[K any](ctx context.Context, tCtx K, currentValue pcommon. } currentValue = currentValue.Slice().At(int(*i)) case pcommon.ValueTypeEmpty: - s, err := currentKey.String(ctx, tCtx) + s, err := keys[i].String(ctx, tCtx) if err != nil { return err } - i, err := currentKey.Int(ctx, tCtx) + i, err := keys[i].Int(ctx, tCtx) if err != nil { return err } @@ -171,7 +169,6 @@ func setIndexableValue[K any](ctx context.Context, tCtx K, currentValue pcommon. default: return fmt.Errorf("type %v does not support string indexing", currentValue.Type()) } - currentKey = currentKey.Next() } newValue.CopyTo(currentValue) return nil diff --git a/pkg/ottl/contexts/internal/value_test.go b/pkg/ottl/contexts/internal/value_test.go index df7c77362a02..5be89f72ea46 100644 --- a/pkg/ottl/contexts/internal/value_test.go +++ b/pkg/ottl/contexts/internal/value_test.go @@ -9,10 +9,14 @@ import ( "github.com/stretchr/testify/assert" "go.opentelemetry.io/collector/pdata/pcommon" + + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" ) func Test_SetIndexableValue_EmptyValueNoIndex(t *testing.T) { - keys := TestKey[any]{} - err := setIndexableValue[any](context.Background(), nil, pcommon.NewValueEmpty(), nil, &keys) + keys := []ottl.Key[any]{ + &TestKey[any]{}, + } + err := setIndexableValue[any](context.Background(), nil, pcommon.NewValueEmpty(), nil, keys) assert.Error(t, err) } diff --git a/pkg/ottl/contexts/ottldatapoint/datapoint.go b/pkg/ottl/contexts/ottldatapoint/datapoint.go index c977abfff037..b8c0307257a0 100644 --- a/pkg/ottl/contexts/ottldatapoint/datapoint.go +++ b/pkg/ottl/contexts/ottldatapoint/datapoint.go @@ -145,10 +145,10 @@ func (pep *pathExpressionParser) parsePath(path ottl.Path[TransformContext]) (ot } switch path.Name() { case "cache": - if path.Key() == nil { + if path.Keys() == nil { return accessCache(), nil } - return accessCacheKey(path.Key()), nil + return accessCacheKey(path.Keys()), nil case "resource": return internal.ResourcePathGetSetter[TransformContext](path.Next()) case "instrumentation_scope": @@ -156,10 +156,10 @@ func (pep *pathExpressionParser) parsePath(path ottl.Path[TransformContext]) (ot case "metric": return internal.MetricPathGetSetter[TransformContext](path.Next()) case "attributes": - if path.Key() == nil { + if path.Keys() == nil { return accessAttributes(), nil } - return accessAttributesKey(path.Key()), nil + return accessAttributesKey(path.Keys()), nil case "start_time_unix_nano": return accessStartTimeUnixNano(), nil case "time_unix_nano": @@ -236,7 +236,7 @@ func accessCache() ottl.StandardGetSetter[TransformContext] { } } -func accessCacheKey(key ottl.Key[TransformContext]) ottl.StandardGetSetter[TransformContext] { +func accessCacheKey(key []ottl.Key[TransformContext]) ottl.StandardGetSetter[TransformContext] { return ottl.StandardGetSetter[TransformContext]{ Getter: func(ctx context.Context, tCtx TransformContext) (any, error) { return internal.GetMapValue[TransformContext](ctx, tCtx, tCtx.getCache(), key) @@ -286,7 +286,7 @@ func accessAttributes() ottl.StandardGetSetter[TransformContext] { } } -func accessAttributesKey(key ottl.Key[TransformContext]) ottl.StandardGetSetter[TransformContext] { +func accessAttributesKey(key []ottl.Key[TransformContext]) ottl.StandardGetSetter[TransformContext] { return ottl.StandardGetSetter[TransformContext]{ Getter: func(ctx context.Context, tCtx TransformContext) (any, error) { switch tCtx.GetDataPoint().(type) { diff --git a/pkg/ottl/contexts/ottldatapoint/datapoint_test.go b/pkg/ottl/contexts/ottldatapoint/datapoint_test.go index 3c26db075b25..7ff174fceada 100644 --- a/pkg/ottl/contexts/ottldatapoint/datapoint_test.go +++ b/pkg/ottl/contexts/ottldatapoint/datapoint_test.go @@ -44,8 +44,10 @@ func Test_newPathGetSetter_Cache(t *testing.T) { name: "cache access", path: &internal.TestPath[TransformContext]{ N: "cache", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("temp"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("temp"), + }, }, }, orig: nil, @@ -206,8 +208,10 @@ func Test_newPathGetSetter_NumberDataPoint(t *testing.T) { name: "attributes string", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("str"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("str"), + }, }, }, orig: "val", @@ -220,8 +224,10 @@ func Test_newPathGetSetter_NumberDataPoint(t *testing.T) { name: "attributes bool", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("bool"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("bool"), + }, }, }, orig: true, @@ -234,8 +240,10 @@ func Test_newPathGetSetter_NumberDataPoint(t *testing.T) { name: "attributes int", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("int"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("int"), + }, }, }, orig: int64(10), @@ -248,8 +256,10 @@ func Test_newPathGetSetter_NumberDataPoint(t *testing.T) { name: "attributes float", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("double"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("double"), + }, }, }, orig: float64(1.2), @@ -262,8 +272,10 @@ func Test_newPathGetSetter_NumberDataPoint(t *testing.T) { name: "attributes bytes", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("bytes"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("bytes"), + }, }, }, orig: []byte{1, 3, 2}, @@ -276,8 +288,10 @@ func Test_newPathGetSetter_NumberDataPoint(t *testing.T) { name: "attributes array string", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_str"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_str"), + }, }, }, orig: func() pcommon.Slice { @@ -293,8 +307,10 @@ func Test_newPathGetSetter_NumberDataPoint(t *testing.T) { name: "attributes array bool", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_bool"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_bool"), + }, }, }, orig: func() pcommon.Slice { @@ -310,8 +326,10 @@ func Test_newPathGetSetter_NumberDataPoint(t *testing.T) { name: "attributes array int", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_int"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_int"), + }, }, }, orig: func() pcommon.Slice { @@ -327,8 +345,10 @@ func Test_newPathGetSetter_NumberDataPoint(t *testing.T) { name: "attributes array float", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_float"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_float"), + }, }, }, orig: func() pcommon.Slice { @@ -344,8 +364,10 @@ func Test_newPathGetSetter_NumberDataPoint(t *testing.T) { name: "attributes array bytes", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_bytes"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_bytes"), + }, }, }, orig: func() pcommon.Slice { @@ -361,8 +383,10 @@ func Test_newPathGetSetter_NumberDataPoint(t *testing.T) { name: "attributes pcommon.Map", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("pMap"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("pMap"), + }, }, }, orig: func() pcommon.Map { @@ -380,8 +404,10 @@ func Test_newPathGetSetter_NumberDataPoint(t *testing.T) { name: "attributes map[string]any", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("map"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("map"), + }, }, }, orig: func() pcommon.Map { @@ -399,13 +425,15 @@ func Test_newPathGetSetter_NumberDataPoint(t *testing.T) { name: "attributes nested", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("slice"), - NextKey: &internal.TestKey[TransformContext]{ + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("slice"), + }, + &internal.TestKey[TransformContext]{ I: ottltest.Intp(0), - NextKey: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("map"), - }, + }, + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("map"), }, }, }, @@ -423,13 +451,15 @@ func Test_newPathGetSetter_NumberDataPoint(t *testing.T) { name: "attributes nested new values", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("new"), - NextKey: &internal.TestKey[TransformContext]{ + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("new"), + }, + &internal.TestKey[TransformContext]{ I: ottltest.Intp(2), - NextKey: &internal.TestKey[TransformContext]{ - I: ottltest.Intp(0), - }, + }, + &internal.TestKey[TransformContext]{ + I: ottltest.Intp(0), }, }, }, @@ -613,8 +643,10 @@ func Test_newPathGetSetter_HistogramDataPoint(t *testing.T) { name: "attributes string", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("str"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("str"), + }, }, }, orig: "val", @@ -627,8 +659,10 @@ func Test_newPathGetSetter_HistogramDataPoint(t *testing.T) { name: "attributes bool", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("bool"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("bool"), + }, }, }, orig: true, @@ -641,8 +675,10 @@ func Test_newPathGetSetter_HistogramDataPoint(t *testing.T) { name: "attributes int", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("int"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("int"), + }, }, }, orig: int64(10), @@ -655,8 +691,10 @@ func Test_newPathGetSetter_HistogramDataPoint(t *testing.T) { name: "attributes float", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("double"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("double"), + }, }, }, orig: float64(1.2), @@ -669,8 +707,10 @@ func Test_newPathGetSetter_HistogramDataPoint(t *testing.T) { name: "attributes bytes", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("bytes"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("bytes"), + }, }, }, orig: []byte{1, 3, 2}, @@ -683,8 +723,10 @@ func Test_newPathGetSetter_HistogramDataPoint(t *testing.T) { name: "attributes array string", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_str"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_str"), + }, }, }, orig: func() pcommon.Slice { @@ -700,8 +742,10 @@ func Test_newPathGetSetter_HistogramDataPoint(t *testing.T) { name: "attributes array bool", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_bool"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_bool"), + }, }, }, orig: func() pcommon.Slice { @@ -717,8 +761,10 @@ func Test_newPathGetSetter_HistogramDataPoint(t *testing.T) { name: "attributes array int", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_int"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_int"), + }, }, }, orig: func() pcommon.Slice { @@ -734,8 +780,10 @@ func Test_newPathGetSetter_HistogramDataPoint(t *testing.T) { name: "attributes array float", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_float"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_float"), + }, }, }, orig: func() pcommon.Slice { @@ -751,8 +799,10 @@ func Test_newPathGetSetter_HistogramDataPoint(t *testing.T) { name: "attributes array bytes", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_bytes"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_bytes"), + }, }, }, orig: func() pcommon.Slice { @@ -768,8 +818,10 @@ func Test_newPathGetSetter_HistogramDataPoint(t *testing.T) { name: "attributes pcommon.Map", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("pMap"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("pMap"), + }, }, }, orig: func() pcommon.Map { @@ -787,8 +839,10 @@ func Test_newPathGetSetter_HistogramDataPoint(t *testing.T) { name: "attributes map[string]any", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("map"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("map"), + }, }, }, orig: func() pcommon.Map { @@ -806,13 +860,15 @@ func Test_newPathGetSetter_HistogramDataPoint(t *testing.T) { name: "attributes nested", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("slice"), - NextKey: &internal.TestKey[TransformContext]{ + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("slice"), + }, + &internal.TestKey[TransformContext]{ I: ottltest.Intp(0), - NextKey: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("map"), - }, + }, + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("map"), }, }, }, @@ -830,13 +886,15 @@ func Test_newPathGetSetter_HistogramDataPoint(t *testing.T) { name: "attributes nested new values", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("new"), - NextKey: &internal.TestKey[TransformContext]{ + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("new"), + }, + &internal.TestKey[TransformContext]{ I: ottltest.Intp(2), - NextKey: &internal.TestKey[TransformContext]{ - I: ottltest.Intp(0), - }, + }, + &internal.TestKey[TransformContext]{ + I: ottltest.Intp(0), }, }, }, @@ -1103,8 +1161,10 @@ func Test_newPathGetSetter_ExpoHistogramDataPoint(t *testing.T) { name: "attributes string", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("str"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("str"), + }, }, }, orig: "val", @@ -1117,8 +1177,10 @@ func Test_newPathGetSetter_ExpoHistogramDataPoint(t *testing.T) { name: "attributes bool", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("bool"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("bool"), + }, }, }, orig: true, @@ -1131,8 +1193,10 @@ func Test_newPathGetSetter_ExpoHistogramDataPoint(t *testing.T) { name: "attributes int", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("int"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("int"), + }, }, }, orig: int64(10), @@ -1145,8 +1209,10 @@ func Test_newPathGetSetter_ExpoHistogramDataPoint(t *testing.T) { name: "attributes float", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("double"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("double"), + }, }, }, orig: 1.2, @@ -1159,8 +1225,10 @@ func Test_newPathGetSetter_ExpoHistogramDataPoint(t *testing.T) { name: "attributes bytes", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("bytes"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("bytes"), + }, }, }, orig: []byte{1, 3, 2}, @@ -1173,8 +1241,10 @@ func Test_newPathGetSetter_ExpoHistogramDataPoint(t *testing.T) { name: "attributes array string", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_str"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_str"), + }, }, }, orig: func() pcommon.Slice { @@ -1190,8 +1260,10 @@ func Test_newPathGetSetter_ExpoHistogramDataPoint(t *testing.T) { name: "attributes array bool", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_bool"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_bool"), + }, }, }, orig: func() pcommon.Slice { @@ -1207,8 +1279,10 @@ func Test_newPathGetSetter_ExpoHistogramDataPoint(t *testing.T) { name: "attributes array int", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_int"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_int"), + }, }, }, orig: func() pcommon.Slice { @@ -1224,8 +1298,10 @@ func Test_newPathGetSetter_ExpoHistogramDataPoint(t *testing.T) { name: "attributes array float", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_float"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_float"), + }, }, }, orig: func() pcommon.Slice { @@ -1241,8 +1317,10 @@ func Test_newPathGetSetter_ExpoHistogramDataPoint(t *testing.T) { name: "attributes array bytes", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_bytes"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_bytes"), + }, }, }, orig: func() pcommon.Slice { @@ -1258,8 +1336,10 @@ func Test_newPathGetSetter_ExpoHistogramDataPoint(t *testing.T) { name: "attributes pcommon.Map", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("pMap"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("pMap"), + }, }, }, orig: func() pcommon.Map { @@ -1277,8 +1357,10 @@ func Test_newPathGetSetter_ExpoHistogramDataPoint(t *testing.T) { name: "attributes map[string]any", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("map"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("map"), + }, }, }, orig: func() pcommon.Map { @@ -1296,13 +1378,15 @@ func Test_newPathGetSetter_ExpoHistogramDataPoint(t *testing.T) { name: "attributes nested", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("slice"), - NextKey: &internal.TestKey[TransformContext]{ + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("slice"), + }, + &internal.TestKey[TransformContext]{ I: ottltest.Intp(0), - NextKey: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("map"), - }, + }, + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("map"), }, }, }, @@ -1320,13 +1404,15 @@ func Test_newPathGetSetter_ExpoHistogramDataPoint(t *testing.T) { name: "attributes nested new values", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("new"), - NextKey: &internal.TestKey[TransformContext]{ + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("new"), + }, + &internal.TestKey[TransformContext]{ I: ottltest.Intp(2), - NextKey: &internal.TestKey[TransformContext]{ - I: ottltest.Intp(0), - }, + }, + &internal.TestKey[TransformContext]{ + I: ottltest.Intp(0), }, }, }, @@ -1494,8 +1580,10 @@ func Test_newPathGetSetter_SummaryDataPoint(t *testing.T) { name: "attributes string", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("str"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("str"), + }, }, }, orig: "val", @@ -1508,8 +1596,10 @@ func Test_newPathGetSetter_SummaryDataPoint(t *testing.T) { name: "attributes bool", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("bool"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("bool"), + }, }, }, orig: true, @@ -1522,8 +1612,10 @@ func Test_newPathGetSetter_SummaryDataPoint(t *testing.T) { name: "attributes int", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("int"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("int"), + }, }, }, orig: int64(10), @@ -1536,8 +1628,10 @@ func Test_newPathGetSetter_SummaryDataPoint(t *testing.T) { name: "attributes float", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("double"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("double"), + }, }, }, orig: 1.2, @@ -1550,8 +1644,10 @@ func Test_newPathGetSetter_SummaryDataPoint(t *testing.T) { name: "attributes bytes", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("bytes"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("bytes"), + }, }, }, orig: []byte{1, 3, 2}, @@ -1564,8 +1660,10 @@ func Test_newPathGetSetter_SummaryDataPoint(t *testing.T) { name: "attributes array string", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_str"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_str"), + }, }, }, orig: func() pcommon.Slice { @@ -1581,8 +1679,10 @@ func Test_newPathGetSetter_SummaryDataPoint(t *testing.T) { name: "attributes array bool", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_bool"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_bool"), + }, }, }, orig: func() pcommon.Slice { @@ -1598,8 +1698,10 @@ func Test_newPathGetSetter_SummaryDataPoint(t *testing.T) { name: "attributes array int", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_int"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_int"), + }, }, }, orig: func() pcommon.Slice { @@ -1615,8 +1717,10 @@ func Test_newPathGetSetter_SummaryDataPoint(t *testing.T) { name: "attributes array float", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_float"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_float"), + }, }, }, orig: func() pcommon.Slice { @@ -1632,8 +1736,10 @@ func Test_newPathGetSetter_SummaryDataPoint(t *testing.T) { name: "attributes array bytes", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_bytes"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_bytes"), + }, }, }, orig: func() pcommon.Slice { @@ -1649,8 +1755,10 @@ func Test_newPathGetSetter_SummaryDataPoint(t *testing.T) { name: "attributes pcommon.Map", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("pMap"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("pMap"), + }, }, }, orig: func() pcommon.Map { @@ -1668,8 +1776,10 @@ func Test_newPathGetSetter_SummaryDataPoint(t *testing.T) { name: "attributes map[string]any", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("map"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("map"), + }, }, }, orig: func() pcommon.Map { @@ -1687,13 +1797,15 @@ func Test_newPathGetSetter_SummaryDataPoint(t *testing.T) { name: "attributes nested", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("slice"), - NextKey: &internal.TestKey[TransformContext]{ + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("slice"), + }, + &internal.TestKey[TransformContext]{ I: ottltest.Intp(0), - NextKey: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("map"), - }, + }, + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("map"), }, }, }, @@ -1711,13 +1823,15 @@ func Test_newPathGetSetter_SummaryDataPoint(t *testing.T) { name: "attributes nested new values", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("new"), - NextKey: &internal.TestKey[TransformContext]{ + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("new"), + }, + &internal.TestKey[TransformContext]{ I: ottltest.Intp(2), - NextKey: &internal.TestKey[TransformContext]{ - I: ottltest.Intp(0), - }, + }, + &internal.TestKey[TransformContext]{ + I: ottltest.Intp(0), }, }, }, diff --git a/pkg/ottl/contexts/ottllog/log.go b/pkg/ottl/contexts/ottllog/log.go index a1eeed624364..30a7fab352bc 100644 --- a/pkg/ottl/contexts/ottllog/log.go +++ b/pkg/ottl/contexts/ottllog/log.go @@ -152,10 +152,10 @@ func (pep *pathExpressionParser) parsePath(path ottl.Path[TransformContext]) (ot } switch path.Name() { case "cache": - if path.Key() == nil { + if path.Keys() == nil { return accessCache(), nil } - return accessCacheKey(path.Key()), nil + return accessCacheKey(path.Keys()), nil case "resource": return internal.ResourcePathGetSetter[TransformContext](path.Next()) case "instrumentation_scope": @@ -178,16 +178,16 @@ func (pep *pathExpressionParser) parsePath(path ottl.Path[TransformContext]) (ot return accessStringBody(), nil } } else { - if path.Key() == nil { + if path.Keys() == nil { return accessBody(), nil } - return accessBodyKey(path.Key()), nil + return accessBodyKey(path.Keys()), nil } case "attributes": - if path.Key() == nil { + if path.Keys() == nil { return accessAttributes(), nil } - return accessAttributesKey(path.Key()), nil + return accessAttributesKey(path.Keys()), nil case "dropped_attributes_count": return accessDroppedAttributesCount(), nil case "flags": @@ -226,7 +226,7 @@ func accessCache() ottl.StandardGetSetter[TransformContext] { } } -func accessCacheKey(key ottl.Key[TransformContext]) ottl.StandardGetSetter[TransformContext] { +func accessCacheKey(key []ottl.Key[TransformContext]) ottl.StandardGetSetter[TransformContext] { return ottl.StandardGetSetter[TransformContext]{ Getter: func(ctx context.Context, tCtx TransformContext) (any, error) { return internal.GetMapValue[TransformContext](ctx, tCtx, tCtx.getCache(), key) @@ -332,7 +332,7 @@ func accessBody() ottl.StandardGetSetter[TransformContext] { } } -func accessBodyKey(key ottl.Key[TransformContext]) ottl.StandardGetSetter[TransformContext] { +func accessBodyKey(key []ottl.Key[TransformContext]) ottl.StandardGetSetter[TransformContext] { return ottl.StandardGetSetter[TransformContext]{ Getter: func(ctx context.Context, tCtx TransformContext) (any, error) { body := tCtx.GetLogRecord().Body() @@ -387,7 +387,7 @@ func accessAttributes() ottl.StandardGetSetter[TransformContext] { } } -func accessAttributesKey(key ottl.Key[TransformContext]) ottl.StandardGetSetter[TransformContext] { +func accessAttributesKey(key []ottl.Key[TransformContext]) ottl.StandardGetSetter[TransformContext] { return ottl.StandardGetSetter[TransformContext]{ Getter: func(ctx context.Context, tCtx TransformContext) (any, error) { return internal.GetMapValue[TransformContext](ctx, tCtx, tCtx.GetLogRecord().Attributes(), key) diff --git a/pkg/ottl/contexts/ottllog/log_test.go b/pkg/ottl/contexts/ottllog/log_test.go index 1a9a214aa430..4b381a31b694 100644 --- a/pkg/ottl/contexts/ottllog/log_test.go +++ b/pkg/ottl/contexts/ottllog/log_test.go @@ -154,8 +154,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "map body index", path: &internal.TestPath[TransformContext]{ N: "body", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("key"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("key"), + }, }, }, orig: "val", @@ -187,8 +189,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "slice body index", path: &internal.TestPath[TransformContext]{ N: "body", - Keys: &internal.TestKey[TransformContext]{ - I: ottltest.Intp(0), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + I: ottltest.Intp(0), + }, }, }, orig: "body", @@ -289,8 +293,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "cache access", path: &internal.TestPath[TransformContext]{ N: "cache", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("temp"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("temp"), + }, }, }, orig: nil, @@ -314,8 +320,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes string", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("str"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("str"), + }, }, }, orig: "val", @@ -328,8 +336,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes bool", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("bool"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("bool"), + }, }, }, orig: true, @@ -342,8 +352,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes int", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("int"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("int"), + }, }, }, orig: int64(10), @@ -356,8 +368,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes float", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("double"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("double"), + }, }, }, orig: float64(1.2), @@ -370,8 +384,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes bytes", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("bytes"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("bytes"), + }, }, }, orig: []byte{1, 3, 2}, @@ -384,8 +400,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes array string", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_str"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_str"), + }, }, }, orig: func() pcommon.Slice { @@ -401,8 +419,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes array bool", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_bool"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_bool"), + }, }, }, orig: func() pcommon.Slice { @@ -418,8 +438,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes array int", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_int"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_int"), + }, }, }, orig: func() pcommon.Slice { @@ -435,8 +457,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes array float", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_float"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_float"), + }, }, }, orig: func() pcommon.Slice { @@ -452,8 +476,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes array bytes", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_bytes"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_bytes"), + }, }, }, orig: func() pcommon.Slice { @@ -469,8 +495,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes pcommon.Map", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("pMap"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("pMap"), + }, }, }, orig: func() pcommon.Map { @@ -488,8 +516,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes map[string]any", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("map"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("map"), + }, }, }, orig: func() pcommon.Map { @@ -507,13 +537,15 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes nested", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("slice"), - NextKey: &internal.TestKey[TransformContext]{ + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("slice"), + }, + &internal.TestKey[TransformContext]{ I: ottltest.Intp(0), - NextKey: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("map"), - }, + }, + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("map"), }, }, }, @@ -531,13 +563,15 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes nested new values", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("new"), - NextKey: &internal.TestKey[TransformContext]{ + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("new"), + }, + &internal.TestKey[TransformContext]{ I: ottltest.Intp(2), - NextKey: &internal.TestKey[TransformContext]{ - I: ottltest.Intp(0), - }, + }, + &internal.TestKey[TransformContext]{ + I: ottltest.Intp(0), }, }, }, @@ -689,8 +723,10 @@ func createTelemetry(bodyType string) (plog.LogRecord, pcommon.InstrumentationSc func Test_InvalidBodyIndexing(t *testing.T) { path := internal.TestPath[TransformContext]{ N: "body", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("key"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("key"), + }, }, } diff --git a/pkg/ottl/contexts/ottlmetric/metrics.go b/pkg/ottl/contexts/ottlmetric/metrics.go index d17a6640f6f4..0cdc916fd14f 100644 --- a/pkg/ottl/contexts/ottlmetric/metrics.go +++ b/pkg/ottl/contexts/ottlmetric/metrics.go @@ -130,10 +130,10 @@ func (pep *pathExpressionParser) parsePath(path ottl.Path[TransformContext]) (ot } switch path.Name() { case "cache": - if path.Key() == nil { + if path.Keys() == nil { return accessCache(), nil } - return accessCacheKey(path.Key()), nil + return accessCacheKey(path.Keys()), nil case "resource": return internal.ResourcePathGetSetter[TransformContext](path.Next()) case "instrumentation_scope": @@ -157,7 +157,7 @@ func accessCache() ottl.StandardGetSetter[TransformContext] { } } -func accessCacheKey(key ottl.Key[TransformContext]) ottl.StandardGetSetter[TransformContext] { +func accessCacheKey(key []ottl.Key[TransformContext]) ottl.StandardGetSetter[TransformContext] { return ottl.StandardGetSetter[TransformContext]{ Getter: func(ctx context.Context, tCtx TransformContext) (any, error) { return internal.GetMapValue[TransformContext](ctx, tCtx, tCtx.getCache(), key) diff --git a/pkg/ottl/contexts/ottlmetric/metrics_test.go b/pkg/ottl/contexts/ottlmetric/metrics_test.go index 8735573b9790..111181855b6e 100644 --- a/pkg/ottl/contexts/ottlmetric/metrics_test.go +++ b/pkg/ottl/contexts/ottlmetric/metrics_test.go @@ -128,8 +128,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "cache access", path: &internal.TestPath[TransformContext]{ N: "cache", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("temp"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("temp"), + }, }, }, orig: nil, diff --git a/pkg/ottl/contexts/ottlresource/resource.go b/pkg/ottl/contexts/ottlresource/resource.go index 99d55a02314a..a7bd956129d0 100644 --- a/pkg/ottl/contexts/ottlresource/resource.go +++ b/pkg/ottl/contexts/ottlresource/resource.go @@ -101,10 +101,10 @@ func (pep *pathExpressionParser) parsePath(path ottl.Path[TransformContext]) (ot } switch path.Name() { case "cache": - if path.Key() == nil { + if path.Keys() == nil { return accessCache(), nil } - return accessCacheKey(path.Key()), nil + return accessCacheKey(path.Keys()), nil default: return internal.ResourcePathGetSetter[TransformContext](path) } @@ -124,7 +124,7 @@ func accessCache() ottl.StandardGetSetter[TransformContext] { } } -func accessCacheKey(key ottl.Key[TransformContext]) ottl.StandardGetSetter[TransformContext] { +func accessCacheKey(key []ottl.Key[TransformContext]) ottl.StandardGetSetter[TransformContext] { return ottl.StandardGetSetter[TransformContext]{ Getter: func(ctx context.Context, tCtx TransformContext) (any, error) { return internal.GetMapValue[TransformContext](ctx, tCtx, tCtx.getCache(), key) diff --git a/pkg/ottl/contexts/ottlresource/resource_test.go b/pkg/ottl/contexts/ottlresource/resource_test.go index a891835d2cc4..2f2a95bc6723 100644 --- a/pkg/ottl/contexts/ottlresource/resource_test.go +++ b/pkg/ottl/contexts/ottlresource/resource_test.go @@ -55,8 +55,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "cache access", path: &internal.TestPath[TransformContext]{ N: "cache", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("temp"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("temp"), + }, }, }, orig: nil, @@ -80,8 +82,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes string", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("str"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("str"), + }, }, }, orig: "val", @@ -94,8 +98,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes bool", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("bool"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("bool"), + }, }, }, orig: true, @@ -108,8 +114,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes int", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("int"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("int"), + }, }, }, orig: int64(10), @@ -122,8 +130,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes float", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("double"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("double"), + }, }, }, orig: float64(1.2), @@ -136,8 +146,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes bytes", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("bytes"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("bytes"), + }, }, }, orig: []byte{1, 3, 2}, @@ -150,8 +162,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes array string", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_str"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_str"), + }, }, }, orig: func() pcommon.Slice { @@ -167,8 +181,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes array bool", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_bool"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_bool"), + }, }, }, orig: func() pcommon.Slice { @@ -184,8 +200,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes array int", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_int"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_int"), + }, }, }, orig: func() pcommon.Slice { @@ -201,8 +219,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes array float", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_float"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_float"), + }, }, }, orig: func() pcommon.Slice { @@ -218,8 +238,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes array bytes", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_bytes"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_bytes"), + }, }, }, orig: func() pcommon.Slice { @@ -235,8 +257,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes pcommon.Map", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("pMap"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("pMap"), + }, }, }, orig: func() pcommon.Map { @@ -254,8 +278,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes mpa[string]interface", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("map"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("map"), + }, }, }, orig: func() pcommon.Map { @@ -273,13 +299,15 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes nested", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("slice"), - NextKey: &internal.TestKey[TransformContext]{ + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("slice"), + }, + &internal.TestKey[TransformContext]{ I: ottltest.Intp(0), - NextKey: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("map"), - }, + }, + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("map"), }, }, }, @@ -297,13 +325,15 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes nested new values", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("new"), - NextKey: &internal.TestKey[TransformContext]{ + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("new"), + }, + &internal.TestKey[TransformContext]{ I: ottltest.Intp(2), - NextKey: &internal.TestKey[TransformContext]{ - I: ottltest.Intp(0), - }, + }, + &internal.TestKey[TransformContext]{ + I: ottltest.Intp(0), }, }, }, diff --git a/pkg/ottl/contexts/ottlscope/scope.go b/pkg/ottl/contexts/ottlscope/scope.go index c175c80400ea..c19e21892887 100644 --- a/pkg/ottl/contexts/ottlscope/scope.go +++ b/pkg/ottl/contexts/ottlscope/scope.go @@ -108,10 +108,10 @@ func (pep *pathExpressionParser) parsePath(path ottl.Path[TransformContext]) (ot } switch path.Name() { case "cache": - if path.Key() == nil { + if path.Keys() == nil { return accessCache(), nil } - return accessCacheKey(path.Key()), nil + return accessCacheKey(path.Keys()), nil case "resource": return internal.ResourcePathGetSetter[TransformContext](path.Next()) default: @@ -133,7 +133,7 @@ func accessCache() ottl.StandardGetSetter[TransformContext] { } } -func accessCacheKey(key ottl.Key[TransformContext]) ottl.StandardGetSetter[TransformContext] { +func accessCacheKey(key []ottl.Key[TransformContext]) ottl.StandardGetSetter[TransformContext] { return ottl.StandardGetSetter[TransformContext]{ Getter: func(ctx context.Context, tCtx TransformContext) (any, error) { return internal.GetMapValue[TransformContext](ctx, tCtx, tCtx.getCache(), key) diff --git a/pkg/ottl/contexts/ottlscope/scope_test.go b/pkg/ottl/contexts/ottlscope/scope_test.go index a65d17a25a0a..5ade4838e5e5 100644 --- a/pkg/ottl/contexts/ottlscope/scope_test.go +++ b/pkg/ottl/contexts/ottlscope/scope_test.go @@ -55,8 +55,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "cache access", path: &internal.TestPath[TransformContext]{ N: "cache", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("temp"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("temp"), + }, }, }, orig: nil, @@ -80,8 +82,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes string", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("str"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("str"), + }, }, }, orig: "val", @@ -94,8 +98,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes bool", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("bool"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("bool"), + }, }, }, orig: true, @@ -108,8 +114,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes int", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("int"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("int"), + }, }, }, orig: int64(10), @@ -122,9 +130,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes float", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - - S: ottltest.Strp("double"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("double"), + }, }, }, orig: float64(1.2), @@ -137,8 +146,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes bytes", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("bytes"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("bytes"), + }, }, }, orig: []byte{1, 3, 2}, @@ -151,8 +162,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes array string", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_str"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_str"), + }, }, }, orig: func() pcommon.Slice { @@ -168,8 +181,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes array bool", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_bool"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_bool"), + }, }, }, orig: func() pcommon.Slice { @@ -185,8 +200,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes array int", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_int"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_int"), + }, }, }, orig: func() pcommon.Slice { @@ -202,8 +219,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes array float", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_float"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_float"), + }, }, }, orig: func() pcommon.Slice { @@ -219,8 +238,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes array bytes", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_bytes"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_bytes"), + }, }, }, orig: func() pcommon.Slice { @@ -236,8 +257,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes pcommon.Map", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("pMap"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("pMap"), + }, }, }, orig: func() pcommon.Map { @@ -255,8 +278,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes map[string]any", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("map"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("map"), + }, }, }, orig: func() pcommon.Map { @@ -274,13 +299,15 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes nested", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("slice"), - NextKey: &internal.TestKey[TransformContext]{ + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("slice"), + }, + &internal.TestKey[TransformContext]{ I: ottltest.Intp(0), - NextKey: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("map"), - }, + }, + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("map"), }, }, }, @@ -298,13 +325,15 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes nested new values", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("new"), - NextKey: &internal.TestKey[TransformContext]{ + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("new"), + }, + &internal.TestKey[TransformContext]{ I: ottltest.Intp(2), - NextKey: &internal.TestKey[TransformContext]{ - I: ottltest.Intp(0), - }, + }, + &internal.TestKey[TransformContext]{ + I: ottltest.Intp(0), }, }, }, diff --git a/pkg/ottl/contexts/ottlspan/span.go b/pkg/ottl/contexts/ottlspan/span.go index 501e9bcc2a86..8f400540ba66 100644 --- a/pkg/ottl/contexts/ottlspan/span.go +++ b/pkg/ottl/contexts/ottlspan/span.go @@ -121,10 +121,10 @@ func (pep *pathExpressionParser) parsePath(path ottl.Path[TransformContext]) (ot } switch path.Name() { case "cache": - if path.Key() == nil { + if path.Keys() == nil { return accessCache(), nil } - return accessCacheKey(path.Key()), nil + return accessCacheKey(path.Keys()), nil case "resource": return internal.ResourcePathGetSetter[TransformContext](path.Next()) case "instrumentation_scope": @@ -148,7 +148,7 @@ func accessCache() ottl.StandardGetSetter[TransformContext] { } } -func accessCacheKey(key ottl.Key[TransformContext]) ottl.StandardGetSetter[TransformContext] { +func accessCacheKey(key []ottl.Key[TransformContext]) ottl.StandardGetSetter[TransformContext] { return ottl.StandardGetSetter[TransformContext]{ Getter: func(ctx context.Context, tCtx TransformContext) (any, error) { return internal.GetMapValue[TransformContext](ctx, tCtx, tCtx.getCache(), key) diff --git a/pkg/ottl/contexts/ottlspan/span_test.go b/pkg/ottl/contexts/ottlspan/span_test.go index 49eecc988b94..239b697dd2ce 100644 --- a/pkg/ottl/contexts/ottlspan/span_test.go +++ b/pkg/ottl/contexts/ottlspan/span_test.go @@ -74,8 +74,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "cache access", path: &internal.TestPath[TransformContext]{ N: "cache", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("temp"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("temp"), + }, }, }, orig: nil, @@ -149,8 +151,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "trace_state key", path: &internal.TestPath[TransformContext]{ N: "trace_state", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("key1"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("key1"), + }, }, }, orig: "val1", @@ -279,8 +283,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes string", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("str"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("str"), + }, }, }, orig: "val", @@ -293,8 +299,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes bool", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("bool"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("bool"), + }, }, }, orig: true, @@ -307,9 +315,11 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes int", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ - S: ottltest.Strp("int"), + S: ottltest.Strp("int"), + }, }, }, orig: int64(10), @@ -323,9 +333,11 @@ func Test_newPathGetSetter(t *testing.T) { path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ - S: ottltest.Strp("double"), + S: ottltest.Strp("double"), + }, }, }, orig: float64(1.2), @@ -339,9 +351,11 @@ func Test_newPathGetSetter(t *testing.T) { path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ - S: ottltest.Strp("bytes"), + S: ottltest.Strp("bytes"), + }, }, }, orig: []byte{1, 3, 2}, @@ -355,9 +369,11 @@ func Test_newPathGetSetter(t *testing.T) { path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_str"), + S: ottltest.Strp("arr_str"), + }, }, }, orig: func() pcommon.Slice { @@ -374,9 +390,11 @@ func Test_newPathGetSetter(t *testing.T) { path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_bool"), + S: ottltest.Strp("arr_bool"), + }, }, }, orig: func() pcommon.Slice { @@ -393,9 +411,11 @@ func Test_newPathGetSetter(t *testing.T) { path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_int"), + S: ottltest.Strp("arr_int"), + }, }, }, orig: func() pcommon.Slice { @@ -412,9 +432,11 @@ func Test_newPathGetSetter(t *testing.T) { path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_float"), + S: ottltest.Strp("arr_float"), + }, }, }, orig: func() pcommon.Slice { @@ -431,9 +453,11 @@ func Test_newPathGetSetter(t *testing.T) { path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_bytes"), + S: ottltest.Strp("arr_bytes"), + }, }, }, orig: func() pcommon.Slice { @@ -450,9 +474,11 @@ func Test_newPathGetSetter(t *testing.T) { path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ - S: ottltest.Strp("pMap"), + S: ottltest.Strp("pMap"), + }, }, }, orig: func() pcommon.Map { @@ -471,9 +497,11 @@ func Test_newPathGetSetter(t *testing.T) { path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ - S: ottltest.Strp("map"), + S: ottltest.Strp("map"), + }, }, }, orig: func() pcommon.Map { @@ -490,16 +518,16 @@ func Test_newPathGetSetter(t *testing.T) { { name: "attributes nested", path: &internal.TestPath[TransformContext]{ - N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - - S: ottltest.Strp("slice"), - NextKey: &internal.TestKey[TransformContext]{ + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("slice"), + }, + &internal.TestKey[TransformContext]{ I: ottltest.Intp(0), - NextKey: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("map"), - }, + }, + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("map"), }, }, }, @@ -517,13 +545,15 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes nested new values", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("new"), - NextKey: &internal.TestKey[TransformContext]{ + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("new"), + }, + &internal.TestKey[TransformContext]{ I: ottltest.Intp(2), - NextKey: &internal.TestKey[TransformContext]{ - I: ottltest.Intp(0), - }, + }, + &internal.TestKey[TransformContext]{ + I: ottltest.Intp(0), }, }, }, diff --git a/pkg/ottl/contexts/ottlspanevent/span_events.go b/pkg/ottl/contexts/ottlspanevent/span_events.go index b16ebb1e74c4..5a1001fc3774 100644 --- a/pkg/ottl/contexts/ottlspanevent/span_events.go +++ b/pkg/ottl/contexts/ottlspanevent/span_events.go @@ -129,10 +129,10 @@ func (pep *pathExpressionParser) parsePath(path ottl.Path[TransformContext]) (ot } switch path.Name() { case "cache": - if path.Key() == nil { + if path.Keys() == nil { return accessCache(), nil } - return accessCacheKey(path.Key()), nil + return accessCacheKey(path.Keys()), nil case "resource": return internal.ResourcePathGetSetter[TransformContext](path.Next()) case "instrumentation_scope": @@ -146,10 +146,10 @@ func (pep *pathExpressionParser) parsePath(path ottl.Path[TransformContext]) (ot case "name": return accessSpanEventName(), nil case "attributes": - if path.Key() == nil { + if path.Keys() == nil { return accessSpanEventAttributes(), nil } - return accessSpanEventAttributesKey(path.Key()), nil + return accessSpanEventAttributesKey(path.Keys()), nil case "dropped_attributes_count": return accessSpanEventDroppedAttributeCount(), nil } @@ -170,7 +170,7 @@ func accessCache() ottl.StandardGetSetter[TransformContext] { } } -func accessCacheKey(key ottl.Key[TransformContext]) ottl.StandardGetSetter[TransformContext] { +func accessCacheKey(key []ottl.Key[TransformContext]) ottl.StandardGetSetter[TransformContext] { return ottl.StandardGetSetter[TransformContext]{ Getter: func(ctx context.Context, tCtx TransformContext) (any, error) { return internal.GetMapValue[TransformContext](ctx, tCtx, tCtx.getCache(), key) @@ -237,7 +237,7 @@ func accessSpanEventAttributes() ottl.StandardGetSetter[TransformContext] { } } -func accessSpanEventAttributesKey(key ottl.Key[TransformContext]) ottl.StandardGetSetter[TransformContext] { +func accessSpanEventAttributesKey(key []ottl.Key[TransformContext]) ottl.StandardGetSetter[TransformContext] { return ottl.StandardGetSetter[TransformContext]{ Getter: func(ctx context.Context, tCtx TransformContext) (any, error) { return internal.GetMapValue[TransformContext](ctx, tCtx, tCtx.GetSpanEvent().Attributes(), key) diff --git a/pkg/ottl/contexts/ottlspanevent/span_events_test.go b/pkg/ottl/contexts/ottlspanevent/span_events_test.go index afa4322f7ab8..b71ce4a07c84 100644 --- a/pkg/ottl/contexts/ottlspanevent/span_events_test.go +++ b/pkg/ottl/contexts/ottlspanevent/span_events_test.go @@ -81,8 +81,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "cache access", path: &internal.TestPath[TransformContext]{ N: "cache", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("temp"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("temp"), + }, }, }, orig: nil, @@ -128,8 +130,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes string", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("str"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("str"), + }, }, }, orig: "val", @@ -142,8 +146,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes bool", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("bool"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("bool"), + }, }, }, orig: true, @@ -156,8 +162,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes int", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("int"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("int"), + }, }, }, orig: int64(10), @@ -170,8 +178,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes float", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("double"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("double"), + }, }, }, orig: float64(1.2), @@ -184,8 +194,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes bytes", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("bytes"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("bytes"), + }, }, }, orig: []byte{1, 3, 2}, @@ -198,8 +210,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes array string", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_str"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_str"), + }, }, }, orig: func() pcommon.Slice { @@ -215,8 +229,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes array bool", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_bool"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_bool"), + }, }, }, orig: func() pcommon.Slice { @@ -232,8 +248,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes array int", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_int"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_int"), + }, }, }, orig: func() pcommon.Slice { @@ -249,8 +267,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes array float", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_float"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_float"), + }, }, }, orig: func() pcommon.Slice { @@ -266,8 +286,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes array bytes", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("arr_bytes"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("arr_bytes"), + }, }, }, orig: func() pcommon.Slice { @@ -283,8 +305,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes pcommon.Map", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("pMap"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("pMap"), + }, }, }, orig: func() pcommon.Map { @@ -302,8 +326,10 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes map[string]any", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("map"), + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("map"), + }, }, }, orig: func() pcommon.Map { @@ -321,13 +347,15 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes nested", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("slice"), - NextKey: &internal.TestKey[TransformContext]{ + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("slice"), + }, + &internal.TestKey[TransformContext]{ I: ottltest.Intp(0), - NextKey: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("map"), - }, + }, + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("map"), }, }, }, @@ -345,13 +373,15 @@ func Test_newPathGetSetter(t *testing.T) { name: "attributes nested new values", path: &internal.TestPath[TransformContext]{ N: "attributes", - Keys: &internal.TestKey[TransformContext]{ - S: ottltest.Strp("new"), - NextKey: &internal.TestKey[TransformContext]{ + KeySlice: []ottl.Key[TransformContext]{ + &internal.TestKey[TransformContext]{ + S: ottltest.Strp("new"), + }, + &internal.TestKey[TransformContext]{ I: ottltest.Intp(2), - NextKey: &internal.TestKey[TransformContext]{ - I: ottltest.Intp(0), - }, + }, + &internal.TestKey[TransformContext]{ + I: ottltest.Intp(0), }, }, }, diff --git a/pkg/ottl/functions.go b/pkg/ottl/functions.go index bf7db8e55e32..8e70332d0176 100644 --- a/pkg/ottl/functions.go +++ b/pkg/ottl/functions.go @@ -29,7 +29,7 @@ func newPath[K any](fields []field) (*basePath[K], error) { for i := len(fields) - 1; i >= 0; i-- { current = &basePath[K]{ name: fields[i].Name, - key: newKey[K](fields[i].Keys), + keys: newKeys[K](fields[i].Keys), nextPath: current, } } @@ -48,18 +48,19 @@ type Path[K any] interface { // Will return nil if there is no next path. Next() Path[K] - // Key provides the Key for this Path. - // Will return nil if there is no Key. - Key() Key[K] + // Keys provides the Keys for this Path. + // Will return nil if there are no Keys. + Keys() []Key[K] } var _ Path[any] = &basePath[any]{} type basePath[K any] struct { - name string - key *baseKey[K] - nextPath *basePath[K] - fetched bool + name string + keys []Key[K] + nextPath *basePath[K] + fetched bool + fetchedKeys bool } func (p *basePath[K]) Name() string { @@ -74,36 +75,39 @@ func (p *basePath[K]) Next() Path[K] { return p.nextPath } -func (p *basePath[K]) Key() Key[K] { - if p.key == nil { +func (p *basePath[K]) Keys() []Key[K] { + if p.keys == nil { return nil } - return p.key + p.fetchedKeys = true + return p.keys } func (p *basePath[K]) isComplete() error { if !p.fetched { return fmt.Errorf("the path section %q was not used by the context - this likely means you are using extra path sections", p.name) } + if p.keys != nil && !p.fetchedKeys { + return fmt.Errorf("the keys indexing %q were not used by the context - this likely means you are trying to index a path that does not support indexing", p.name) + } if p.nextPath == nil { return nil } return p.nextPath.isComplete() } -func newKey[K any](keys []key) *baseKey[K] { +func newKeys[K any](keys []key) []Key[K] { if len(keys) == 0 { return nil } - var current *baseKey[K] - for i := len(keys) - 1; i >= 0; i-- { - current = &baseKey[K]{ - s: keys[i].String, - i: keys[i].Int, - nextKey: current, + ks := make([]Key[K], len(keys)) + for i := range keys { + ks[i] = &baseKey[K]{ + s: keys[i].String, + i: keys[i].Int, } } - return current + return ks } // Key represents a chain of keys in an OTTL statement, such as `attributes["foo"]["bar"]`. @@ -119,18 +123,13 @@ type Key[K any] interface { // If the Key does not have a int value the returned value is nil. // If Key experiences an error retrieving the value it is returned. Int(context.Context, K) (*int64, error) - - // Next provides the next Key. - // Will return nil if there is no next Key. - Next() Key[K] } var _ Key[any] = &baseKey[any]{} type baseKey[K any] struct { - s *string - i *int64 - nextKey *baseKey[K] + s *string + i *int64 } func (k *baseKey[K]) String(_ context.Context, _ K) (*string, error) { @@ -141,13 +140,6 @@ func (k *baseKey[K]) Int(_ context.Context, _ K) (*int64, error) { return k.i, nil } -func (k *baseKey[K]) Next() Key[K] { - if k.nextKey == nil { - return nil - } - return k.nextKey -} - func (p *Parser[K]) parsePath(ip *basePath[K]) (GetSetter[K], error) { g, err := p.pathParser(ip) if err != nil { diff --git a/pkg/ottl/functions_test.go b/pkg/ottl/functions_test.go index fe9c309fecb5..00e10f60ec36 100644 --- a/pkg/ottl/functions_test.go +++ b/pkg/ottl/functions_test.go @@ -417,6 +417,35 @@ func Test_NewFunctionCall_invalid(t *testing.T) { }, }, }, + { + name: "Keys not allowed", + inv: editor{ + Function: "testing_getsetter", + Arguments: []argument{ + { + Value: value{ + Literal: &mathExprLiteral{ + Path: &path{ + Fields: []field{ + { + Name: "name", + Keys: []key{ + { + String: ottltest.Strp("foo"), + }, + { + String: ottltest.Strp("bar"), + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, } for _, tt := range tests { @@ -1426,7 +1455,7 @@ func Test_NewFunctionCall(t *testing.T) { Path: &path{ Fields: []field{ { - Name: "name", + Name: "attributes", Keys: []key{ { String: ottltest.Strp("foo"), @@ -1445,6 +1474,28 @@ func Test_NewFunctionCall(t *testing.T) { }, want: nil, }, + { + name: "path that allows keys but none have been specified", + inv: editor{ + Function: "testing_getsetter", + Arguments: []argument{ + { + Value: value{ + Literal: &mathExprLiteral{ + Path: &path{ + Fields: []field{ + { + Name: "attributes", + }, + }, + }, + }, + }, + }, + }, + }, + want: nil, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -2165,12 +2216,16 @@ func Test_basePath_Next(t *testing.T) { assert.Nil(t, next.Next()) } -func Test_basePath_Key(t *testing.T) { +func Test_basePath_Keys(t *testing.T) { k := &baseKey[any]{} bp := basePath[any]{ - key: k, + keys: []Key[any]{ + k, + }, } - assert.Equal(t, k, bp.Key()) + ks := bp.Keys() + assert.Equal(t, 1, len(ks)) + assert.Equal(t, k, ks[0]) } func Test_basePath_isComplete(t *testing.T) { @@ -2286,10 +2341,10 @@ func Test_newPath(t *testing.T) { assert.NoError(t, err) p := Path[any](np) assert.Equal(t, "body", p.Name()) - assert.Nil(t, p.Key()) + assert.Nil(t, p.Keys()) p = p.Next() assert.Equal(t, "string", p.Name()) - assert.Nil(t, p.Key()) + assert.Nil(t, p.Keys()) assert.Nil(t, p.Next()) } @@ -2313,15 +2368,6 @@ func Test_baseKey_Int(t *testing.T) { assert.Equal(t, int64(1), *i) } -func Test_baseKey_Next(t *testing.T) { - bp := baseKey[any]{ - nextKey: &baseKey[any]{}, - } - next := bp.Next() - assert.NotNil(t, next) - assert.Nil(t, next.Next()) -} - func Test_newKey(t *testing.T) { keys := []key{ { @@ -2331,15 +2377,16 @@ func Test_newKey(t *testing.T) { String: ottltest.Strp("bar"), }, } - k := Key[any](newKey[any](keys)) - s, err := k.String(context.Background(), nil) + ks := newKeys[any](keys) + + assert.Equal(t, 2, len(ks)) + + s, err := ks[0].String(context.Background(), nil) assert.NoError(t, err) assert.NotNil(t, s) assert.Equal(t, "foo", *s) - k = k.Next() - s, err = k.String(context.Background(), nil) + s, err = ks[1].String(context.Background(), nil) assert.NoError(t, err) assert.NotNil(t, s) assert.Equal(t, "bar", *s) - assert.Nil(t, k.Next()) } diff --git a/pkg/ottl/parser_test.go b/pkg/ottl/parser_test.go index fa09f3253077..aea7ddf3609e 100644 --- a/pkg/ottl/parser_test.go +++ b/pkg/ottl/parser_test.go @@ -1236,6 +1236,10 @@ func Test_parseCondition_full(t *testing.T) { func testParsePath[K any](p Path[K]) (GetSetter[any], error) { if p != nil && (p.Name() == "name" || p.Name() == "attributes") { + if p.Name() == "attributes" { + p.Keys() + } + return &StandardGetSetter[any]{ Getter: func(ctx context.Context, tCtx any) (any, error) { return tCtx, nil