Skip to content

[pkg/ottl] Allow indexing any slice type #29441

@TylerHelmuth

Description

@TylerHelmuth

I was just shown the scenario

set(resource.attributes["my.environment.2"], Split(resource.attributes["host.name"],"-")[1])

that produces an error:

{"kind": "processor", "name": "transform", "pipeline": "logs", "error": "type, []string, does not support int indexing", "statement": "set(resource.attributes[\"my.environment.2\"], Split(resource.attributes[\"host.name\"],\"-\")[1])"}

Although the language does support indexing the return value of the Converter, it only supports indexing pcommon.Slice and []any, but Split returns a []string. We need to make indexing slices more generic. This might need to be its own issue. Related code:

case k.Int != nil:
switch r := result.(type) {
case pcommon.Slice:
if int(*k.Int) >= r.Len() || int(*k.Int) < 0 {
return nil, fmt.Errorf("index %v out of bounds", *k.Int)
}
result = ottlcommon.GetValue(r.At(int(*k.Int)))
case []any:
if int(*k.Int) >= len(r) || int(*k.Int) < 0 {
return nil, fmt.Errorf("index %v out of bounds", *k.Int)
}
result = r[*k.Int]
default:
return nil, fmt.Errorf("type, %T, does not support int indexing", result)
}

Originally posted by @TylerHelmuth in #26108 (comment)

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinggood first issueGood for newcomershelp wantedExtra attention is needednever staleIssues marked with this label will be never staled and automatically removedpkg/ottlpriority:p2Medium

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions