Skip to content

[pkg/ottl] Add math expressions support to slices dynamic indexing #37644

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
edmocosta opened this issue Feb 3, 2025 · 4 comments · Fixed by #37745
Closed

[pkg/ottl] Add math expressions support to slices dynamic indexing #37644

edmocosta opened this issue Feb 3, 2025 · 4 comments · Fixed by #37745
Labels
enhancement New feature or request pkg/ottl priority:p2 Medium

Comments

@edmocosta
Copy link
Contributor

Component(s)

pkg/ottl

Is your feature request related to a problem? Please describe.

OTTL supports indexing maps and slices with dynamic values (#36644), allowing statements to access elements using any ottl.mathExprLiteral literal (Int, String, Converter, Path), which is great.

Maybe, It would be useful to extend the slice indexing support with math expressions, so we would be able to also access elements that needs some kind of computation. For example, to get the last slice's element, we can currently use:

log_statements:
 - context: log
   statements:
    - set(cache["last"], Len(attributes["array.attribute"]) - 1)
    - set(attributes["new"], attributes["array.attribute"][cache["last"]])

With math expression support, we could avoid using the cache and express the above statements as:

log_statements:
 - context: log
   statements:
    - set(attributes["new"], attributes["array.attribute"][Len(attributes["array.attribute"]) - 1)])

Describe the solution you'd like

--

Describe alternatives you've considered

Using cache, which also seems reasonable.

Additional context

No response

@edmocosta edmocosta added enhancement New feature or request needs triage New item requiring triage labels Feb 3, 2025
Copy link
Contributor

github-actions bot commented Feb 3, 2025

Pinging code owners:

See Adding Labels via Comments if you do not have permissions to add labels yourself.

@odubajDT
Copy link
Contributor

odubajDT commented Feb 4, 2025

Hey, I would like to look at this if it gets accepted

@TylerHelmuth
Copy link
Member

I support this idea

@odubajDT
Copy link
Contributor

odubajDT commented Feb 6, 2025

@edmocosta @TylerHelmuth created a draft PR with a working version.

Will add some more tests in the next days before marking it ready for review. If you have time, you can have a first look and also if you have ideas for statements you would like to have in E2E tests, it will be appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request pkg/ottl priority:p2 Medium
Projects
None yet
3 participants