Skip to content

Commit

Permalink
Add ottl len converter (#24420)
Browse files Browse the repository at this point in the history
**Description:** 

Add ottl `Len` converter to make `Substring` useful for truncating.
E.g.:
```
    log_statements:
      - context: log
        statements:
        - set(body, Substring(body, 0, 10)) where Len(body) >= 10
```

**Link to tracking Issue:**  Resolves #23847.

**Testing:** Unit tests, local verification with running collector.

**Documentation:** Added documentation to
`pkg/ottl/ottlfuncs/README.md`.

This PR is the result of
[conversation](#23880 (comment))
on #23880.

---------

Co-authored-by: Tyler Helmuth <[email protected]>
Co-authored-by: Evan Bradley <[email protected]>
  • Loading branch information
3 people authored Jul 27, 2023
1 parent 915f963 commit 3e1037a
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .chloggen/add-ottl-len-converter.yaml
Original file line number Diff line number Diff line change
@@ -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:
14 changes: 14 additions & 0 deletions pkg/ottl/ottlfuncs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`
Expand Down
71 changes: 71 additions & 0 deletions pkg/ottl/ottlfuncs/func_len.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
127 changes: 127 additions & 0 deletions pkg/ottl/ottlfuncs/func_len_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions pkg/ottl/ottlfuncs/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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](),
Expand Down

0 comments on commit 3e1037a

Please sign in to comment.