Skip to content

Conversation

@alexcams
Copy link

@alexcams alexcams commented Nov 5, 2025

Description

Added new ottl function delete(target, index, Optional[length]

The delete function deletes elements starting at index to index + length from target array/slice. If lengh is not provided, only element at target[index] will be deleted. If index is calculated using Index(target, value) and there's no match in the slice, target array won't be changed.

It accepts any OTTL array type, including arrays with different type of data.
Implemented this as a function modifying target rather than converter returning merged arrays
This implementation doesn't delete duplicate values, could be added in the future or implemented as a separated function.

Link to tracking issue

Closes #43098

Testing

Added unit tests with intended behavior.
Tested manually with following config:

receivers:
  filelog:
    include: [ test.log ]
    start_at: beginning

exporters:
  debug:
    verbosity: detailed
    sampling_initial: 10000
    sampling_thereafter: 10000

processors:
  transform:
    error_mode: ignore
    log_statements:
      - context: log
        statements: 
          - set(attributes["test"], [[1, 2, 3, 4]]) # init slice with nested slice
          - append(attributes["test"], values = ["val", 1, true, 3.14, "foo", "foo", 2, false, 6.28]) # append multiple values
          - set(attributes["test_copy"], attributes["test"]) # make a copy 
          - delete(attributes["test"], 1) # delete "val"
          - delete(attributes["test"], Len(attributes["test"]) - 1) # delete last element (6,28)
          - delete(attributes["test"], Index(attributes["test"], "foo")) # delete "foo" first instance
          - delete(attributes["test_copy"], 0, length=4) # delete first 4 elements from copy

service:
  telemetry:
    metrics:
      level: none
  pipelines:
    logs:
      receivers: [filelog]
      processors: [transform]
      exporters:
        - debug

Output:

Body: Str(2025-11-05 10:00:00 INFO Test log message)
Attributes:
     -> log.file.name: Str(test.log)
     -> test: Slice([[1,2,3,4],1,true,3.14,"foo",2,false])
     -> test_copy: Slice([3.14,"foo","foo",2,false,6.28])

Documentation

Updated README.md

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Nov 5, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@github-actions github-actions bot added the first-time contributor PRs made by new contributors label Nov 5, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Nov 5, 2025

Welcome, contributor! Thank you for your contribution to opentelemetry-collector-contrib.

Important reminders:

A maintainer will review your pull request soon. Thank you for helping make OpenTelemetry better!

@alexcams alexcams marked this pull request as draft November 5, 2025 08:49
@alexcams alexcams force-pushed the add-ottl-delete-elemelt-function branch from 25a241a to fe95cef Compare November 5, 2025 09:48
@alexcams alexcams marked this pull request as ready for review November 5, 2025 09:48
@alexcams
Copy link
Author

alexcams commented Nov 5, 2025

Last commit updates the first implemented algorithm to a new one that avoids the use of AsRaw() and FromRaw() functions, both of cost O(N). Instead it is replaces by a single loop O(N) and preallocating memory for the result slice.
Here's a comparative between both algorithms showing that the second one reduces time and memory spent for different scenarios by 50% (at least).
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

first-time contributor PRs made by new contributors pkg/ottl

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[pkg/ottl] add a function to remove an element from a Slice/Array

3 participants