Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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
27 changes: 27 additions & 0 deletions .chloggen/add-ottl-delete-elemelt-function.yaml
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. receiver/filelog)
component: pkg/ottl

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Introducing `delete` function for deleting items from an existing array"

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [43098]

# (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: [user]
71 changes: 71 additions & 0 deletions pkg/ottl/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func Test_e2e_editors(t *testing.T) {
tCtx.GetLogRecord().Attributes().Remove("things")
tCtx.GetLogRecord().Attributes().Remove("conflict.conflict1")
tCtx.GetLogRecord().Attributes().Remove("conflict")
tCtx.GetLogRecord().Attributes().Remove("slice2")
},
},
{
Expand All @@ -74,6 +75,7 @@ func Test_e2e_editors(t *testing.T) {
tCtx.GetLogRecord().Attributes().Remove("things")
tCtx.GetLogRecord().Attributes().Remove("conflict.conflict1")
tCtx.GetLogRecord().Attributes().Remove("conflict")
tCtx.GetLogRecord().Attributes().Remove("slice2")
},
},
{
Expand All @@ -93,6 +95,12 @@ func Test_e2e_editors(t *testing.T) {
tCtx.GetLogRecord().Attributes().PutInt("things.0.value", 2)
tCtx.GetLogRecord().Attributes().PutStr("things.1.name", "bar")
tCtx.GetLogRecord().Attributes().PutInt("things.1.value", 5)

tCtx.GetLogRecord().Attributes().Remove("slice2")
tCtx.GetLogRecord().Attributes().PutStr("slice2.0", "val")
tCtx.GetLogRecord().Attributes().PutStr("slice2.1", "foo")
tCtx.GetLogRecord().Attributes().PutStr("slice2.2", "bar")
tCtx.GetLogRecord().Attributes().PutStr("slice2.3", "baz")
},
},
{
Expand All @@ -117,6 +125,11 @@ func Test_e2e_editors(t *testing.T) {
m.PutInt("test.things.0.value", 2)
m.PutStr("test.things.1.name", "bar")
m.PutInt("test.things.1.value", 5)

m.PutStr("test.slice2.0", "val")
m.PutStr("test.slice2.1", "foo")
m.PutStr("test.slice2.2", "bar")
m.PutStr("test.slice2.3", "baz")
m.CopyTo(tCtx.GetLogRecord().Attributes())
},
},
Expand Down Expand Up @@ -145,6 +158,11 @@ func Test_e2e_editors(t *testing.T) {
m.PutStr("test.things.1.name", "bar")
m.PutInt("test.things.1.value", 5)

m.PutStr("test.slice2", "val")
m.PutStr("test.slice2.0", "foo")
m.PutStr("test.slice2.1", "bar")
m.PutStr("test.slice2.2", "baz")

m.CopyTo(tCtx.GetLogRecord().Attributes())
},
},
Expand All @@ -161,6 +179,10 @@ func Test_e2e_editors(t *testing.T) {
m.PutStr("foo.flags", "pass")
m.PutStr("foo.bar", "pass")
m.PutStr("foo.flags", "pass")
m.PutStr("slice2.0", "val")
m.PutStr("slice2.1", "foo")
m.PutStr("slice2.2", "bar")
m.PutStr("slice2.3", "baz")
m.PutEmptySlice("foo.slice").AppendEmpty().SetStr("val")
m.PutStr("conflict.conflict1.conflict2", "nopass")
mm := m.PutEmptyMap("conflict.conflict1")
Expand Down Expand Up @@ -189,6 +211,7 @@ func Test_e2e_editors(t *testing.T) {
tCtx.GetLogRecord().Attributes().Remove("things")
tCtx.GetLogRecord().Attributes().Remove("conflict.conflict1")
tCtx.GetLogRecord().Attributes().Remove("conflict")
tCtx.GetLogRecord().Attributes().Remove("slice2")
},
},
{
Expand All @@ -206,6 +229,7 @@ func Test_e2e_editors(t *testing.T) {
tCtx.GetLogRecord().Attributes().Remove("things")
tCtx.GetLogRecord().Attributes().Remove("conflict.conflict1")
tCtx.GetLogRecord().Attributes().Remove("conflict")
tCtx.GetLogRecord().Attributes().Remove("slice2")
},
},
{
Expand Down Expand Up @@ -393,6 +417,47 @@ func Test_e2e_editors(t *testing.T) {
s.AppendEmpty().SetInt(6)
},
},
{
statement: `delete(attributes["slice2"], 0)`,
want: func(tCtx *ottllog.TransformContext) {
v, _ := tCtx.GetLogRecord().Attributes().Get("slice2")
s := v.Slice()
// :ToDo: Implement RemoveAt and RemoveRange in pcommon.Slice
s.RemoveIf(func(v pcommon.Value) bool {
return v.Str() == "val"
})
},
},
{
statement: `delete(attributes["slice2"], Len(attributes["slice2"]) - 1)`,
want: func(tCtx *ottllog.TransformContext) {
v, _ := tCtx.GetLogRecord().Attributes().Get("slice2")
s := v.Slice()
s.RemoveIf(func(v pcommon.Value) bool {
return v.Str() == "baz"
})
},
},
{
statement: `delete(attributes["slice2"], 1, endIndex=3)`,
want: func(tCtx *ottllog.TransformContext) {
v, _ := tCtx.GetLogRecord().Attributes().Get("slice2")
s := v.Slice()
s.RemoveIf(func(v pcommon.Value) bool {
return (v.Str() == "foo" || v.Str() == "bar")
})
},
},
{
statement: `delete(attributes["slice2"], Index(attributes["slice2"], "foo"))`,
want: func(tCtx *ottllog.TransformContext) {
v, _ := tCtx.GetLogRecord().Attributes().Get("slice2")
s := v.Slice()
s.RemoveIf(func(v pcommon.Value) bool {
return v.Str() == "foo"
})
},
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -2154,6 +2219,12 @@ func constructLogTransformContextEditors() *ottllog.TransformContext {
thing2.PutStr("name", "bar")
thing2.PutInt("value", 5)

s3 := logRecord.Attributes().PutEmptySlice("slice2")
s3.AppendEmpty().SetStr("val")
s3.AppendEmpty().SetStr("foo")
s3.AppendEmpty().SetStr("bar")
s3.AppendEmpty().SetStr("baz")

return ottllog.NewTransformContextPtr(rLogs, rLogs.ScopeLogs().At(0), logRecord)
}

Expand Down
17 changes: 17 additions & 0 deletions pkg/ottl/ottlfuncs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Editors:
Available Editors:

- [append](#append)
- [delete](#delete)
- [delete_key](#delete_key)
- [delete_matching_keys](#delete_matching_keys)
- [keep_matching_keys](#keep_matching_keys)
Expand Down Expand Up @@ -73,6 +74,22 @@ Resulting field is always of type `pcommon.Slice` and will not convert the types
- `append(log.attributes["tags"], values = ["staging", "staging:east"])`
- `append(log.attributes["tags_copy"], log.attributes["tags"])`

### delete

`delete(target, startIndex, Optional[endIndex])`

The `delete` function removes elements from a slice. It deletes elements in the range `[startIndex, endIndex)` (startIndex inclusive, endIndex exclusive). If `endIndex` is not provided, only the element at `target[startIndex]` is deleted. If `startIndex` equals `endIndex`, no changes are applied to the target, following the behavior of Go's `slices.Delete(s, i, j)` function.

Examples:

- `delete(attributes["tags"], 0)` # delete first

- `delete(attributes["tags"], Len(attributes["tags"]) - 1)` # delete last

- `delete(attributes["tags"], 0, 3)` # delete indices 0, 1, & 2

- `delete(attributes["tags"], Index(attributes["tags"], "unparsed"))` # delete first occurrence of "unparsed"

### delete_key

`delete_key(target, key)`
Expand Down
94 changes: 94 additions & 0 deletions pkg/ottl/ottlfuncs/func_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// 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"
"errors"
"fmt"

"go.opentelemetry.io/collector/pdata/pcommon"

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

type DeleteArguments[K any] struct {
Target ottl.PSliceGetSetter[K]
StartIndex ottl.IntGetter[K]
EndIndex ottl.Optional[ottl.IntGetter[K]]
}

func NewDeleteFactory[K any]() ottl.Factory[K] {
return ottl.NewFactory("delete", &DeleteArguments[K]{}, createDeleteFunction[K])
}

func createDeleteFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) {
args, ok := oArgs.(*DeleteArguments[K])
if !ok {
return nil, errors.New("DeleteFactory args must be of type *DeleteArguments[K]")
}

return deleteFrom(args.Target, args.StartIndex, args.EndIndex), nil
}

func deleteFrom[K any](target ottl.PSliceGetSetter[K], startIndexGetter ottl.IntGetter[K], endIndexGetter ottl.Optional[ottl.IntGetter[K]]) ottl.ExprFunc[K] {
return func(ctx context.Context, tCtx K) (any, error) {
t, err := target.Get(ctx, tCtx)
if err != nil {
return nil, err
}

startIndex, err := startIndexGetter.Get(ctx, tCtx)
if err != nil {
return nil, err
}

sliceLen := int64(t.Len())
endIndex := startIndex + 1
if !endIndexGetter.IsEmpty() {
endIndex, err = endIndexGetter.Get().Get(ctx, tCtx)
if err != nil {
return nil, err
}
}

if startIndex == 0 && endIndex == sliceLen {
// If deleting all elements, return an empty slice without looping
return nil, target.Set(ctx, tCtx, pcommon.NewSlice())
}

err = validateBounds(startIndex, endIndex, sliceLen)
if err != nil {
return nil, err
}

if startIndex == endIndex {
// No elements to delete
return nil, target.Set(ctx, tCtx, t)
}

var i int64
t.RemoveIf(func(_ pcommon.Value) bool {
remove := i >= startIndex && i < endIndex
i++
return remove
})
return nil, target.Set(ctx, tCtx, t)
}
}

func validateBounds(startIndex, endIndex, sliceLen int64) error {
if startIndex < 0 || startIndex >= sliceLen {
return fmt.Errorf("startIndex %d out of bounds for slice of length %d", startIndex, sliceLen)
}

if endIndex < startIndex {
return fmt.Errorf("endIndex %d cannot be less than startIndex %d", endIndex, startIndex)
}

if endIndex > sliceLen {
return fmt.Errorf("deletion range [%d:%d] out of bounds for slice of length %d", startIndex, endIndex, sliceLen)
}
return nil
}
Loading
Loading