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

[pkg/ottl] Update IsMatch to convert bool int and float to strings #16503

Merged
merged 8 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
16 changes: 16 additions & 0 deletions .chloggen/ottl-improve-ismatch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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: Updates the IsMatch function to convert bools, ints, and floats to strings before matching.

# One or more tracking issues related to the change
issues: [16503]

# (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:
4 changes: 3 additions & 1 deletion pkg/ottl/ottlfuncs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ The `IsMatch` factory function returns true if the `target` matches the regex `p

`target` is either a path expression to a telemetry field to retrieve or a literal string. `pattern` is a regexp pattern.

The function matches the target against the pattern, returning true if the match is successful and false otherwise. If target is nil or not a string false is always returned.
The function matches the target against the pattern, returning true if the match is successful and false otherwise.
If target is a boolean, int, or float it will be converted to a string.
If target is nil or not a string, boolean, int, or float false is always returned.

Examples:

Expand Down
19 changes: 15 additions & 4 deletions pkg/ottl/ottlfuncs/func_is_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ package ottlfuncs // import "github.com/open-telemetry/opentelemetry-collector-c
import (
"context"
"fmt"
"go.opentelemetry.io/collector/pdata/pcommon"
"regexp"
"strconv"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
)
Expand All @@ -32,11 +34,20 @@ func IsMatch[K any](target ottl.Getter[K], pattern string) (ottl.ExprFunc[K], er
if err != nil {
return nil, err
}
if val != nil {
if valStr, ok := val.(string); ok {
return compiledPattern.MatchString(valStr), nil
}

switch v := val.(type) {
bogdandrutu marked this conversation as resolved.
Show resolved Hide resolved
case string:
return compiledPattern.MatchString(v), nil
case bool:
return compiledPattern.MatchString(strconv.FormatBool(v)), nil
case int64:
return compiledPattern.MatchString(strconv.FormatInt(v, 10)), nil
default:
bogdandrutu marked this conversation as resolved.
Show resolved Hide resolved
pv := pcommon.NewValueEmpty()
pv.FromRaw(v)
return compiledPattern.MatchString(pv.AsString()), nil
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
}

return false, nil
}, nil
}
36 changes: 33 additions & 3 deletions pkg/ottl/ottlfuncs/func_is_match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,43 @@ func Test_isMatch(t *testing.T) {
expected: true,
},
{
name: "target not a string",
name: "target bool",
target: &ottl.StandardGetSetter[interface{}]{
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) {
return 1, nil
return true, nil
},
},
pattern: "doesnt matter will be false",
pattern: "true",
expected: true,
},
{
name: "target int",
target: &ottl.StandardGetSetter[interface{}]{
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) {
return int64(1), nil
},
},
pattern: `\d`,
expected: true,
},
{
name: "target float",
target: &ottl.StandardGetSetter[interface{}]{
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) {
return 1.1, nil
},
},
pattern: `\d\.\d`,
expected: true,
},
{
name: "target invalid type",
target: &ottl.StandardGetSetter[interface{}]{
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) {
return []string{"invalid"}, nil
},
},
pattern: `\[invalid\]`,
expected: false,
},
{
Expand Down