Skip to content

Add OTTL get editor function #35618

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions pkg/ottl/ottlfuncs/func_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
)

type GetArguments[K any] struct {
Value ottl.Getter[K]
}

func NewGetFactory[K any]() ottl.Factory[K] {
return ottl.NewFactory("get", &GetArguments[K]{}, createGetFunction[K])
}

func createGetFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) {
args, ok := oArgs.(*GetArguments[K])

if !ok {
return nil, fmt.Errorf("GetFactory args must be of type *GetArguments[K]")
}

return get(args.Value), nil
}

func get[K any](value ottl.Getter[K]) ottl.ExprFunc[K] {
return func(ctx context.Context, tCtx K) (any, error) {
return value.Get(ctx, tCtx)
}
}
71 changes: 71 additions & 0 deletions pkg/ottl/ottlfuncs/func_get_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package ottlfuncs

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/pdata/pcommon"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
)

func Test_get(t *testing.T) {
tests := []struct {
name string
getter ottl.Getter[any]
want pcommon.Value
}{
{
name: "get string",
getter: ottl.StandardGetSetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return "a", nil
},
},
want: pcommon.NewValueStr("a"),
},
{
name: "get int",
getter: ottl.StandardGetSetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return 11, nil
},
},
want: pcommon.NewValueInt(11),
},
{
name: "get double",
getter: ottl.StandardGetSetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return 104.12, nil
},
},
want: pcommon.NewValueDouble(104.12),
},
{
name: "nil",
getter: ottl.StandardGetSetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return nil, nil
},
},
want: pcommon.NewValueEmpty(),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exprFunc := get(tt.getter)

result, err := exprFunc(context.Background(), nil)
assert.NoError(t, err)

actual := pcommon.NewValueEmpty()
assert.NoError(t, actual.FromRaw(result))
assert.Equal(t, tt.want, actual)
})
}
}
1 change: 1 addition & 0 deletions pkg/ottl/ottlfuncs/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
func StandardFuncs[K any]() map[string]ottl.Factory[K] {
f := []ottl.Factory[K]{
// Editors
NewGetFactory[K](),
NewDeleteKeyFactory[K](),
NewDeleteMatchingKeysFactory[K](),
NewKeepMatchingKeysFactory[K](),
Expand Down
Loading