forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[processor/transform] Add IsMatch function (open-telemetry#11123)
* added IsMatch function
- Loading branch information
1 parent
5fcf4ba
commit 0676d43
Showing
8 changed files
with
163 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
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
35 changes: 35 additions & 0 deletions
35
processor/transformprocessor/internal/common/func_is_match.go
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,35 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package common // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/transformprocessor/internal/common" | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
func isMatch(target Getter, pattern string) (ExprFunc, error) { | ||
regexp, err := regexp.Compile(pattern) | ||
if err != nil { | ||
return nil, fmt.Errorf("the pattern supplied to IsMatch is not a valid regexp pattern: %w", err) | ||
} | ||
return func(ctx TransformContext) interface{} { | ||
if val := target.Get(ctx); val != nil { | ||
if valStr, ok := val.(string); ok { | ||
return regexp.MatchString(valStr) | ||
} | ||
} | ||
return false | ||
}, nil | ||
} |
103 changes: 103 additions & 0 deletions
103
processor/transformprocessor/internal/common/func_is_match_test.go
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,103 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package common | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/transformprocessor/internal/common/testhelper" | ||
) | ||
|
||
func Test_isMatch(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
target Getter | ||
pattern string | ||
expected bool | ||
}{ | ||
{ | ||
name: "replace match true", | ||
target: &testGetSetter{ | ||
getter: func(ctx TransformContext) interface{} { | ||
return "hello world" | ||
}, | ||
}, | ||
pattern: "hello.*", | ||
expected: true, | ||
}, | ||
{ | ||
name: "replace match false", | ||
target: &testGetSetter{ | ||
getter: func(ctx TransformContext) interface{} { | ||
return "goodbye world" | ||
}, | ||
}, | ||
pattern: "hello.*", | ||
expected: false, | ||
}, | ||
{ | ||
name: "replace match complex", | ||
target: &testGetSetter{ | ||
getter: func(ctx TransformContext) interface{} { | ||
return "-12.001" | ||
}, | ||
}, | ||
pattern: "[-+]?\\d*\\.\\d+([eE][-+]?\\d+)?", | ||
expected: true, | ||
}, | ||
{ | ||
name: "target not a string", | ||
target: &testGetSetter{ | ||
getter: func(ctx TransformContext) interface{} { | ||
return 1 | ||
}, | ||
}, | ||
pattern: "doesnt matter will be false", | ||
expected: false, | ||
}, | ||
{ | ||
name: "target nil", | ||
target: &testGetSetter{ | ||
getter: func(ctx TransformContext) interface{} { | ||
return nil | ||
}, | ||
}, | ||
pattern: "doesnt matter will be false", | ||
expected: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ctx := testhelper.TestTransformContext{} | ||
|
||
exprFunc, _ := isMatch(tt.target, tt.pattern) | ||
actual := exprFunc(ctx) | ||
|
||
assert.Equal(t, tt.expected, actual) | ||
}) | ||
} | ||
} | ||
|
||
func Test_isMatch_validation(t *testing.T) { | ||
target := &testGetSetter{ | ||
getter: func(ctx TransformContext) interface{} { | ||
return "anything" | ||
}, | ||
} | ||
_, err := isMatch(target, "\\K") | ||
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
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
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