Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pkg/ottl] Add ParseKeyValue function #31035

Merged
merged 9 commits into from
Feb 15, 2024
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: []
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 white space.
evan-bradley marked this conversation as resolved.
Show resolved Hide resolved

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
120 changes: 120 additions & 0 deletions pkg/ottl/ottlfuncs/func_parse_key_value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package ottlfuncs

import (
"context"
"fmt"
"strings"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
"go.opentelemetry.io/collector/pdata/pcommon"
)

type ParseKeyValueArguments[K any] struct {
Target ottl.StringGetter[K]
Delimiter ottl.Optional[string]
PairDelimiter ottl.Optional[string]
evan-bradley marked this conversation as resolved.
Show resolved Hide resolved
}

func NewParseKeyValueFactory[K any]() ottl.Factory[K] {
return ottl.NewFactory("ParseKeyValue", &ParseKeyValueArguments[K]{}, createParseKeyValueFunction[K])
}

func createParseKeyValueFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) {
args, ok := oArgs.(*ParseKeyValueArguments[K])

if !ok {
return nil, fmt.Errorf("ParseKeyValueFactory args must be of type *ParseKeyValueArguments[K]")
}

return parseKeyValue[K](args.Target, args.Delimiter, args.PairDelimiter)
}

func parseKeyValue[K any](target ottl.StringGetter[K], d ottl.Optional[string], p ottl.Optional[string]) (ottl.ExprFunc[K], error) {
delimiter := "="
if !d.IsEmpty() {
delimiter = d.Get()
}

pairDelimiter := " "
if !p.IsEmpty() {
pairDelimiter = p.Get()
}

if pairDelimiter == delimiter {
return nil, fmt.Errorf("pair delimiter \"%s\" cannot be equal to delimiter \"%s\"", pairDelimiter, delimiter)
evan-bradley marked this conversation as resolved.
Show resolved Hide resolved
}
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved

return func(ctx context.Context, tCtx K) (any, error) {
source, err := target.Get(ctx, tCtx)
if err != nil {
return nil, err
}

if source == "" {
return nil, fmt.Errorf("cannot parse from empty target")
evan-bradley marked this conversation as resolved.
Show resolved Hide resolved
}

pairs, err := splitPairs(source, pairDelimiter)
if err != nil {
return nil, fmt.Errorf("splitting pairs failed: %w", err)
}

parsed := make(map[string]any)
for _, p := range pairs {
pair := strings.SplitN(p, delimiter, 2)
if len(pair) != 2 {
return nil, fmt.Errorf("cannot split '%s' into 2 items, got %d", p, len(pair))
evan-bradley marked this conversation as resolved.
Show resolved Hide resolved
}
key := strings.TrimSpace(strings.Trim(pair[0], "\"'"))
value := strings.TrimSpace(strings.Trim(pair[1], "\"'"))
parsed[key] = value
}

result := pcommon.NewMap()
err = result.FromRaw(parsed)
return result, err
}, nil
}

// splitPairs will split the input on the pairDelimiter and return the resulting slice.
// `strings.Split` is not used because it does not respect quotes and will split if the delimiter appears in a quoted value
func splitPairs(input, pairDelimiter string) ([]string, error) {
evan-bradley marked this conversation as resolved.
Show resolved Hide resolved
var result []string
currentPair := ""
delimiterLength := len(pairDelimiter)
quoteChar := "" // "" means we are not in quotes

i := 0
for i < len(input) {
if quoteChar == "" && i+delimiterLength <= len(input) && input[i:i+delimiterLength] == pairDelimiter {
if currentPair == "" {
i++
continue
}
result = append(result, currentPair)
currentPair = ""
i += delimiterLength
continue
} else if input[i] == '"' || input[i] == '\'' {
if quoteChar != "" {
if quoteChar == string(input[i]) {
quoteChar = ""
}
} else {
quoteChar = string(input[i])
}
}
currentPair += string(input[i])
i++
}

if quoteChar != "" {
return nil, fmt.Errorf("never reached end of a quoted value")
}

if currentPair != "" {
result = append(result, currentPair)
}

return result, nil
}
Loading
Loading