diff --git a/.chloggen/lkwronski.issue-27894-is-int-converter.yaml b/.chloggen/lkwronski.issue-27894-is-int-converter.yaml new file mode 100644 index 000000000000..5d88fc50f6eb --- /dev/null +++ b/.chloggen/lkwronski.issue-27894-is-int-converter.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# 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 IsInt function to facilitate type checking. + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [27894] + +# (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: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [user] diff --git a/pkg/ottl/ottlfuncs/README.md b/pkg/ottl/ottlfuncs/README.md index 38f81cd748e0..9f4728c8b5c1 100644 --- a/pkg/ottl/ottlfuncs/README.md +++ b/pkg/ottl/ottlfuncs/README.md @@ -388,6 +388,7 @@ Available Converters: - [Int](#int) - [IsBool](#isbool) - [IsDouble](#isdouble) +- [IsInt](#isint) - [IsMap](#ismap) - [IsMatch](#ismatch) - [IsString](#isstring) @@ -629,6 +630,22 @@ Examples: - `IsDouble(attributes["maybe a double"])` +### IsInt + +`IsInt(value)` + +The `IsInt` Converter returns true if the given value is a int. + +The `value` is either a path expression to a telemetry field to retrieve, or a literal. + +If `value` is a `int64` or a `pcommon.ValueTypeInt` then returns `true`, otherwise returns `false`. + +Examples: + +- `IsInt(body)` + +- `IsInt(attributes["maybe a int"])` + ### IsMap `IsMap(value)` diff --git a/pkg/ottl/ottlfuncs/func_is_int.go b/pkg/ottl/ottlfuncs/func_is_int.go new file mode 100644 index 000000000000..9b392012243e --- /dev/null +++ b/pkg/ottl/ottlfuncs/func_is_int.go @@ -0,0 +1,45 @@ +// 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 IsIntArguments[K any] struct { + Target ottl.IntGetter[K] +} + +func NewIsIntFactory[K any]() ottl.Factory[K] { + return ottl.NewFactory("IsInt", &IsIntArguments[K]{}, createIsIntFunction[K]) +} + +func createIsIntFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) { + args, ok := oArgs.(*IsIntArguments[K]) + + if !ok { + return nil, fmt.Errorf("IsIntFactory args must be of type *IsIntArguments[K]") + } + + return isInt(args.Target), nil +} + +// nolint:errorlint +func isInt[K any](target ottl.IntGetter[K]) ottl.ExprFunc[K] { + return func(ctx context.Context, tCtx K) (any, error) { + _, err := target.Get(ctx, tCtx) + // Use type assertion because we don't want to check wrapped errors + switch err.(type) { + case ottl.TypeError: + return false, nil + case nil: + return true, nil + default: + return false, err + } + } +} diff --git a/pkg/ottl/ottlfuncs/func_is_int_test.go b/pkg/ottl/ottlfuncs/func_is_int_test.go new file mode 100644 index 000000000000..18566236e8dc --- /dev/null +++ b/pkg/ottl/ottlfuncs/func_is_int_test.go @@ -0,0 +1,89 @@ +// 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_IsInt(t *testing.T) { + tests := []struct { + name string + value any + expected bool + }{ + { + name: "int", + value: int64(0), + expected: true, + }, + { + name: "ValueTypeInt", + value: pcommon.NewValueInt(0), + expected: true, + }, + { + name: "float64", + value: float64(2.7), + expected: false, + }, + { + name: "ValueTypeString", + value: pcommon.NewValueStr("a string"), + expected: false, + }, + { + name: "not Int", + value: "string", + expected: false, + }, + { + name: "string number", + value: "0", + expected: false, + }, + { + name: "ValueTypeSlice", + value: pcommon.NewValueSlice(), + expected: false, + }, + { + name: "nil", + value: nil, + expected: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + exprFunc := isInt[any](&ottl.StandardIntGetter[any]{ + Getter: func(context.Context, any) (any, 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_IsInt_Error(t *testing.T) { + exprFunc := isInt[any](&ottl.StandardIntGetter[any]{ + Getter: func(context.Context, any) (any, error) { + return nil, ottl.TypeError("") + }, + }) + result, err := exprFunc(context.Background(), nil) + assert.Equal(t, false, 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 786e270bae36..a12ba9d147ba 100644 --- a/pkg/ottl/ottlfuncs/functions.go +++ b/pkg/ottl/ottlfuncs/functions.go @@ -46,6 +46,7 @@ func converters[K any]() []ottl.Factory[K] { NewIntFactory[K](), NewIsBoolFactory[K](), NewIsDoubleFactory[K](), + NewIsIntFactory[K](), NewIsMapFactory[K](), NewIsMatchFactory[K](), NewIsStringFactory[K](),