-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pkg/ottl] Add ToKeyValueString Converter (#35409)
**Description:** <Describe what has changed.> <!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> Implements ToKeyValueString OTTL Converter. **Link to tracking Issue:** #35334 **Testing:** Added unit tests and e2e **Documentation:** Added
- Loading branch information
Showing
6 changed files
with
462 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ToKeyValueString Converter | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [35334] | ||
|
||
# (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: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package ottlfuncs // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs" | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
gosort "sort" | ||
"strings" | ||
|
||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
) | ||
|
||
type ToKeyValueStringArguments[K any] struct { | ||
Target ottl.PMapGetter[K] | ||
Delimiter ottl.Optional[string] | ||
PairDelimiter ottl.Optional[string] | ||
SortOutput ottl.Optional[bool] | ||
} | ||
|
||
func NewToKeyValueStringFactory[K any]() ottl.Factory[K] { | ||
return ottl.NewFactory("ToKeyValueString", &ToKeyValueStringArguments[K]{}, createToKeyValueStringFunction[K]) | ||
} | ||
|
||
func createToKeyValueStringFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) { | ||
args, ok := oArgs.(*ToKeyValueStringArguments[K]) | ||
|
||
if !ok { | ||
return nil, fmt.Errorf("ToKeyValueStringFactory args must be of type *ToKeyValueStringArguments[K]") | ||
} | ||
|
||
return toKeyValueString[K](args.Target, args.Delimiter, args.PairDelimiter, args.SortOutput) | ||
} | ||
|
||
func toKeyValueString[K any](target ottl.PMapGetter[K], d ottl.Optional[string], p ottl.Optional[string], s ottl.Optional[bool]) (ottl.ExprFunc[K], error) { | ||
delimiter := "=" | ||
if !d.IsEmpty() { | ||
if d.Get() == "" { | ||
return nil, fmt.Errorf("delimiter cannot be set to an empty string") | ||
} | ||
delimiter = d.Get() | ||
} | ||
|
||
pairDelimiter := " " | ||
if !p.IsEmpty() { | ||
if p.Get() == "" { | ||
return nil, fmt.Errorf("pair delimiter cannot be set to an empty string") | ||
} | ||
pairDelimiter = p.Get() | ||
} | ||
|
||
if pairDelimiter == delimiter { | ||
return nil, fmt.Errorf("pair delimiter %q cannot be equal to delimiter %q", pairDelimiter, delimiter) | ||
} | ||
|
||
sortOutput := false | ||
if !s.IsEmpty() { | ||
sortOutput = s.Get() | ||
} | ||
|
||
return func(ctx context.Context, tCtx K) (any, error) { | ||
source, err := target.Get(ctx, tCtx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return convertMapToKV(source, delimiter, pairDelimiter, sortOutput), nil | ||
}, nil | ||
} | ||
|
||
// convertMapToKV converts a pcommon.Map to a key value string | ||
func convertMapToKV(target pcommon.Map, delimiter string, pairDelimiter string, sortOutput bool) string { | ||
|
||
var kvStrings []string | ||
if sortOutput { | ||
var keyValues []struct { | ||
key string | ||
val pcommon.Value | ||
} | ||
|
||
// Sort by keys | ||
target.Range(func(k string, v pcommon.Value) bool { | ||
keyValues = append(keyValues, struct { | ||
key string | ||
val pcommon.Value | ||
}{key: k, val: v}) | ||
return true | ||
}) | ||
gosort.Slice(keyValues, func(i, j int) bool { | ||
return keyValues[i].key < keyValues[j].key | ||
}) | ||
|
||
// Convert KV pairs | ||
for _, kv := range keyValues { | ||
kvStrings = append(kvStrings, buildKVString(kv.key, kv.val, delimiter, pairDelimiter)) | ||
} | ||
} else { | ||
target.Range(func(k string, v pcommon.Value) bool { | ||
kvStrings = append(kvStrings, buildKVString(k, v, delimiter, pairDelimiter)) | ||
return true | ||
}) | ||
} | ||
|
||
return strings.Join(kvStrings, pairDelimiter) | ||
} | ||
|
||
func buildKVString(k string, v pcommon.Value, delimiter string, pairDelimiter string) string { | ||
key := escapeAndQuoteKV(k, delimiter, pairDelimiter) | ||
value := escapeAndQuoteKV(v.AsString(), delimiter, pairDelimiter) | ||
return key + delimiter + value | ||
} | ||
|
||
func escapeAndQuoteKV(s string, delimiter string, pairDelimiter string) string { | ||
s = strings.ReplaceAll(s, `"`, `\"`) | ||
if strings.Contains(s, pairDelimiter) || strings.Contains(s, delimiter) { | ||
s = `"` + s + `"` | ||
} | ||
return s | ||
} |
Oops, something went wrong.