-
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.
Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
- Loading branch information
Showing
4 changed files
with
403 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
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,109 @@ | ||
// 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" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
type AssociateArguments[K any] struct { | ||
Target ottl.Getter[K] | ||
KeyPath []string | ||
ValuePath ottl.Optional[[]string] | ||
} | ||
|
||
func NewAssociateFactory[K any]() ottl.Factory[K] { | ||
return ottl.NewFactory("Associate", &AssociateArguments[K]{}, createAssociateFunction[K]) | ||
} | ||
|
||
func createAssociateFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) { | ||
args, ok := oArgs.(*AssociateArguments[K]) | ||
if !ok { | ||
return nil, fmt.Errorf("AssociateFactory args must be of type *AssociateArguments[K") | ||
} | ||
|
||
return Associate(args.Target, args.KeyPath, args.ValuePath) | ||
} | ||
|
||
func Associate[K any](target ottl.Getter[K], keyPath []string, valuePath ottl.Optional[[]string]) (ottl.ExprFunc[K], error) { | ||
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") | ||
} | ||
val, err := target.Get(ctx, tCtx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
switch v := val.(type) { | ||
case []any: | ||
return associate(v, keyPath, valuePath) | ||
case pcommon.Slice: | ||
return associate(v.AsRaw(), keyPath, valuePath) | ||
default: | ||
return nil, fmt.Errorf("unsupported type provided to Associate function: %T", v) | ||
} | ||
}, nil | ||
} | ||
|
||
func associate(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: | ||
obj, err := extractValue(e, keyPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not extract key value: %w", err) | ||
} | ||
|
||
var key string | ||
switch k := obj.(type) { | ||
case string: | ||
key = k | ||
default: | ||
return nil, fmt.Errorf("provided key path %v does not resolve to a string", keyPath) | ||
} | ||
|
||
if valuePath.IsEmpty() { | ||
result[key] = e | ||
continue | ||
} | ||
obj, err = extractValue(e, valuePath.Get()) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not extract value: %w", err) | ||
} | ||
result[key] = obj | ||
default: | ||
return nil, fmt.Errorf("unsupported value type: %T", e) | ||
} | ||
} | ||
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) | ||
} | ||
} |
Oops, something went wrong.