-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add IsMatchVersion convertor to pkg/ottl.
- Loading branch information
Showing
8 changed files
with
273 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,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: Introduce IsMatchVersion converter | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [] | ||
|
||
# (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] |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,54 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package ottlfuncs // import "github.com/canva/otel-platform/opentelemetry-collector/pkg/ottl/ottlfuncs" | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/Masterminds/semver/v3" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
) | ||
|
||
type IsMatchVersionArguments[K any] struct { | ||
Target ottl.StringGetter[K] | ||
Constraint string | ||
} | ||
|
||
func NewIsMatchVersionFactory[K any]() ottl.Factory[K] { | ||
return ottl.NewFactory("IsMatchVersion", &IsMatchVersionArguments[K]{}, createIsMatchVersionFunction[K]) | ||
} | ||
|
||
func createIsMatchVersionFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) { | ||
args, ok := oArgs.(*IsMatchVersionArguments[K]) | ||
|
||
if !ok { | ||
return nil, fmt.Errorf("IsMatchVersionFactory args must be of type *IsMatchVersionArguments[K]") | ||
} | ||
|
||
return isMatchVersion(args.Target, args.Constraint) | ||
} | ||
|
||
func isMatchVersion[K any](target ottl.StringGetter[K], constraint string) (ottl.ExprFunc[K], error) { | ||
semverconstraint, err := semver.NewConstraint(constraint) | ||
if err != nil { | ||
return nil, fmt.Errorf("the constrain supplied to IsMatchVersion is not a valid: %w", err) | ||
} | ||
return func(ctx context.Context, tCtx K) (any, error) { | ||
val, err := target.Get(ctx, tCtx) | ||
|
||
if err != nil { | ||
return false, err | ||
} | ||
|
||
version, err := semver.NewVersion(val) | ||
|
||
if err != nil { | ||
return false, fmt.Errorf("failed to parse semver version from: %s, err: %w", val, err) | ||
} | ||
|
||
return semverconstraint.Check(version), nil | ||
}, nil | ||
} |
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,98 @@ | ||
// 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_isMatchVersion(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
value any | ||
constraint string | ||
expected bool | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "version match", | ||
value: "1.2.3", | ||
constraint: "1.2.x", | ||
expected: true, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "version doesn't match", | ||
value: "1.3.3", | ||
constraint: "1.2.x", | ||
expected: false, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "version pcommon.ValueTypeStr", | ||
value: pcommon.NewValueStr("1.2.3"), | ||
constraint: "1.2.x", | ||
expected: true, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "version pcommon.ValueTypeInt", | ||
value: pcommon.NewValueInt(123), | ||
constraint: "1.2.x", | ||
expected: false, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "not valid version string type", | ||
value: "abc.2.3", | ||
constraint: "1.2.x", | ||
expected: false, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "nil value", | ||
value: nil, | ||
constraint: "1.2.x", | ||
expected: false, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "not valid version pcommon.ValueTypeStr", | ||
value: pcommon.NewValueStr("abc.2.3"), | ||
constraint: "1.2.x", | ||
expected: false, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
exprFunc, err := isMatchVersion[any](&ottl.StandardStringGetter[any]{ | ||
Getter: func(_ context.Context, _ any) (any, error) { | ||
return tt.value, nil | ||
}, | ||
}, tt.constraint) | ||
assert.NoError(t, err) | ||
result, err := exprFunc(context.Background(), nil) | ||
|
||
assert.True(t, (err != nil) == tt.wantErr, "Expected errors: %t received error: %t, err: %w", tt.wantErr, err != nil, err) | ||
assert.Equal(t, tt.expected, result) | ||
}) | ||
} | ||
} | ||
|
||
func Test_isMatchVersion_invalid_constrain(t *testing.T) { | ||
target := &ottl.StandardStringGetter[any]{ | ||
Getter: func(_ context.Context, _ any) (any, error) { | ||
return "1.2.3", nil | ||
}, | ||
} | ||
_, err := isMatchVersion[any](target, "abc.1.2") | ||
assert.Error(t, err) | ||
} |
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