Skip to content

Commit 0ac9801

Browse files
committed
[pkg/ottl] Fix OTTL functions by using setters
1 parent 3898a7c commit 0ac9801

21 files changed

+270
-99
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: pkg/ottl
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Fix OTTL replace functions.
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [39100]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user]

pkg/ottl/ottlfuncs/func_delete_key.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ package ottlfuncs // import "github.com/open-telemetry/opentelemetry-collector-c
66
import (
77
"context"
88
"errors"
9+
"fmt"
10+
11+
"go.opentelemetry.io/collector/pdata/pcommon"
912

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

1316
type DeleteKeyArguments[K any] struct {
14-
Target ottl.PMapGetter[K]
17+
Target ottl.GetSetter[K]
1518
Key string
1619
}
1720

@@ -29,13 +32,39 @@ func createDeleteKeyFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments
2932
return deleteKey(args.Target, args.Key), nil
3033
}
3134

32-
func deleteKey[K any](target ottl.PMapGetter[K], key string) ottl.ExprFunc[K] {
35+
func deleteKey[K any](target ottl.GetSetter[K], key string) ottl.ExprFunc[K] {
3336
return func(ctx context.Context, tCtx K) (any, error) {
34-
val, err := target.Get(ctx, tCtx)
37+
val, err := anyToMap(ctx, tCtx, target)
3538
if err != nil {
3639
return nil, err
3740
}
3841
val.Remove(key)
39-
return nil, nil
42+
return nil, target.Set(ctx, tCtx, val)
43+
}
44+
}
45+
46+
var emptyMap = pcommon.Map{}
47+
48+
func anyToMap[K any](ctx context.Context, tCtx K, target ottl.GetSetter[K]) (pcommon.Map, error) {
49+
anyVal, err := target.Get(ctx, tCtx)
50+
if err != nil {
51+
return emptyMap, err
52+
}
53+
switch v := anyVal.(type) {
54+
case pcommon.Map:
55+
return v, nil
56+
case pcommon.Value:
57+
if v.Type() != pcommon.ValueTypeMap {
58+
return emptyMap, ottl.TypeError(fmt.Sprintf("expected pcommon.Map but got %v", v.Type()))
59+
}
60+
return v.Map(), nil
61+
case map[string]any:
62+
m := pcommon.NewMap()
63+
if err = m.FromRaw(v); err != nil {
64+
return emptyMap, err
65+
}
66+
return m, nil
67+
default:
68+
return emptyMap, ottl.TypeError(fmt.Sprintf("expected pcommon.Map but got %T", v))
4069
}
4170
}

pkg/ottl/ottlfuncs/func_delete_key_test.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package ottlfuncs
55

66
import (
77
"context"
8+
"errors"
89
"testing"
910

1011
"github.com/stretchr/testify/assert"
@@ -19,15 +20,22 @@ func Test_deleteKey(t *testing.T) {
1920
input.PutInt("test2", 3)
2021
input.PutBool("test3", true)
2122

22-
target := &ottl.StandardPMapGetter[pcommon.Map]{
23+
target := &ottl.StandardGetSetter[pcommon.Map]{
2324
Getter: func(_ context.Context, tCtx pcommon.Map) (any, error) {
2425
return tCtx, nil
2526
},
27+
Setter: func(_ context.Context, tCtx pcommon.Map, val any) error {
28+
if v, ok := val.(pcommon.Map); ok {
29+
v.CopyTo(tCtx)
30+
return nil
31+
}
32+
return errors.New("expected pcommon.Map")
33+
},
2634
}
2735

2836
tests := []struct {
2937
name string
30-
target ottl.PMapGetter[pcommon.Map]
38+
target ottl.GetSetter[pcommon.Map]
3139
key string
3240
want func(pcommon.Map)
3341
}{
@@ -80,7 +88,7 @@ func Test_deleteKey(t *testing.T) {
8088

8189
func Test_deleteKey_bad_input(t *testing.T) {
8290
input := pcommon.NewValueStr("not a map")
83-
target := &ottl.StandardPMapGetter[any]{
91+
target := &ottl.StandardGetSetter[any]{
8492
Getter: func(_ context.Context, tCtx any) (any, error) {
8593
return tCtx, nil
8694
},
@@ -94,7 +102,7 @@ func Test_deleteKey_bad_input(t *testing.T) {
94102
}
95103

96104
func Test_deleteKey_get_nil(t *testing.T) {
97-
target := &ottl.StandardPMapGetter[any]{
105+
target := &ottl.StandardGetSetter[any]{
98106
Getter: func(_ context.Context, tCtx any) (any, error) {
99107
return tCtx, nil
100108
},

pkg/ottl/ottlfuncs/func_delete_matching_keys.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
type DeleteMatchingKeysArguments[K any] struct {
18-
Target ottl.PMapGetter[K]
18+
Target ottl.GetSetter[K]
1919
Pattern string
2020
}
2121

@@ -33,19 +33,19 @@ func createDeleteMatchingKeysFunction[K any](_ ottl.FunctionContext, oArgs ottl.
3333
return deleteMatchingKeys(args.Target, args.Pattern)
3434
}
3535

36-
func deleteMatchingKeys[K any](target ottl.PMapGetter[K], pattern string) (ottl.ExprFunc[K], error) {
36+
func deleteMatchingKeys[K any](target ottl.GetSetter[K], pattern string) (ottl.ExprFunc[K], error) {
3737
compiledPattern, err := regexp.Compile(pattern)
3838
if err != nil {
3939
return nil, fmt.Errorf("the regex pattern supplied to delete_matching_keys is not a valid pattern: %w", err)
4040
}
4141
return func(ctx context.Context, tCtx K) (any, error) {
42-
val, err := target.Get(ctx, tCtx)
42+
val, err := anyToMap(ctx, tCtx, target)
4343
if err != nil {
4444
return nil, err
4545
}
4646
val.RemoveIf(func(key string, _ pcommon.Value) bool {
4747
return compiledPattern.MatchString(key)
4848
})
49-
return nil, nil
49+
return nil, target.Set(ctx, tCtx, val)
5050
}, nil
5151
}

pkg/ottl/ottlfuncs/func_delete_matching_keys_test.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package ottlfuncs
55

66
import (
77
"context"
8+
"errors"
89
"testing"
910

1011
"github.com/stretchr/testify/assert"
@@ -20,15 +21,22 @@ func Test_deleteMatchingKeys(t *testing.T) {
2021
input.PutInt("test2", 3)
2122
input.PutBool("test3", true)
2223

23-
target := &ottl.StandardPMapGetter[pcommon.Map]{
24+
target := &ottl.StandardGetSetter[pcommon.Map]{
2425
Getter: func(_ context.Context, tCtx pcommon.Map) (any, error) {
2526
return tCtx, nil
2627
},
28+
Setter: func(_ context.Context, tCtx pcommon.Map, m any) error {
29+
if v, ok := m.(pcommon.Map); ok {
30+
v.CopyTo(tCtx)
31+
return nil
32+
}
33+
return errors.New("expected pcommon.Map")
34+
},
2735
}
2836

2937
tests := []struct {
3038
name string
31-
target ottl.PMapGetter[pcommon.Map]
39+
target ottl.GetSetter[pcommon.Map]
3240
pattern string
3341
want func(pcommon.Map)
3442
}{
@@ -80,7 +88,7 @@ func Test_deleteMatchingKeys(t *testing.T) {
8088

8189
func Test_deleteMatchingKeys_bad_input(t *testing.T) {
8290
input := pcommon.NewValueInt(1)
83-
target := &ottl.StandardPMapGetter[any]{
91+
target := &ottl.StandardGetSetter[any]{
8492
Getter: func(_ context.Context, tCtx any) (any, error) {
8593
return tCtx, nil
8694
},
@@ -94,7 +102,7 @@ func Test_deleteMatchingKeys_bad_input(t *testing.T) {
94102
}
95103

96104
func Test_deleteMatchingKeys_get_nil(t *testing.T) {
97-
target := &ottl.StandardPMapGetter[any]{
105+
target := &ottl.StandardGetSetter[any]{
98106
Getter: func(_ context.Context, tCtx any) (any, error) {
99107
return tCtx, nil
100108
},
@@ -107,7 +115,7 @@ func Test_deleteMatchingKeys_get_nil(t *testing.T) {
107115
}
108116

109117
func Test_deleteMatchingKeys_invalid_pattern(t *testing.T) {
110-
target := &ottl.StandardPMapGetter[any]{
118+
target := &ottl.StandardGetSetter[any]{
111119
Getter: func(_ context.Context, _ any) (any, error) {
112120
t.Errorf("nothing should be received in this scenario")
113121
return nil, nil

pkg/ottl/ottlfuncs/func_flatten.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
)
1717

1818
type FlattenArguments[K any] struct {
19-
Target ottl.PMapGetter[K]
19+
Target ottl.GetSetter[K]
2020
Prefix ottl.Optional[string]
2121
Depth ottl.Optional[int64]
2222
ResolveConflicts ottl.Optional[bool]
@@ -43,7 +43,7 @@ func createFlattenFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments)
4343
return flatten(args.Target, args.Prefix, args.Depth, args.ResolveConflicts)
4444
}
4545

46-
func flatten[K any](target ottl.PMapGetter[K], p ottl.Optional[string], d ottl.Optional[int64], c ottl.Optional[bool]) (ottl.ExprFunc[K], error) {
46+
func flatten[K any](target ottl.GetSetter[K], p ottl.Optional[string], d ottl.Optional[int64], c ottl.Optional[bool]) (ottl.ExprFunc[K], error) {
4747
depth := int64(math.MaxInt64)
4848
if !d.IsEmpty() {
4949
depth = d.Get()
@@ -63,7 +63,7 @@ func flatten[K any](target ottl.PMapGetter[K], p ottl.Optional[string], d ottl.O
6363
}
6464

6565
return func(ctx context.Context, tCtx K) (any, error) {
66-
m, err := target.Get(ctx, tCtx)
66+
m, err := anyToMap(ctx, tCtx, target)
6767
if err != nil {
6868
return nil, err
6969
}
@@ -72,7 +72,7 @@ func flatten[K any](target ottl.PMapGetter[K], p ottl.Optional[string], d ottl.O
7272
flattenData.flattenMap(m, prefix, 0)
7373
flattenData.result.MoveTo(m)
7474

75-
return nil, nil
75+
return nil, target.Set(ctx, tCtx, m)
7676
}, nil
7777
}
7878

pkg/ottl/ottlfuncs/func_flatten_test.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package ottlfuncs
55

66
import (
77
"context"
8+
"errors"
9+
"fmt"
810
"reflect"
911
"testing"
1012

@@ -340,10 +342,20 @@ func Test_flatten(t *testing.T) {
340342
m := pcommon.NewMap()
341343
err := m.FromRaw(tt.target)
342344
assert.NoError(t, err)
343-
target := ottl.StandardPMapGetter[any]{
345+
target := ottl.StandardGetSetter[any]{
344346
Getter: func(_ context.Context, _ any) (any, error) {
345347
return m, nil
346348
},
349+
Setter: func(_ context.Context, tCtx any, m any) error {
350+
fmt.Printf("### %T %T\n", tCtx, m)
351+
if v, ok := m.(pcommon.Map); ok {
352+
if dst, ok2 := m.(pcommon.Map); ok2 {
353+
v.CopyTo(dst)
354+
return nil
355+
}
356+
}
357+
return errors.New("expected pcommon.Map")
358+
},
347359
}
348360

349361
exprFunc, err := flatten[any](target, tt.prefix, tt.depth, ottl.NewTestingOptional[bool](tt.conflict))
@@ -490,10 +502,19 @@ func Test_flatten_undeterministic(t *testing.T) {
490502
m := pcommon.NewMap()
491503
err := m.FromRaw(tt.target)
492504
assert.NoError(t, err)
493-
target := ottl.StandardPMapGetter[any]{
505+
target := ottl.StandardGetSetter[any]{
494506
Getter: func(_ context.Context, _ any) (any, error) {
495507
return m, nil
496508
},
509+
Setter: func(_ context.Context, tCtx any, m any) error {
510+
if v, ok := m.(pcommon.Map); ok {
511+
if dst, ok2 := tCtx.(pcommon.Map); ok2 {
512+
v.CopyTo(dst)
513+
}
514+
return nil
515+
}
516+
return errors.New("expected pcommon.Map")
517+
},
497518
}
498519

499520
exprFunc, err := flatten[any](target, tt.prefix, tt.depth, ottl.NewTestingOptional[bool](tt.conflict))
@@ -510,7 +531,7 @@ func Test_flatten_undeterministic(t *testing.T) {
510531
}
511532

512533
func Test_flatten_bad_target(t *testing.T) {
513-
target := &ottl.StandardPMapGetter[any]{
534+
target := &ottl.StandardGetSetter[any]{
514535
Getter: func(_ context.Context, _ any) (any, error) {
515536
return 1, nil
516537
},
@@ -538,7 +559,7 @@ func Test_flatten_bad_depth(t *testing.T) {
538559

539560
for _, tt := range tests {
540561
t.Run(tt.name, func(t *testing.T) {
541-
target := &ottl.StandardPMapGetter[any]{
562+
target := &ottl.StandardGetSetter[any]{
542563
Getter: func(_ context.Context, _ any) (any, error) {
543564
return pcommon.NewMap(), nil
544565
},

pkg/ottl/ottlfuncs/func_keep_keys.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
type KeepKeysArguments[K any] struct {
16-
Target ottl.PMapGetter[K]
16+
Target ottl.GetSetter[K]
1717
Keys []string
1818
}
1919

@@ -31,14 +31,14 @@ func createKeepKeysFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments)
3131
return keepKeys(args.Target, args.Keys), nil
3232
}
3333

34-
func keepKeys[K any](target ottl.PMapGetter[K], keys []string) ottl.ExprFunc[K] {
34+
func keepKeys[K any](target ottl.GetSetter[K], keys []string) ottl.ExprFunc[K] {
3535
keySet := make(map[string]struct{}, len(keys))
3636
for _, key := range keys {
3737
keySet[key] = struct{}{}
3838
}
3939

4040
return func(ctx context.Context, tCtx K) (any, error) {
41-
val, err := target.Get(ctx, tCtx)
41+
val, err := anyToMap(ctx, tCtx, target)
4242
if err != nil {
4343
return nil, err
4444
}
@@ -49,6 +49,6 @@ func keepKeys[K any](target ottl.PMapGetter[K], keys []string) ottl.ExprFunc[K]
4949
if val.Len() == 0 {
5050
val.Clear()
5151
}
52-
return nil, nil
52+
return nil, target.Set(ctx, tCtx, val)
5353
}
5454
}

0 commit comments

Comments
 (0)