-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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 SliceToMap function #35412
Changes from 6 commits
02e3beb
1dbe782
d4bd9d5
daec82b
063f3f7
2fcb89d
62bfd95
55044da
7ddd25e
9312683
1c51b42
85d7ba7
e2e10d0
2b5994b
b1a2e2c
dc7fda9
9bff249
fc0920c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -450,6 +450,7 @@ Available Converters: | |
- [SHA1](#sha1) | ||
- [SHA256](#sha256) | ||
- [SHA512](#sha512) | ||
- [SliceToMap](#slicetomap) | ||
- [Sort](#sort) | ||
- [SpanID](#spanid) | ||
- [Split](#split) | ||
|
@@ -1357,6 +1358,59 @@ Examples: | |
|
||
- `SHA512("name")` | ||
|
||
### SliceToMap | ||
|
||
`SliceToMap(target, keyPath, Optional[valuePath])` | ||
TylerHelmuth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The `SliceToMap` converter converts a slice of objects to a map. The name of the keys for the map entries | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we describe how this could be used to address slice items? I think we should highlight the fact that this allows limited list manipulation in OTTL. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have added a small sample for how the converted map could be used afterwards now. |
||
is determined by `keyPath`, which points to the value of an attribute within each slice item. Note that | ||
the `keyPath` must resolve to a string value, otherwise the converter will not be able to convert the item | ||
to a map entry. | ||
The optional `valuePath` determines which attribute should be used as the value for the map entry. If no | ||
`valuePath` is defined, the value of the map entry will be the same as the original slice item. | ||
|
||
Examples: | ||
|
||
The examples below will convert the following input: | ||
|
||
```yaml | ||
attributes: | ||
hello: world | ||
things: | ||
- name: foo | ||
value: 2 | ||
- name: bar | ||
value: 5 | ||
``` | ||
|
||
- `SliceToMap(attributes["things"], ["name"])`: | ||
|
||
This converts the input above to the following: | ||
|
||
```yaml | ||
attributes: | ||
hello: world | ||
things: | ||
foo: | ||
name: foo | ||
value: 2 | ||
bar: | ||
name: bar | ||
value: 5 | ||
``` | ||
|
||
- `Associate(attributes["things"], ["name"], ["details", "value"])`: | ||
bacherfl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
This converts the input above to the following: | ||
|
||
```yaml | ||
attributes: | ||
hello: world | ||
things: | ||
foo: 2 | ||
bar: 5 | ||
``` | ||
|
||
### Sort | ||
|
||
`Sort(target, Optional[order])` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package ottlfuncs // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs" | ||
import ( | ||
"fmt" | ||
|
||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
"golang.org/x/net/context" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
) | ||
|
||
type SliceToMapArguments[K any] struct { | ||
Target ottl.Getter[K] | ||
TylerHelmuth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
KeyPath []string | ||
ValuePath ottl.Optional[[]string] | ||
} | ||
|
||
func NewSliceToMapFactory[K any]() ottl.Factory[K] { | ||
return ottl.NewFactory("SliceToMap", &SliceToMapArguments[K]{}, sliceToMapFunction[K]) | ||
} | ||
|
||
func sliceToMapFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) { | ||
args, ok := oArgs.(*SliceToMapArguments[K]) | ||
if !ok { | ||
return nil, fmt.Errorf("AssociateFactory args must be of type *SliceToMapArguments[K") | ||
bacherfl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return SliceToMap(args.Target, args.KeyPath, args.ValuePath) | ||
} | ||
|
||
func SliceToMap[K any](target ottl.Getter[K], keyPath []string, valuePath ottl.Optional[[]string]) (ottl.ExprFunc[K], error) { | ||
bacherfl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return func(ctx context.Context, tCtx K) (any, error) { | ||
if len(keyPath) == 0 { | ||
return nil, fmt.Errorf("key path must contain at least one element") | ||
} | ||
djaglowski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
val, err := target.Get(ctx, tCtx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
switch v := val.(type) { | ||
case []any: | ||
return sliceToMap(v, keyPath, valuePath) | ||
case pcommon.Slice: | ||
return sliceToMap(v.AsRaw(), keyPath, valuePath) | ||
default: | ||
return nil, fmt.Errorf("unsupported type provided to SliceToMap function: %T", v) | ||
} | ||
}, nil | ||
} | ||
|
||
func sliceToMap(v []any, keyPath []string, valuePath ottl.Optional[[]string]) (any, error) { | ||
result := make(map[string]any, len(v)) | ||
for _, elem := range v { | ||
switch e := elem.(type) { | ||
case map[string]any: | ||
bacherfl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
obj, err := extractValue(e, keyPath) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
var key string | ||
switch k := obj.(type) { | ||
case string: | ||
key = k | ||
default: | ||
continue | ||
} | ||
bacherfl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if valuePath.IsEmpty() { | ||
result[key] = e | ||
continue | ||
} | ||
obj, err = extractValue(e, valuePath.Get()) | ||
djaglowski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
continue | ||
} | ||
result[key] = obj | ||
default: | ||
continue | ||
} | ||
} | ||
m := pcommon.NewMap() | ||
if err := m.FromRaw(result); err != nil { | ||
return nil, fmt.Errorf("could not create pcommon.Map from result: %w", err) | ||
} | ||
|
||
return m, nil | ||
} | ||
|
||
func extractValue(v map[string]any, path []string) (any, error) { | ||
if len(path) == 0 { | ||
return nil, fmt.Errorf("must provide at least one path item") | ||
} | ||
obj, ok := v[path[0]] | ||
if !ok { | ||
return nil, fmt.Errorf("provided object does not contain the path %v", path) | ||
} | ||
if len(path) == 1 { | ||
return obj, nil | ||
} | ||
|
||
switch o := obj.(type) { | ||
case map[string]any: | ||
return extractValue(o, path[1:]) | ||
default: | ||
return nil, fmt.Errorf("provided object does not contain the path %v", path) | ||
} | ||
bacherfl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@evan-bradley @TylerHelmuth - This is not related to the function implemented in this PR, but after adding a slice of nested objects to the test attributes I found that the
flatten
function seems to not flatten the attributes of nested objects within a slice, but rather leaves the objects within the slice unchanged - is this intended or is this a bug in that function?Also, when setting the
depth
to0
(as in the test case below), slices at the top level of the input of the flatten function will still be flattened.Please let me know if this behavior is intended or if this may be a bug - If it's the latter, I can create an issue for that and work on a fix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These sound like bugs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These sound like bugs to me, too. Could you file an issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have created two issues (I believe these are two distinct bugs):
#36161
#36162