Skip to content

Commit

Permalink
[chore] fix: using replace_all_patterns on logs makes attributes valu…
Browse files Browse the repository at this point in the history
…es empty
  • Loading branch information
Ishmeet committed Apr 27, 2023
1 parent 3e0b003 commit ac7788d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
16 changes: 16 additions & 0 deletions .chloggen/fix-func-replace-all-patterns.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: transformprocessor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fixed a scenario where replace_all_patterns would wipe out non-string values on key name change.

# One or more tracking issues related to the change
issues: [21109]

# (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:
6 changes: 3 additions & 3 deletions pkg/ottl/ottlfuncs/func_replace_all_patterns.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ func ReplaceAllPatterns[K any](target ottl.PMapGetter[K], mode string, regexPatt
updatedString := compiledPattern.ReplaceAllString(originalValue.Str(), replacement)
updated.PutStr(key, updatedString)
} else {
updated.PutStr(key, originalValue.Str())
originalValue.CopyTo(updated.PutEmpty(key))
}
case modeKey:
if compiledPattern.MatchString(key) {
updatedKey := compiledPattern.ReplaceAllLiteralString(key, replacement)
updated.PutStr(updatedKey, originalValue.Str())
originalValue.CopyTo(updated.PutEmpty(updatedKey))
} else {
updated.PutStr(key, originalValue.Str())
originalValue.CopyTo(updated.PutEmpty(key))
}
}
return true
Expand Down
27 changes: 27 additions & 0 deletions pkg/ottl/ottlfuncs/func_replace_all_patterns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ func Test_replaceAllPatterns(t *testing.T) {
input.PutStr("test", "hello world")
input.PutStr("test2", "hello")
input.PutStr("test3", "goodbye world1 and world2")
input.PutInt("test4", 1234)
input.PutDouble("test5", 1234)
input.PutBool("test6", true)

target := &ottl.StandardTypeGetter[pcommon.Map, pcommon.Map]{
Getter: func(ctx context.Context, tCtx pcommon.Map) (interface{}, error) {
Expand All @@ -55,6 +58,9 @@ func Test_replaceAllPatterns(t *testing.T) {
expectedMap.PutStr("test", "hello {universe} world")
expectedMap.PutStr("test2", "hello {universe}")
expectedMap.PutStr("test3", "goodbye world1 and world2")
expectedMap.PutInt("test4", 1234)
expectedMap.PutDouble("test5", 1234)
expectedMap.PutBool("test6", true)
},
},
{
Expand All @@ -67,6 +73,9 @@ func Test_replaceAllPatterns(t *testing.T) {
expectedMap.PutStr("test", "hello world")
expectedMap.PutStr("test2", "hello")
expectedMap.PutStr("test3", "goodbye world1 and world2")
expectedMap.PutInt("test4", 1234)
expectedMap.PutDouble("test5", 1234)
expectedMap.PutBool("test6", true)
},
},
{
Expand All @@ -79,6 +88,9 @@ func Test_replaceAllPatterns(t *testing.T) {
expectedMap.PutStr("test", "hello **** ")
expectedMap.PutStr("test2", "hello")
expectedMap.PutStr("test3", "goodbye **** and **** ")
expectedMap.PutInt("test4", 1234)
expectedMap.PutDouble("test5", 1234)
expectedMap.PutBool("test6", true)
},
},
{
Expand All @@ -92,6 +104,9 @@ func Test_replaceAllPatterns(t *testing.T) {
expectedMap.PutStr("test", "hello world")
expectedMap.PutStr("foo", "hello")
expectedMap.PutStr("test3", "goodbye world1 and world2")
expectedMap.PutInt("test4", 1234)
expectedMap.PutDouble("test5", 1234)
expectedMap.PutBool("test6", true)
},
},
{
Expand All @@ -105,6 +120,9 @@ func Test_replaceAllPatterns(t *testing.T) {
expectedMap.PutStr("test", "hello world")
expectedMap.PutStr("test2", "hello")
expectedMap.PutStr("test3", "goodbye world1 and world2")
expectedMap.PutInt("test4", 1234)
expectedMap.PutDouble("test5", 1234)
expectedMap.PutBool("test6", true)
},
},
{
Expand All @@ -118,6 +136,9 @@ func Test_replaceAllPatterns(t *testing.T) {
expectedMap.PutStr("test.", "hello world")
expectedMap.PutStr("test.2", "hello")
expectedMap.PutStr("test.3", "goodbye world1 and world2")
expectedMap.PutInt("test.4", 1234)
expectedMap.PutDouble("test.5", 1234)
expectedMap.PutBool("test.6", true)
},
},
{
Expand All @@ -131,6 +152,9 @@ func Test_replaceAllPatterns(t *testing.T) {
expectedMap.PutStr("test", "hello world")
expectedMap.PutStr("test2", "hello")
expectedMap.PutStr("test3", "goodbye world-1 and world-2")
expectedMap.PutInt("test4", 1234)
expectedMap.PutDouble("test5", 1234)
expectedMap.PutBool("test6", true)
},
},
{
Expand All @@ -144,6 +168,9 @@ func Test_replaceAllPatterns(t *testing.T) {
expectedMap.PutStr("test", "hello world")
expectedMap.PutStr("test2", "hello")
expectedMap.PutStr("test3", "goodbye $world-1 and $world-2")
expectedMap.PutInt("test4", 1234)
expectedMap.PutDouble("test5", 1234)
expectedMap.PutBool("test6", true)
},
},
}
Expand Down

0 comments on commit ac7788d

Please sign in to comment.