Skip to content
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

[pdata] Implement Go 1.23 style iterators #11982

Open
yurishkuro opened this issue Dec 25, 2024 · 2 comments
Open

[pdata] Implement Go 1.23 style iterators #11982

yurishkuro opened this issue Dec 25, 2024 · 2 comments

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

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.

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

No branches or pull requests

3 participants