-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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 new delete functions (#11824)
* Add new delete functions * Updated changelog
- Loading branch information
1 parent
2b1cf4c
commit 43da034
Showing
10 changed files
with
422 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
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.