Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 27 additions & 0 deletions .chloggen/ottl-has-prefix-suffix-dynamic.yaml
Original file line number Diff line number Diff line change
@@ -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. receiver/filelog)
component: pkg/ottl

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Added support for dynamic prefix/suffix in HasPrefix and HasSuffix functions in OTTL."

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [43555]

# (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: []
16 changes: 16 additions & 0 deletions pkg/ottl/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1350,13 +1350,27 @@ func Test_e2e_ottl_features(t *testing.T) {
tCtx.GetLogRecord().Attributes().PutStr("test", "pass")
},
},
{
name: "Using HasPrefix with dynamic prefix",
statement: `set(attributes["test"], "pass") where HasPrefix(body, attributes["dynamicprefix"])`,
want: func(tCtx ottllog.TransformContext) {
tCtx.GetLogRecord().Attributes().PutStr("test", "pass")
},
},
{
name: "Using HasSuffix",
statement: `set(attributes["test"], "pass") where HasSuffix(body, "tionA")`,
want: func(tCtx ottllog.TransformContext) {
tCtx.GetLogRecord().Attributes().PutStr("test", "pass")
},
},
{
name: "Using HasSuffix with dynamic suffix",
statement: `set(attributes["test"], "pass") where HasSuffix(body, attributes["dynamicsuffix"])`,
want: func(tCtx ottllog.TransformContext) {
tCtx.GetLogRecord().Attributes().PutStr("test", "pass")
},
},
{
name: "Using hex",
statement: `set(attributes["test"], "pass") where trace_id == TraceID(0x0102030405060708090a0b0c0d0e0f10)`,
Expand Down Expand Up @@ -1784,6 +1798,8 @@ func constructLogTransformContext() ottllog.TransformContext {
logRecord.SetTraceID(traceID)
logRecord.SetSpanID(spanID)
logRecord.Attributes().PutStr("http.method", "get")
logRecord.Attributes().PutStr("dynamicprefix", "operation")
logRecord.Attributes().PutStr("dynamicsuffix", "tionA")
logRecord.Attributes().PutStr("http.path", "/health")
logRecord.Attributes().PutStr("http.url", "http://localhost/health")
logRecord.Attributes().PutStr("flags", "A|B|C")
Expand Down
14 changes: 9 additions & 5 deletions pkg/ottl/ottlfuncs/func_has_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

type HasPrefixArguments[K any] struct {
Target ottl.StringGetter[K]
Prefix string
Prefix ottl.StringGetter[K]
}

func NewHasPrefixFactory[K any]() ottl.Factory[K] {
Expand All @@ -27,15 +27,19 @@ func createHasPrefixFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments
return nil, errors.New("HasPrefixFactory args must be of type *HasPrefixArguments[K]")
}

return HasPrefix(args.Target, args.Prefix)
return HasPrefix(args.Target, args.Prefix), nil
}

func HasPrefix[K any](target ottl.StringGetter[K], prefix string) (ottl.ExprFunc[K], error) {
func HasPrefix[K any](target, prefix ottl.StringGetter[K]) ottl.ExprFunc[K] {
return func(ctx context.Context, tCtx K) (any, error) {
val, err := target.Get(ctx, tCtx)
if err != nil {
return nil, err
}
return strings.HasPrefix(val, prefix), nil
}, nil
prefixVal, err := prefix.Get(ctx, tCtx)
if err != nil {
return nil, err
}
return strings.HasPrefix(val, prefixVal), nil
}
}
34 changes: 27 additions & 7 deletions pkg/ottl/ottlfuncs/func_has_prefix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@ func Test_HasPrefix(t *testing.T) {
tests := []struct {
name string
target any
prefix string
prefix ottl.StringGetter[any]
expected bool
}{
{
name: "has prefix true",
target: "hello world",
prefix: "hello ",
prefix: &ottl.StandardStringGetter[any]{Getter: func(context.Context, any) (any, error) { return "hello ", nil }},
expected: true,
},
{
name: "has prefix false",
target: "hello world",
prefix: " world",
prefix: &ottl.StandardStringGetter[any]{Getter: func(context.Context, any) (any, error) { return " world", nil }},
expected: false,
},
{
name: "target pcommon.Value",
target: pcommon.NewValueStr("hello world"),
prefix: `hello`,
prefix: &ottl.StandardStringGetter[any]{Getter: func(context.Context, any) (any, error) { return "hello", nil }},
expected: true,
},
}
Expand Down Expand Up @@ -67,8 +67,28 @@ func Test_HasPrefix_Error(t *testing.T) {
return true, nil
},
}
exprFunc, err := HasPrefix[any](target, "test")
assert.NoError(t, err)
_, err = exprFunc(t.Context(), nil)
prefix := &ottl.StandardStringGetter[any]{
Getter: func(context.Context, any) (any, error) {
return "test", nil
},
}
exprFunc := HasPrefix[any](target, prefix)
_, err := exprFunc(t.Context(), nil)
require.Error(t, err)
}

func Test_HasPrefix_Error_prefix(t *testing.T) {
target := &ottl.StandardStringGetter[any]{
Getter: func(context.Context, any) (any, error) {
return true, nil
},
}
prefix := &ottl.StandardStringGetter[any]{
Getter: func(context.Context, any) (any, error) {
return true, nil
},
}
exprFunc := HasPrefix[any](target, prefix)
_, err := exprFunc(t.Context(), nil)
require.Error(t, err)
}
14 changes: 9 additions & 5 deletions pkg/ottl/ottlfuncs/func_has_suffix.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

type HasSuffixArguments[K any] struct {
Target ottl.StringGetter[K]
Suffix string
Suffix ottl.StringGetter[K]
}

func NewHasSuffixFactory[K any]() ottl.Factory[K] {
Expand All @@ -27,15 +27,19 @@ func createHasSuffixFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments
return nil, errors.New("HasSuffixFactory args must be of type *HasSuffixArguments[K]")
}

return HasSuffix(args.Target, args.Suffix)
return HasSuffix(args.Target, args.Suffix), nil
}

func HasSuffix[K any](target ottl.StringGetter[K], suffix string) (ottl.ExprFunc[K], error) {
func HasSuffix[K any](target, suffix ottl.StringGetter[K]) ottl.ExprFunc[K] {
return func(ctx context.Context, tCtx K) (any, error) {
val, err := target.Get(ctx, tCtx)
if err != nil {
return nil, err
}
return strings.HasSuffix(val, suffix), nil
}, nil
suffixVal, err := suffix.Get(ctx, tCtx)
if err != nil {
return nil, err
}
return strings.HasSuffix(val, suffixVal), nil
}
}
34 changes: 27 additions & 7 deletions pkg/ottl/ottlfuncs/func_has_suffix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@ func Test_HasSuffix(t *testing.T) {
tests := []struct {
name string
target any
suffix string
suffix ottl.StringGetter[any]
expected bool
}{
{
name: "has suffix true",
target: "hello world",
suffix: " world",
suffix: &ottl.StandardStringGetter[any]{Getter: func(context.Context, any) (any, error) { return " world", nil }},
expected: true,
},
{
name: "has suffix false",
target: "hello world",
suffix: "hello ",
suffix: &ottl.StandardStringGetter[any]{Getter: func(context.Context, any) (any, error) { return "hello ", nil }},
expected: false,
},
{
name: "target pcommon.Value",
target: pcommon.NewValueStr("hello world"),
suffix: `world`,
suffix: &ottl.StandardStringGetter[any]{Getter: func(context.Context, any) (any, error) { return "world", nil }},
expected: true,
},
}
Expand Down Expand Up @@ -67,8 +67,28 @@ func Test_HasSuffix_Error(t *testing.T) {
return true, nil
},
}
exprFunc, err := HasSuffix[any](target, "test")
assert.NoError(t, err)
_, err = exprFunc(t.Context(), nil)
suffix := &ottl.StandardStringGetter[any]{
Getter: func(context.Context, any) (any, error) {
return "test", nil
},
}
exprFunc := HasSuffix[any](target, suffix)
_, err := exprFunc(t.Context(), nil)
require.Error(t, err)
}

func Test_HasSuffix_Error_suffix(t *testing.T) {
target := &ottl.StandardStringGetter[any]{
Getter: func(context.Context, any) (any, error) {
return true, nil
},
}
suffix := &ottl.StandardStringGetter[any]{
Getter: func(context.Context, any) (any, error) {
return true, nil
},
}
exprFunc := HasSuffix[any](target, suffix)
_, err := exprFunc(t.Context(), nil)
require.Error(t, err)
}