forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
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 SliceToMap function (open-telemetry#35412)
**Description:** This PR adds a function that converts slices to maps, as described in the linked issue. Currently still WIP, but creating a draft PR already to show how this could be implemented and used **Link to tracking Issue:** open-telemetry#35256 **Testing:** Added unit and end to end tests **Documentation:** Added description for the new function in the readme file --------- Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com> Co-authored-by: Daniel Jaglowski <jaglows3@gmail.com> Co-authored-by: Evan Bradley <11745660+evan-bradley@users.noreply.github.com>
- Loading branch information
1 parent
1ef1f83
commit 98f321b
Showing
6 changed files
with
577 additions
and
3 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 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] | ||
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.