Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .chloggen/pkg-ottl-add-parse-key-value-function.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# 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: Add `ParseKeyValue` function for parsing key value pairs from a target string

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

# (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:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
4 changes: 4 additions & 0 deletions internal/coreinternal/parseutils/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package parseutils // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/parseutils"
14 changes: 14 additions & 0 deletions internal/coreinternal/parseutils/package_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package parseutils

import (
"testing"

"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}
49 changes: 49 additions & 0 deletions internal/coreinternal/parseutils/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package parseutils

import "fmt"

// SplitString will split the input on the delimiter and return the resulting slice while respecting quotes. Outer quotes are stripped.
// Use in place of `strings.Split` when quotes need to be respected.
// Requires `delimiter` not be an empty string
func SplitString(input, delimiter string) ([]string, error) {
var result []string
current := ""
delimiterLength := len(delimiter)
quoteChar := "" // "" means we are not in quotes

for i := 0; i < len(input); i++ {
if quoteChar == "" && i+delimiterLength <= len(input) && input[i:i+delimiterLength] == delimiter { // delimiter
if current == "" { // leading || trailing delimiter; ignore
i += delimiterLength - 1
continue
}
result = append(result, current)
current = ""
i += delimiterLength - 1
continue
}

if quoteChar == "" && (input[i] == '"' || input[i] == '\'') { // start of quote
quoteChar = string(input[i])
continue
}
if string(input[i]) == quoteChar { // end of quote
quoteChar = ""
continue
}

current += string(input[i])
}

if quoteChar != "" { // check for closed quotes
return nil, fmt.Errorf("never reached the end of a quoted value")
}
if current != "" { // avoid adding empty value bc of a trailing delimiter
return append(result, current), nil
}

return result, nil
}
186 changes: 186 additions & 0 deletions internal/coreinternal/parseutils/parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package parseutils

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_SplitString(t *testing.T) {
testCases := []struct {
name string
input string
delimiter string
expected []string
expectedErr error
}{
{
name: "simple",
input: "a b c",
delimiter: " ",
expected: []string{
"a",
"b",
"c",
},
},
{
name: "single quotes",
input: "a 'b c d'",
delimiter: " ",
expected: []string{
"a",
"b c d",
},
},
{
name: "double quotes",
input: `a " b c " d`,
delimiter: " ",
expected: []string{
"a",
" b c ",
"d",
},
},
{
name: "multi-char delimiter",
input: "abc!@! def !@! g",
delimiter: "!@!",
expected: []string{
"abc",
" def ",
" g",
},
},
{
name: "leading and trailing delimiters",
input: " name=ottl func=key_value hello=world ",
delimiter: " ",
expected: []string{
"name=ottl",
"func=key_value",
"hello=world",
},
},
{
name: "embedded double quotes in single quoted value",
input: `ab c='this is a "co ol" value'`,
delimiter: " ",
expected: []string{
"ab",
`c=this is a "co ol" value`,
},
},
{
name: "embedded double quotes end single quoted value",
input: `ab c='this is a "co ol"'`,
delimiter: " ",
expected: []string{
"ab",
`c=this is a "co ol"`,
},
},
{
name: "quoted values include whitespace",
input: `name=" ottl " func=" key_ value"`,
delimiter: " ",
expected: []string{
"name= ottl ",
"func= key_ value",
},
},
{
name: "delimiter longer than input",
input: "abc",
delimiter: "aaaa",
expected: []string{
"abc",
},
},
{
name: "delimiter not found",
input: "a b c",
delimiter: "!",
expected: []string{
"a b c",
},
},
{
name: "newlines in input",
input: `a
b
c`,
delimiter: " ",
expected: []string{
"a\nb\nc",
},
},
{
name: "newline delimiter",
input: `a b c
d e f
g
h`,
delimiter: "\n",
expected: []string{
"a b c",
"d e f",
"g ",
"h",
},
},
{
name: "empty input",
input: "",
delimiter: " ",
expected: nil,
},
{
name: "equal input and delimiter",
input: "abc",
delimiter: "abc",
expected: nil,
},
{
name: "unclosed quotes",
input: "a 'b c",
delimiter: " ",
expectedErr: fmt.Errorf("never reached the end of a quoted value"),
},
{
name: "mismatched quotes",
input: `a 'b c' "d '`,
delimiter: " ",
expectedErr: fmt.Errorf("never reached the end of a quoted value"),
},
{
name: "tab delimiters",
input: "a b c",
delimiter: "\t",
expected: []string{
"a",
"b",
"c",
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result, err := SplitString(tc.input, tc.delimiter)

if tc.expectedErr == nil {
assert.NoError(t, err)
assert.Equal(t, tc.expected, result)
} else {
assert.EqualError(t, err, tc.expectedErr.Error())
assert.Nil(t, result)
}
})
}
}
24 changes: 24 additions & 0 deletions pkg/ottl/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,30 @@ func Test_e2e_converters(t *testing.T) {
m.PutDouble("id", 1)
},
},
{
statement: `set(attributes["test"], ParseKeyValue("k1=v1 k2=v2"))`,
want: func(tCtx ottllog.TransformContext) {
m := tCtx.GetLogRecord().Attributes().PutEmptyMap("test")
m.PutStr("k1", "v1")
m.PutStr("k2", "v2")
},
},
{
statement: `set(attributes["test"], ParseKeyValue("k1!v1_k2!v2", "!", "_"))`,
want: func(tCtx ottllog.TransformContext) {
m := tCtx.GetLogRecord().Attributes().PutEmptyMap("test")
m.PutStr("k1", "v1")
m.PutStr("k2", "v2")
},
},
{
statement: `set(attributes["test"], ParseKeyValue("k1!v1_k2!\"v2__!__v2\"", "!", "_"))`,
want: func(tCtx ottllog.TransformContext) {
m := tCtx.GetLogRecord().Attributes().PutEmptyMap("test")
m.PutStr("k1", "v1")
m.PutStr("k2", "v2__!__v2")
},
},
{
statement: `set(attributes["test"], Seconds(Duration("1m")))`,
want: func(tCtx ottllog.TransformContext) {
Expand Down
21 changes: 21 additions & 0 deletions pkg/ottl/ottlfuncs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ Available Converters:
- [Nanoseconds](#nanoseconds)
- [Now](#now)
- [ParseJSON](#parsejson)
- [ParseKeyValue](#parsekeyvalue)
- [Seconds](#seconds)
- [SHA1](#sha1)
- [SHA256](#sha256)
Expand Down Expand Up @@ -840,6 +841,26 @@ Examples:

- `ParseJSON(body)`

### ParseKeyValue

`ParseKeyValue(target, Optional[delimiter], Optional[pair_delimiter])`

The `ParseKeyValue` Converter returns a `pcommon.Map` that is a result of parsing the target string for key value pairs.

`target` is a Getter that returns a string. `delimiter` is an optional string that is used to split the key and value in a pair, the default is `=`. `pair_delimiter` is an optional string that is used to split key value pairs, the default is a single space (` `).

For example, the following target `"k1=v1 k2=v2 k3=v3"` will use default delimiters and be parsed into the following map:
```
{ "k1": "v1", "k2": "v2", "k3": "v3" }
```

Examples:

- `ParseKeyValue("k1=v1 k2=v2 k3=v3")`
- `ParseKeyValue("k1!v1_k2!v2_k3!v3", "!", "_")`
- `ParseKeyValue(attributes["pairs"])`


### Seconds

`Seconds(value)`
Expand Down
Loading