Skip to content
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

Add ottl len converter #24420

Merged
merged 6 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
jack-berg marked this conversation as resolved.
Show resolved Hide resolved

# 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 length of the target string.

`target` is a `string`.

If the `target` is not a string or does not exist, the `Len` Converter will return an error.

Examples:

- `Len(body)`

### Log

`Log(value)`
Expand Down
39 changes: 39 additions & 0 deletions pkg/ottl/ottlfuncs/func_len.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
)

type LenArguments[K any] struct {
Target ottl.StringGetter[K] `ottlarg:"0"`
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
}

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 strLen(args.Target), nil
}

func strLen[K any](target ottl.StringGetter[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
}
return int64(len(val)), nil
}
}
59 changes: 59 additions & 0 deletions pkg/ottl/ottlfuncs/func_len_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package ottlfuncs

import (
"context"
"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) {
tests := []struct {
name string
value interface{}
expected int
}{
{
name: "string",
value: "a string",
expected: 8,
},
{
name: "ValueTypeString",
value: pcommon.NewValueStr("a string"),
expected: 8,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exprFunc := strLen[any](&ottl.StandardStringGetter[any]{
Getter: func(context.Context, interface{}) (interface{}, error) {
return tt.value, nil
},
})
result, err := exprFunc(context.Background(), nil)
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
})
}
}

// nolint:errorlint
func Test_Len_Error(t *testing.T) {
exprFunc := strLen[any](&ottl.StandardStringGetter[any]{
Getter: func(context.Context, interface{}) (interface{}, error) {
return nil, ottl.TypeError("")
},
})
result, err := exprFunc(context.Background(), nil)
assert.Equal(t, nil, result)
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
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