Skip to content

Commit a3a9a25

Browse files
panys9Fiery-Fenix
authored andcommitted
[pkg/ottl] Replace key when using function (open-telemetry#38626)
#### Description When using the `replace_all_patterns` with mode set to `"key"` like `replace_all_patterns(cache, "key", "^(.*)$", "$$1", ToSnakeCase)`. This caused the value to be replaced instead of the key. By replacing the statement updating the value it now work as expected. At least this is how I interpret this function to work. #### Link to tracking issue open-telemetry#32896 #### Testing Added unit test for this scenario since it was missing. #### Documentation --------- Signed-off-by: Patrik Nyström <[email protected]>
1 parent 0d54452 commit a3a9a25

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: pkg/ottl
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: "Fix so replace_all_patterns can replace keys using optional function"
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [32896]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext: When using the `replace_all_patterns` with `key` and `optional` function on the replacement, the value was being replaced with the key. This change fixes that and now the key is replaced as intended.
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

pkg/ottl/ottlfuncs/func_replace_all_patterns.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ func replaceAllPatterns[K any](target ottl.PMapGetter[K], mode string, regexPatt
8383
case modeKey:
8484
if compiledPattern.MatchString(key) {
8585
if !fn.IsEmpty() {
86-
updatedString, err := applyOptReplaceFunction(ctx, tCtx, compiledPattern, fn, key, replacementVal, replacementFormat)
86+
updatedKey, err := applyOptReplaceFunction(ctx, tCtx, compiledPattern, fn, key, replacementVal, replacementFormat)
8787
if err != nil {
8888
break AttributeLoop
8989
}
90-
updated.PutStr(key, updatedString)
90+
originalValue.CopyTo(updated.PutEmpty(updatedKey))
9191
} else {
9292
updatedKey := compiledPattern.ReplaceAllString(key, replacementVal)
9393
originalValue.CopyTo(updated.PutEmpty(updatedKey))

pkg/ottl/ottlfuncs/func_replace_all_patterns_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,28 @@ func Test_replaceAllPatterns(t *testing.T) {
514514
expectedMap.PutStr("test7", "empty_string_replacement")
515515
},
516516
},
517+
{
518+
name: "replacement matches with function",
519+
target: target,
520+
mode: modeKey,
521+
pattern: `test(\d)`,
522+
replacement: ottl.StandardStringGetter[pcommon.Map]{
523+
Getter: func(context.Context, pcommon.Map) (any, error) {
524+
return "$1", nil
525+
},
526+
},
527+
replacementFormat: ottl.Optional[ottl.StringGetter[pcommon.Map]]{},
528+
function: optionalArg,
529+
want: func(expectedMap pcommon.Map) {
530+
expectedMap.PutStr("test", "hello world")
531+
expectedMap.PutStr("hash(2)", "hello")
532+
expectedMap.PutStr("hash(3)", "goodbye world1 and world2")
533+
expectedMap.PutInt("hash(4)", 1234)
534+
expectedMap.PutDouble("hash(5)", 1234)
535+
expectedMap.PutBool("hash(6)", true)
536+
expectedMap.PutStr("hash(7)", "")
537+
},
538+
},
517539
}
518540
for _, tt := range tests {
519541
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)