Skip to content

Commit

Permalink
[pkg/ottl] Allow capture groups in replace_all_patterns when replacin…
Browse files Browse the repository at this point in the history
…g keys (open-telemetry#22708)

Allow capture groups in replace_all_patterns when using the key mode

Co-authored-by: Evan Bradley <[email protected]>
  • Loading branch information
evan-bradley and evan-bradley authored May 23, 2023
1 parent 3c58b69 commit b6b8380
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
20 changes: 20 additions & 0 deletions .chloggen/ottl-issue-22094.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use this changelog template to create an entry for release notes.
# If your change doesn't affect end users, such as a test fix or a tooling change,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.

# 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: pkg/ottl

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Allow using capture groups in `replace_all_patterns` when replacing map keys

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [22094]

# (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:
2 changes: 1 addition & 1 deletion pkg/ottl/ottlfuncs/func_replace_all_patterns.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func replaceAllPatterns[K any](target ottl.PMapGetter[K], mode string, regexPatt
}
case modeKey:
if compiledPattern.MatchString(key) {
updatedKey := compiledPattern.ReplaceAllLiteralString(key, replacement)
updatedKey := compiledPattern.ReplaceAllString(key, replacement)
originalValue.CopyTo(updated.PutEmpty(updatedKey))
} else {
originalValue.CopyTo(updated.PutEmpty(key))
Expand Down
17 changes: 16 additions & 1 deletion pkg/ottl/ottlfuncs/func_replace_all_patterns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func Test_replaceAllPatterns(t *testing.T) {
},
},
{
name: "expand capturing groups",
name: "expand capturing groups in values",
target: target,
mode: modeValue,
pattern: `world(\d)`,
Expand All @@ -146,6 +146,21 @@ func Test_replaceAllPatterns(t *testing.T) {
expectedMap.PutBool("test6", true)
},
},
{
name: "expand capturing groups in keys",
target: target,
mode: modeKey,
pattern: `test(\d)`,
replacement: "test-$1",
want: func(expectedMap pcommon.Map) {
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)
},
},
{
name: "replacement with literal $",
target: target,
Expand Down

0 comments on commit b6b8380

Please sign in to comment.