-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Add OTTL delete function #44053
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
Merged
evan-bradley
merged 16 commits into
open-telemetry:main
from
alexcams:add-ottl-delete-elemelt-function
Jan 26, 2026
+534
−0
Merged
Add OTTL delete function #44053
Changes from 10 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
fe1283c
Add OTTL delete function to delete items from an array
alexcams 9af7728
Improved delete function performance and memory consumption
alexcams 4954198
Solved linting errors
alexcams e74f2dd
Solved comments to return out of bound errors
alexcams 316040b
Changed target type to 'PSliceGetSetter'
alexcams fb301d8
Use 2 different loops to avoid useless iterations
alexcams 393fa27
Merge branch 'main' into add-ottl-delete-elemelt-function
alexcams ed55e87
make generate-chloggen-components
alexcams e3262c8
changed implementation to follow slices.Delete() logic and solved com…
alexcams 8f0c4e0
Merge branch 'main' into add-ottl-delete-elemelt-function
alexcams b27591a
updated implementation to use RemoveIf over the original slices to sa…
alexcams 27aab47
Update pkg/ottl/ottlfuncs/README.md
alexcams b4e9ca1
Renamed function to 'delete_index'
alexcams a8a863c
Renamed files to proper function name
alexcams 9e49eb7
Merge branch 'main' into add-ottl-delete-elemelt-function
alexcams 85ba8d6
Merge branch 'main' into add-ottl-delete-elemelt-function
evan-bradley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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. 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] |
This file contains hidden or 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 hidden or 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 hidden or 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,99 @@ | ||
| // 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) | ||
| } | ||
|
|
||
| resSlice := pcommon.NewSlice() | ||
| resSlice.EnsureCapacity(int(sliceLen - (endIndex - startIndex))) | ||
|
|
||
| for i := range startIndex { | ||
| t.At(int(i)).MoveTo(resSlice.AppendEmpty()) | ||
| } | ||
|
|
||
| for i := endIndex; i < sliceLen; i++ { | ||
| t.At(int(i)).MoveTo(resSlice.AppendEmpty()) | ||
| } | ||
|
|
||
| return nil, target.Set(ctx, tCtx, resSlice) | ||
edmocosta marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| 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 | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.