-
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
Merged
djaglowski
merged 18 commits into
open-telemetry:main
from
bacherfl:feat/35256/associate-function
Nov 4, 2024
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
02e3beb
[pkg/ottl]: add Associate function
bacherfl 1dbe782
[pkg/ottl]: rename to SliceToMap function
bacherfl d4bd9d5
rename files
bacherfl daec82b
skip non-applicable slice elements
bacherfl 063f3f7
add license header and fix linting
bacherfl 2fcb89d
adapt e2e tests
bacherfl 62bfd95
revert accidental change
bacherfl 55044da
Apply suggestions from code review
bacherfl 7ddd25e
adapt to PR review
bacherfl 9312683
add changelog entry
bacherfl 1c51b42
appease linter
bacherfl 85d7ba7
Apply suggestions from code review
bacherfl e2e10d0
fix the example
bacherfl 2b5994b
describe how converted entries can be used
bacherfl b1a2e2c
return error when slice element could not be processed
bacherfl dc7fda9
Merge branch 'main' into feat/35256/associate-function
bacherfl 9bff249
Apply suggestions from code review
bacherfl fc0920c
fix compile error
bacherfl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 SliceToMap function | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [35256] | ||
|
||
# (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,105 @@ | ||
// 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("SliceToMapFactory args must be of type *SliceToMapArguments[K") | ||
} | ||
|
||
return getSliceToMapFunc(args.Target, args.KeyPath, args.ValuePath) | ||
} | ||
|
||
func getSliceToMapFunc[K any](target ottl.Getter[K], keyPath []string, valuePath ottl.Optional[[]string]) (ottl.ExprFunc[K], error) { | ||
if len(keyPath) == 0 { | ||
return nil, fmt.Errorf("key path must contain at least one element") | ||
} | ||
return func(ctx context.Context, tCtx K) (any, error) { | ||
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 { | ||
e, ok := elem.(map[string]any) | ||
if !ok { | ||
return nil, fmt.Errorf("could not cast element '%v' to map[string]any", elem) | ||
} | ||
extractedKey, err := extractValue(e, keyPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not extract key from element: %w", err) | ||
} | ||
|
||
key, ok := extractedKey.(string) | ||
if !ok { | ||
return nil, fmt.Errorf("extracted key attribute is not of type string") | ||
} | ||
|
||
if valuePath.IsEmpty() { | ||
result[key] = e | ||
continue | ||
} | ||
extractedValue, err := extractValue(e, valuePath.Get()) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not extract value from element: %w", err) | ||
} | ||
result[key] = extractedValue | ||
} | ||
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 | ||
} | ||
|
||
if o, ok := obj.(map[string]any); ok { | ||
return extractValue(o, path[1:]) | ||
} | ||
return nil, fmt.Errorf("provided object does not contain the path %v", path) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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