-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
[processor/transform] Add new delete functions #11824
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
31 changes: 31 additions & 0 deletions
31
processor/transformprocessor/internal/common/func_delete_key.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,31 @@ | ||
// 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 "go.opentelemetry.io/collector/pdata/pcommon" | ||
|
||
func deleteKey(target Getter, key string) (ExprFunc, error) { | ||
return func(ctx TransformContext) interface{} { | ||
val := target.Get(ctx) | ||
if val == nil { | ||
return nil | ||
} | ||
|
||
if attrs, ok := val.(pcommon.Map); ok { | ||
attrs.Remove(key) | ||
} | ||
return nil | ||
}, nil | ||
} |
137 changes: 137 additions & 0 deletions
137
processor/transformprocessor/internal/common/func_delete_key_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,137 @@ | ||
// 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" | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/transformprocessor/internal/common/testhelper" | ||
) | ||
|
||
func Test_deleteKey(t *testing.T) { | ||
input := pcommon.NewMap() | ||
input.InsertString("test", "hello world") | ||
input.InsertInt("test2", 3) | ||
input.InsertBool("test3", true) | ||
|
||
target := &testGetSetter{ | ||
getter: func(ctx TransformContext) interface{} { | ||
return ctx.GetItem() | ||
}, | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
target Getter | ||
key string | ||
want func(pcommon.Map) | ||
}{ | ||
{ | ||
name: "delete test", | ||
target: target, | ||
key: "test", | ||
want: func(expectedMap pcommon.Map) { | ||
expectedMap.Clear() | ||
expectedMap.InsertBool("test3", true) | ||
expectedMap.InsertInt("test2", 3) | ||
}, | ||
}, | ||
{ | ||
name: "delete test2", | ||
target: target, | ||
key: "test2", | ||
want: func(expectedMap pcommon.Map) { | ||
expectedMap.Clear() | ||
expectedMap.InsertString("test", "hello world") | ||
expectedMap.InsertBool("test3", true) | ||
}, | ||
}, | ||
{ | ||
name: "delete nothing", | ||
target: target, | ||
key: "not a valid key", | ||
want: func(expectedMap pcommon.Map) { | ||
expectedMap.Clear() | ||
expectedMap.InsertString("test", "hello world") | ||
expectedMap.InsertInt("test2", 3) | ||
expectedMap.InsertBool("test3", true) | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
scenarioMap := pcommon.NewMap() | ||
input.CopyTo(scenarioMap) | ||
|
||
ctx := testhelper.TestTransformContext{ | ||
Item: scenarioMap, | ||
} | ||
|
||
exprFunc, _ := deleteKey(tt.target, tt.key) | ||
exprFunc(ctx) | ||
|
||
expected := pcommon.NewMap() | ||
tt.want(expected) | ||
|
||
assert.Equal(t, expected, scenarioMap) | ||
}) | ||
} | ||
} | ||
|
||
func Test_deleteKey_bad_input(t *testing.T) { | ||
input := pcommon.NewValueString("not a map") | ||
ctx := testhelper.TestTransformContext{ | ||
Item: input, | ||
} | ||
|
||
target := &testGetSetter{ | ||
getter: func(ctx TransformContext) interface{} { | ||
return ctx.GetItem() | ||
}, | ||
setter: func(ctx TransformContext, val interface{}) { | ||
t.Errorf("nothing should be set in this scenario") | ||
}, | ||
} | ||
|
||
key := "anything" | ||
|
||
exprFunc, _ := deleteKey(target, key) | ||
exprFunc(ctx) | ||
|
||
assert.Equal(t, pcommon.NewValueString("not a map"), input) | ||
} | ||
|
||
func Test_deleteKey_get_nil(t *testing.T) { | ||
ctx := testhelper.TestTransformContext{ | ||
Item: nil, | ||
} | ||
|
||
target := &testGetSetter{ | ||
getter: func(ctx TransformContext) interface{} { | ||
return ctx.GetItem() | ||
}, | ||
setter: func(ctx TransformContext, val interface{}) { | ||
t.Errorf("nothing should be set in this scenario") | ||
}, | ||
} | ||
|
||
key := "anything" | ||
|
||
exprFunc, _ := deleteKey(target, key) | ||
exprFunc(ctx) | ||
} |
42 changes: 42 additions & 0 deletions
42
processor/transformprocessor/internal/common/func_delete_matching_keys.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,42 @@ | ||
// 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" | ||
|
||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
) | ||
|
||
func deleteMatchingKeys(target Getter, pattern string) (ExprFunc, error) { | ||
compiledPattern, err := regexp.Compile(pattern) | ||
if err != nil { | ||
return nil, fmt.Errorf("the regex pattern supplied to delete_matching_keys is not a valid pattern: %w", err) | ||
} | ||
return func(ctx TransformContext) interface{} { | ||
val := target.Get(ctx) | ||
if val == nil { | ||
return nil | ||
} | ||
|
||
if attrs, ok := val.(pcommon.Map); ok { | ||
attrs.RemoveIf(func(key string, _ pcommon.Value) bool { | ||
return compiledPattern.MatchString(key) | ||
}) | ||
} | ||
return nil | ||
}, nil | ||
} |
140 changes: 140 additions & 0 deletions
140
processor/transformprocessor/internal/common/func_delete_matching_keys_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,140 @@ | ||
// 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" | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/transformprocessor/internal/common/testhelper" | ||
) | ||
|
||
func Test_deleteMatchingKeys(t *testing.T) { | ||
input := pcommon.NewMap() | ||
input.InsertString("test", "hello world") | ||
input.InsertInt("test2", 3) | ||
input.InsertBool("test3", true) | ||
|
||
target := &testGetSetter{ | ||
getter: func(ctx TransformContext) interface{} { | ||
return ctx.GetItem() | ||
}, | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
target Getter | ||
pattern string | ||
want func(pcommon.Map) | ||
}{ | ||
{ | ||
name: "delete everything", | ||
target: target, | ||
pattern: "test.*", | ||
want: func(expectedMap pcommon.Map) { | ||
expectedMap.Clear() | ||
expectedMap.EnsureCapacity(3) | ||
}, | ||
}, | ||
{ | ||
name: "delete attributes that end in a number", | ||
target: target, | ||
pattern: "\\d$", | ||
want: func(expectedMap pcommon.Map) { | ||
expectedMap.Clear() | ||
expectedMap.InsertString("test", "hello world") | ||
}, | ||
}, | ||
{ | ||
name: "delete nothing", | ||
target: target, | ||
pattern: "not a matching pattern", | ||
want: func(expectedMap pcommon.Map) { | ||
expectedMap.Clear() | ||
expectedMap.InsertString("test", "hello world") | ||
expectedMap.InsertInt("test2", 3) | ||
expectedMap.InsertBool("test3", true) | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
scenarioMap := pcommon.NewMap() | ||
input.CopyTo(scenarioMap) | ||
|
||
ctx := testhelper.TestTransformContext{ | ||
Item: scenarioMap, | ||
} | ||
|
||
exprFunc, _ := deleteMatchingKeys(tt.target, tt.pattern) | ||
exprFunc(ctx) | ||
|
||
expected := pcommon.NewMap() | ||
tt.want(expected) | ||
|
||
assert.Equal(t, expected, scenarioMap) | ||
}) | ||
} | ||
} | ||
|
||
func Test_deleteMatchingKeys_bad_input(t *testing.T) { | ||
input := pcommon.NewValueInt(1) | ||
ctx := testhelper.TestTransformContext{ | ||
Item: input, | ||
} | ||
|
||
target := &testGetSetter{ | ||
getter: func(ctx TransformContext) interface{} { | ||
return ctx.GetItem() | ||
}, | ||
} | ||
|
||
exprFunc, err := deleteMatchingKeys(target, "anything") | ||
assert.Nil(t, err) | ||
exprFunc(ctx) | ||
|
||
assert.Equal(t, pcommon.NewValueInt(1), input) | ||
} | ||
|
||
func Test_deleteMatchingKeys_get_nil(t *testing.T) { | ||
ctx := testhelper.TestTransformContext{ | ||
Item: nil, | ||
} | ||
|
||
target := &testGetSetter{ | ||
getter: func(ctx TransformContext) interface{} { | ||
return ctx.GetItem() | ||
}, | ||
} | ||
|
||
exprFunc, _ := deleteMatchingKeys(target, "anything") | ||
exprFunc(ctx) | ||
} | ||
|
||
func Test_deleteMatchingKeys_invalid_pattern(t *testing.T) { | ||
target := &testGetSetter{ | ||
getter: func(ctx TransformContext) interface{} { | ||
t.Errorf("nothing should be received in this scenario") | ||
return nil | ||
}, | ||
} | ||
|
||
invalidRegexPattern := "*" | ||
exprFunc, err := deleteMatchingKeys(target, invalidRegexPattern) | ||
assert.Nil(t, exprFunc) | ||
assert.Contains(t, err.Error(), "error parsing regexp:") | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be
delete_all_matches
to be consistent withreplace_all_matches
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about that but I didn't want anyone to think it deleted the matching sections in the map's value vs replacing it like
replace_all_matches
. I wanted it to be clear that its the whole entry being deleted, so I kept it closer todelete_key