Skip to content

Commit

Permalink
[pkg/ottl] Validate Keys are used (#30162)
Browse files Browse the repository at this point in the history
**Description:** 
This PR updates OTTL's validation logic to validate the keys are
considered during path parsing.

Unlike the path, keys are needed during hot path execution. For that
reason, the contexts need all the keys during parsing, not an individual
key that might be linked to the next key.

This PR updates the Path interface to have a list of keys instead of a
link to the first Key. This keeps the keys interaction closer to the
original key struct from the grammar, which means when the interfaces
release we have less breaking changes.

**Link to tracking Issue:** <Issue number if applicable>
Closes
#30051

**Testing:** <Describe what testing was performed and which tests were
added.>
Added unit tests to validate that indexing fails on paths that dont use
keys and pass on paths that allow keys.

---------

Co-authored-by: Evan Bradley <11745660+evan-bradley@users.noreply.github.com>
  • Loading branch information
TylerHelmuth and evan-bradley authored Jan 8, 2024
1 parent bfb60fa commit a4853ed
Show file tree
Hide file tree
Showing 31 changed files with 1,134 additions and 674 deletions.
27 changes: 27 additions & 0 deletions .chloggen/ottl-validate-key.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: 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: []
18 changes: 9 additions & 9 deletions pkg/ottl/contexts/internal/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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:])
}
140 changes: 85 additions & 55 deletions pkg/ottl/contexts/internal/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,68 +11,80 @@ 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),
},
},
err: fmt.Errorf("map must be indexed by a string"),
},
{
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"),
},
},
err: fmt.Errorf("slice must be indexed by an int"),
},
{
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),
},
},
err: fmt.Errorf("index 1 out of bounds"),
},
{
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),
},
},
err: fmt.Errorf("index -1 out of bounds"),
},
{
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"),
},
},
Expand All @@ -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)
}
Expand All @@ -117,61 +131,73 @@ 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),
},
},
err: fmt.Errorf("map must be indexed by a string"),
},
{
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"),
},
},
err: fmt.Errorf("slice must be indexed by an int"),
},
{
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),
},
},
err: fmt.Errorf("index 1 out of bounds"),
},
{
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),
},
},
err: fmt.Errorf("index -1 out of bounds"),
},
{
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"),
},
},
Expand All @@ -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()
Expand All @@ -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()
Expand Down
18 changes: 5 additions & 13 deletions pkg/ottl/contexts/internal/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

Expand All @@ -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) {
Expand All @@ -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
}
6 changes: 3 additions & 3 deletions pkg/ottl/contexts/internal/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit a4853ed

Please sign in to comment.