open-telemetry / opentelemetry-collector-contrib

Contrib repository for the OpenTelemetry Collector
https://opentelemetry.io
Apache License 2.0
2.88k stars 2.25k forks source link

[pkg/ottl] Add Sort converter sorting array value #34200

Open kaisecheng opened 1 month ago

kaisecheng commented 1 month ago

Component(s)

pkg/ottl

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

I wanna sort an array in attribute in ascending or descending order to improve readability and simplify data processing.

Describe the solution you'd like

The Sort() function should accept an input array and return a new array sorted in either ascending or descending order.

Sort(attributes["device.tags"], "asc")
Sort(attributes["device.tags"], "desc")

The function should support sorting arrays of different types, including integers, floats, strings, and booleans. For boolean arrays, elements will be converted to strings for comparison. For arrays containing mixed types (e.g., integers and strings), all elements will be converted to strings for comparison.

Describe alternatives you've considered

No response

Additional context

No response

github-actions[bot] commented 1 month ago

Pinging code owners:

kaisecheng commented 1 month ago

@evan-bradley I can work on this feature. Do you think the description is clear enough to start?

evan-bradley commented 1 month ago

This makes sense to me. @TylerHelmuth let us know if you have any concerns.

TylerHelmuth commented 1 month ago

The values will only be compared as strings, but not actually changed, right?

kaisecheng commented 1 month ago

The values will only be compared as strings, but not actually changed, right?

Yes. The underlying values are not changed. Sort() makes a copy. Numeric array compare as numbers. Bools, strings and mixed types compare as strings. Other types are returned as-is

TylerHelmuth commented 1 month ago

Ok cool. I'd like to see a PR for this feature.