-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
**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
1 parent
915f963
commit 3e1037a
Showing
5 changed files
with
233 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters