open-telemetry / opentelemetry-collector-contrib

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

[pkg/ottl] OTTL Sprintf Function #33405

Closed sinkingpoint closed 1 month ago

sinkingpoint commented 3 months ago

Component(s)

pkg/ottl

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

When using OTTL, it's often useful to be able to construct strings in various ways. We do have a few ways of doing this already (e.g. Concat), but nothing as flexible as a generic Sprintf. For example, we have no way of padding ints (%02d in Sprintf)

Describe the solution you'd like

I would like to introduce a new OTTL function, Sprintf which takes a format string, and an array of fields, and calls fmt.Sprintf with those arguments.

This would require introducing a new Getter which gets a generic interface{}, rather than the existing Getters which return concrete types.

Describe alternatives you've considered

We can sort of get there with creative uses of Concat, but this quickly becomes unruly with complex definitions. e.g. this is one we have:

Concat([attributes["appname"], attributes["hostname"], Concat([Year(Now()), Month(Now()), Day(Now())], "-"), Concat(["service-", Hour(Now()), ".log"], "")], "/")

Which would look like:

Sprintf("%s/%s/%d-%d-%d/service-%d.log", attributes["appname"], attributes["hostname"], Year(Now()), Month(Now()), Day(Now()), Hour(Now()))

Additional context

No response

github-actions[bot] commented 3 months ago

Pinging code owners:

evan-bradley commented 3 months ago

I think this makes sense. It should be fairly easy to implement, we already have a untyped getter (ottl.Getter).

github-actions[bot] commented 1 month ago

This issue has been inactive for 60 days. It will be closed in 60 days if there is no activity. To ping code owners by adding a component label, see Adding Labels via Comments, or if you are unsure of which component this issue relates to, please ping @open-telemetry/collector-contrib-triagers. If this issue is still relevant, please ping the code owners or leave a comment explaining why it is still relevant. Otherwise, please close it.

Pinging code owners:

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