Skip to content

[processor/transform] Add support for functions to validate parameters #9563

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- `k8sattributesprocessor`: Support regex capture groups in tag_name (#9525)
- `transformprocessor`: Add new `truncation` function to allow truncating string values in maps such as `attributes` or `resource.attributes` (#9546)
- `datadogexporter`: Add `api.fail_on_invalid_key` to fail fast if api key is invalid (#9426)
- `transformprocessor`: Add support for functions to validate parameters (#9563)
- `googlecloudexporter`: Add GCP cloud logging exporter (#9679)

### 🧰 Bug fixes 🧰
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (
"go.opentelemetry.io/collector/pdata/ptrace"
)

func hello() ExprFunc {
func hello() (ExprFunc, error) {
return func(ctx TransformContext) interface{} {
return "world"
}
}, nil
}

func Test_newGetter(t *testing.T) {
Expand Down
22 changes: 15 additions & 7 deletions processor/transformprocessor/internal/common/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ func DefaultFunctions() map[string]interface{} {
return registry
}

func set(target Setter, value Getter) ExprFunc {
func set(target Setter, value Getter) (ExprFunc, error) {
return func(ctx TransformContext) interface{} {
val := value.Get(ctx)
if val != nil {
target.Set(ctx, val)
}
return nil
}
}, nil
}

func keepKeys(target GetSetter, keys []string) ExprFunc {
func keepKeys(target GetSetter, keys []string) (ExprFunc, error) {
keySet := make(map[string]struct{}, len(keys))
for _, key := range keys {
keySet[key] = struct{}{}
Expand All @@ -68,10 +68,10 @@ func keepKeys(target GetSetter, keys []string) ExprFunc {
target.Set(ctx, filtered)
}
return nil
}
}, nil
}

func truncateAll(target GetSetter, limit int64) ExprFunc {
func truncateAll(target GetSetter, limit int64) (ExprFunc, error) {
return func(ctx TransformContext) interface{} {
if limit < 0 {
return nil
Expand All @@ -98,7 +98,7 @@ func truncateAll(target GetSetter, limit int64) ExprFunc {
// TODO: Write log when truncation is performed
}
return nil
}
}, nil
}

// TODO(anuraaga): See if reflection can be avoided without complicating definition of transform functions.
Expand Down Expand Up @@ -157,7 +157,15 @@ func NewFunctionCall(inv Invocation, functions map[string]interface{}, pathParse
}
val := reflect.ValueOf(f)
ret := val.Call(args)
return ret[0].Interface().(ExprFunc), nil

var err error
if ret[1].IsNil() {
err = nil
} else {
err = ret[1].Interface().(error)
}

return ret[0].Interface().(ExprFunc), err
}
return nil, fmt.Errorf("undefined function %v", inv.Function)
}
20 changes: 19 additions & 1 deletion processor/transformprocessor/internal/common/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package common

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -132,11 +133,28 @@ func Test_newFunctionCall_invalid(t *testing.T) {
},
},
},
{
name: "function call returns error",
inv: Invocation{
Function: "testing_error",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := NewFunctionCall(tt.inv, DefaultFunctions(), testParsePath)

functions := DefaultFunctions()
functions["testing_error"] = functionThatHasAnError

_, err := NewFunctionCall(tt.inv, functions, testParsePath)
assert.Error(t, err)
})
}
}

func functionThatHasAnError() (ExprFunc, error) {
err := errors.New("testing")
return func(ctx TransformContext) interface{} {
return "anything"
}, err
}