Skip to content

Commit 57f1ccb

Browse files
committed
[pkg/ottl] Add support for HasPrefix and HasSuffix
Signed-off-by: Bogdan Drutu <[email protected]>
1 parent 2ec3148 commit 57f1ccb

File tree

6 files changed

+259
-0
lines changed

6 files changed

+259
-0
lines changed

.chloggen/has-suffix-prefix.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: pkg/ottl
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Add support for HasPrefix and HasSuffix functions
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: []
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

pkg/ottl/ottlfuncs/func_has_prefix.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package ottlfuncs // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs"
5+
6+
import (
7+
"context"
8+
"errors"
9+
"strings"
10+
11+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
12+
)
13+
14+
type HasPrefixArguments[K any] struct {
15+
Target ottl.StringGetter[K]
16+
Prefix string
17+
}
18+
19+
func NewHasPrefixFactory[K any]() ottl.Factory[K] {
20+
return ottl.NewFactory("HasPrefix", &HasPrefixArguments[K]{}, createHasPrefixFunction[K])
21+
}
22+
23+
func createHasPrefixFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) {
24+
args, ok := oArgs.(*HasPrefixArguments[K])
25+
26+
if !ok {
27+
return nil, errors.New("HasPrefixFactory args must be of type *HasPrefixArguments[K]")
28+
}
29+
30+
return HasPrefix(args.Target, args.Prefix)
31+
}
32+
33+
func HasPrefix[K any](target ottl.StringGetter[K], prefix string) (ottl.ExprFunc[K], error) {
34+
return func(ctx context.Context, tCtx K) (any, error) {
35+
val, err := target.Get(ctx, tCtx)
36+
if err != nil {
37+
return nil, err
38+
}
39+
return strings.HasPrefix(val, prefix), nil
40+
}, nil
41+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package ottlfuncs
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
"go.opentelemetry.io/collector/pdata/pcommon"
13+
14+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
15+
)
16+
17+
func Test_HasPrefix(t *testing.T) {
18+
tests := []struct {
19+
name string
20+
target any
21+
prefix string
22+
expected bool
23+
}{
24+
{
25+
name: "has prefix true",
26+
target: "hello world",
27+
prefix: "hello ",
28+
expected: true,
29+
},
30+
{
31+
name: "has prefix false",
32+
target: "hello world",
33+
prefix: " world",
34+
expected: false,
35+
},
36+
{
37+
name: "target pcommon.Value",
38+
target: pcommon.NewValueStr("hello world"),
39+
prefix: `hello`,
40+
expected: true,
41+
},
42+
}
43+
for _, tt := range tests {
44+
t.Run(tt.name, func(t *testing.T) {
45+
factory := NewHasPrefixFactory[any]()
46+
exprFunc, err := factory.CreateFunction(
47+
ottl.FunctionContext{},
48+
&HasPrefixArguments[any]{
49+
Target: ottl.StandardStringGetter[any]{
50+
Getter: func(_ context.Context, _ any) (any, error) {
51+
return tt.target, nil
52+
},
53+
},
54+
Prefix: tt.prefix,
55+
})
56+
assert.NoError(t, err)
57+
result, err := exprFunc(context.Background(), nil)
58+
require.NoError(t, err)
59+
assert.Equal(t, tt.expected, result)
60+
})
61+
}
62+
}
63+
64+
func Test_HasPrefix_Error(t *testing.T) {
65+
target := &ottl.StandardStringGetter[any]{
66+
Getter: func(_ context.Context, _ any) (any, error) {
67+
return true, nil
68+
},
69+
}
70+
exprFunc, err := HasPrefix[any](target, "test")
71+
assert.NoError(t, err)
72+
_, err = exprFunc(context.Background(), nil)
73+
require.Error(t, err)
74+
}

pkg/ottl/ottlfuncs/func_has_suffix.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package ottlfuncs // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs"
5+
6+
import (
7+
"context"
8+
"errors"
9+
"strings"
10+
11+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
12+
)
13+
14+
type HasSuffixArguments[K any] struct {
15+
Target ottl.StringGetter[K]
16+
Suffix string
17+
}
18+
19+
func NewHasSuffixFactory[K any]() ottl.Factory[K] {
20+
return ottl.NewFactory("HasSuffix", &HasSuffixArguments[K]{}, createHasSuffixFunction[K])
21+
}
22+
23+
func createHasSuffixFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) {
24+
args, ok := oArgs.(*HasSuffixArguments[K])
25+
26+
if !ok {
27+
return nil, errors.New("HasSuffixFactory args must be of type *HasSuffixArguments[K]")
28+
}
29+
30+
return HasSuffix(args.Target, args.Suffix)
31+
}
32+
33+
func HasSuffix[K any](target ottl.StringGetter[K], suffix string) (ottl.ExprFunc[K], error) {
34+
return func(ctx context.Context, tCtx K) (any, error) {
35+
val, err := target.Get(ctx, tCtx)
36+
if err != nil {
37+
return nil, err
38+
}
39+
return strings.HasSuffix(val, suffix), nil
40+
}, nil
41+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package ottlfuncs
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
"go.opentelemetry.io/collector/pdata/pcommon"
13+
14+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
15+
)
16+
17+
func Test_HasSuffix(t *testing.T) {
18+
tests := []struct {
19+
name string
20+
target any
21+
suffix string
22+
expected bool
23+
}{
24+
{
25+
name: "has suffix true",
26+
target: "hello world",
27+
suffix: " world",
28+
expected: true,
29+
},
30+
{
31+
name: "has suffix false",
32+
target: "hello world",
33+
suffix: "hello ",
34+
expected: false,
35+
},
36+
{
37+
name: "target pcommon.Value",
38+
target: pcommon.NewValueStr("hello world"),
39+
suffix: `world`,
40+
expected: true,
41+
},
42+
}
43+
for _, tt := range tests {
44+
t.Run(tt.name, func(t *testing.T) {
45+
factory := NewHasSuffixFactory[any]()
46+
exprFunc, err := factory.CreateFunction(
47+
ottl.FunctionContext{},
48+
&HasSuffixArguments[any]{
49+
Target: ottl.StandardStringGetter[any]{
50+
Getter: func(_ context.Context, _ any) (any, error) {
51+
return tt.target, nil
52+
},
53+
},
54+
Suffix: tt.suffix,
55+
})
56+
assert.NoError(t, err)
57+
result, err := exprFunc(context.Background(), nil)
58+
require.NoError(t, err)
59+
assert.Equal(t, tt.expected, result)
60+
})
61+
}
62+
}
63+
64+
func Test_HasSuffix_Error(t *testing.T) {
65+
target := &ottl.StandardStringGetter[any]{
66+
Getter: func(_ context.Context, _ any) (any, error) {
67+
return true, nil
68+
},
69+
}
70+
exprFunc, err := HasSuffix[any](target, "test")
71+
assert.NoError(t, err)
72+
_, err = exprFunc(context.Background(), nil)
73+
require.Error(t, err)
74+
}

pkg/ottl/ottlfuncs/functions.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ func converters[K any]() []ottl.Factory[K] {
4949
NewExtractGrokPatternsFactory[K](),
5050
NewFnvFactory[K](),
5151
NewGetXMLFactory[K](),
52+
NewHasPrefixFactory[K](),
53+
NewHasSuffixFactory[K](),
5254
NewHourFactory[K](),
5355
NewHoursFactory[K](),
5456
NewInsertXMLFactory[K](),

0 commit comments

Comments
 (0)