Skip to content

[pdata] Implement Go 1.23 style iterators #11982

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
yurishkuro opened this issue Dec 25, 2024 · 4 comments · Fixed by #12380
Closed

[pdata] Implement Go 1.23 style iterators #11982

yurishkuro opened this issue Dec 25, 2024 · 4 comments · Fixed by #12380
Assignees

Comments

@yurishkuro
Copy link
Member

yurishkuro commented Dec 25, 2024

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

Iterating through pdata structures today is pretty convoluted, e.g.

for i := 0; i < traces.ResourceSpans().Len(); i++ {
	resource := traces.ResourceSpans().At(i)
	for j := 0; j < resource.ScopeSpans().Len(); j++ {
		scope := resource.ScopeSpans().At(j)
		for k := 0; k < scope.Spans().Len(); k++ {
			span := scope.Spans().At(k)

Describe the solution you'd like
Go 1.23 introduced the ability to use iterators for looping through custom structs. The above code could look like this, if we introduce iterator functions to pdata, e.g. Seq() and Seq2():

for resource := range traces.ResourceSpans().Seq() {
	for scope := range resource.ScopeSpans().Seq() {
		for span := range scope.Spans().Seq() {
for i, resource := range traces.ResourceSpans().Seq2() {
	for j, scope := range resource.ScopeSpans().Seq2() {
		for k, span := range scope.Spans().Seq2() {
@bogdandrutu
Copy link
Member

@yurishkuro good call, will do when we move to minimum supported version to be 1.23

@mauri870
Copy link
Contributor

mauri870 commented Jan 2, 2025

I think this is a perfect use case for iterators, the way this is implemented today is quite verbose to write and almost every single use case requires looping over the entries in some way.

I suggest we go with an All method, following the api signature for the already existing maps and slices counterparts.

Since this involves changes in autogenerated code, there might be oportunities for reusing existing iterator routines from the standard library, for example ResourceSpans uses an slice internally.

@mauri870
Copy link
Contributor

I started working on this main...mauri870:opentelemetry-collector:pdata-iterator-all. It requires Go 1.23 so we cannot accept it until all the other collector packages are bumped to 1.23.

@mauri870
Copy link
Contributor

Now that the ecosystem moved to Go 1.23 I will resume working on this. I should have a PR up this week.

github-merge-queue bot pushed a commit that referenced this issue Mar 14, 2025
#### Description

This PR adds support for iterators to Slice and Map types and their
autogenerated counterparts. Iterators were stabilized in Go 1.23.

The All method is analogous to [maps.All](https://pkg.go.dev/maps#All)
and [slices.All ](https://pkg.go.dev/slices#All) and is a more idiomatic
alternative to the more verbose `for i := 0; i < es.Len(); i++ { z :=
es.At(i) }` way of looping.

Code that is written like this today:

```go
for i := 0; i < ld.ResourceLogs().Len(); i++ {
        rl := ld.ResourceLogs().At(i)
        for j := 0; j < rl.ScopeLogs().Len(); j++ {
                sl := rl.ScopeLogs().At(j)
                for k := 0; k < sl.LogRecords().Len(); k++ {
                        lr := sl.LogRecords().At(k)
                        // use lr
                }
        }
}
```

Can now be written like this:

```go
for _, rl := range ld.ResourceLogs().All() {
        for _, sl := range rl.ScopeLogs().All() {
                for _, lr := range sl.LogRecords().All() {
                        // use lr
                }
        }
}
```

#### Link to tracking issue

Fixes
#11982
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants