diff --git a/.chloggen/add-ottl-len-converter.yaml b/.chloggen/add-ottl-len-converter.yaml new file mode 100644 index 000000000000..3bf9fcec40cb --- /dev/null +++ b/.chloggen/add-ottl-len-converter.yaml @@ -0,0 +1,20 @@ +# Use this changelog template to create an entry for release notes. +# If your change doesn't affect end users, such as a test fix or a tooling change, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# 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: Add new `Len` converter that computes the length of strings, slices, and maps. + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [23847] + +# (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: \ No newline at end of file diff --git a/pkg/ottl/ottlfuncs/README.md b/pkg/ottl/ottlfuncs/README.md index 34514fe42fc6..6cdf1e2486a1 100644 --- a/pkg/ottl/ottlfuncs/README.md +++ b/pkg/ottl/ottlfuncs/README.md @@ -459,6 +459,20 @@ Examples: - `IsString(attributes["maybe a string"])` +### Len + +`Len(target)` + +The `Len` Converter returns the int64 length of the target string or slice. + +`target` is either a `string`, `slice`, `map`, `pcommon.Slice`, `pcommon.Map`, or `pcommon.Value` with type `pcommon.ValueTypeStr`, `pcommon.ValueTypeSlice`, or `pcommon.ValueTypeMap`. + +If the `target` is not an acceptable type, the `Len` Converter will return an error. + +Examples: + +- `Len(body)` + ### Log `Log(value)` diff --git a/pkg/ottl/ottlfuncs/func_len.go b/pkg/ottl/ottlfuncs/func_len.go new file mode 100644 index 000000000000..1766469035ff --- /dev/null +++ b/pkg/ottl/ottlfuncs/func_len.go @@ -0,0 +1,71 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package ottlfuncs // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs" + +import ( + "context" + "fmt" + "reflect" + + "go.opentelemetry.io/collector/pdata/pcommon" + + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" +) + +const ( + typeError = "target arg must be of type string, []any, map[string]any, pcommon.Map, pcommon.Slice, or pcommon.Value (of type String, Map, Slice)" +) + +type LenArguments[K any] struct { + Target ottl.Getter[K] `ottlarg:"0"` +} + +func NewLenFactory[K any]() ottl.Factory[K] { + return ottl.NewFactory("Len", &LenArguments[K]{}, createLenFunction[K]) +} + +func createLenFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) { + args, ok := oArgs.(*LenArguments[K]) + + if !ok { + return nil, fmt.Errorf("LenFactory args must be of type *LenArguments[K]") + } + + return computeLen(args.Target), nil +} + +// nolint:exhaustive +func computeLen[K any](target ottl.Getter[K]) ottl.ExprFunc[K] { + return func(ctx context.Context, tCtx K) (interface{}, error) { + val, err := target.Get(ctx, tCtx) + if err != nil { + return nil, err + } + + switch valType := val.(type) { + case pcommon.Value: + switch valType.Type() { + case pcommon.ValueTypeStr: + return int64(len(valType.Str())), nil + case pcommon.ValueTypeSlice: + return int64(valType.Slice().Len()), nil + case pcommon.ValueTypeMap: + return int64(valType.Map().Len()), nil + } + return nil, fmt.Errorf(typeError) + case pcommon.Map: + return int64(valType.Len()), nil + case pcommon.Slice: + return int64(valType.Len()), nil + } + + v := reflect.ValueOf(val) + switch v.Kind() { + case reflect.String, reflect.Map, reflect.Slice: + return int64(v.Len()), nil + } + + return nil, fmt.Errorf(typeError) + } +} diff --git a/pkg/ottl/ottlfuncs/func_len_test.go b/pkg/ottl/ottlfuncs/func_len_test.go new file mode 100644 index 000000000000..3b28a43cb70a --- /dev/null +++ b/pkg/ottl/ottlfuncs/func_len_test.go @@ -0,0 +1,127 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package ottlfuncs + +import ( + "context" + "strconv" + "testing" + + "github.com/stretchr/testify/assert" + "go.opentelemetry.io/collector/pdata/pcommon" + + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" +) + +func Test_Len(t *testing.T) { + pcommonSlice := pcommon.NewSlice() + err := pcommonSlice.FromRaw(make([]any, 5)) + if err != nil { + t.Error(err) + } + + pcommonMap := pcommon.NewMap() + err = pcommonMap.FromRaw(dummyMap(5)) + if err != nil { + t.Error(err) + } + + pcommonValueSlice := pcommon.NewValueSlice() + err = pcommonValueSlice.FromRaw(make([]any, 5)) + if err != nil { + t.Error(err) + } + + pcommonValueMap := pcommon.NewValueMap() + err = pcommonValueMap.FromRaw(dummyMap(5)) + if err != nil { + t.Error(err) + } + + tests := []struct { + name string + value interface{} + expected int64 + }{ + { + name: "string", + value: "a string", + expected: 8, + }, + { + name: "map", + value: dummyMap(5), + expected: 5, + }, + { + name: "string slice", + value: make([]string, 5), + expected: 5, + }, + { + name: "int slice", + value: make([]int, 5), + expected: 5, + }, + { + name: "pcommon map", + value: pcommonMap, + expected: 5, + }, + { + name: "pcommon slice", + value: pcommonSlice, + expected: 5, + }, + { + name: "pcommon value string", + value: pcommon.NewValueStr("a string"), + expected: 8, + }, + { + name: "pcommon value slice", + value: pcommonValueSlice, + expected: 5, + }, + { + name: "pcommon value map", + value: pcommonValueMap, + expected: 5, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + exprFunc := computeLen[any](&ottl.StandardGetSetter[any]{ + Getter: func(context context.Context, tCtx any) (interface{}, error) { + return tt.value, nil + }, + }) + result, err := exprFunc(context.Background(), nil) + assert.NoError(t, err) + assert.Equal(t, tt.expected, result) + }) + } +} + +func dummyMap(size int) map[string]any { + m := make(map[string]any, size) + for i := 0; i < size; i++ { + m[strconv.Itoa(i)] = i + } + return m +} + +// nolint:errorlint +func Test_Len_Error(t *testing.T) { + exprFunc := computeLen[any](&ottl.StandardGetSetter[any]{ + Getter: func(context.Context, interface{}) (interface{}, error) { + return 24, nil + }, + }) + result, err := exprFunc(context.Background(), nil) + assert.Nil(t, result) + assert.Error(t, err) + _, ok := err.(ottl.TypeError) + assert.False(t, ok) +} diff --git a/pkg/ottl/ottlfuncs/functions.go b/pkg/ottl/ottlfuncs/functions.go index e8499781ca16..e8d0b577e29d 100644 --- a/pkg/ottl/ottlfuncs/functions.go +++ b/pkg/ottl/ottlfuncs/functions.go @@ -42,6 +42,7 @@ func converters[K any]() []ottl.Factory[K] { NewIsMapFactory[K](), NewIsMatchFactory[K](), NewIsStringFactory[K](), + NewLenFactory[K](), NewLogFactory[K](), NewParseJSONFactory[K](), NewSHA1Factory[K](),