Skip to content

Commit

Permalink
Use stdlib iterators (#6714)
Browse files Browse the repository at this point in the history
## Description of the changes
- Since we upgraded to Go 1.23 we can use standard "iter" package

## How was this change tested?
- CI

---------

Signed-off-by: Yuri Shkuro <[email protected]>
Signed-off-by: Yuri Shkuro <[email protected]>
  • Loading branch information
yurishkuro authored Feb 13, 2025
1 parent 42cd2b9 commit c0f5b4b
Show file tree
Hide file tree
Showing 22 changed files with 47 additions and 53 deletions.
8 changes: 0 additions & 8 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,6 @@ linters-settings:
files:
- "!**/v1adapter/**"

# TODO: remove once we have upgraded to Go 1.23
disallow-iter:
deny:
- pkg: iter
desc: "Use github.com/jaegertracing/jaeger/pkg/iter"
files:
- "**"

goimports:
local-prefixes: github.com/jaegertracing/jaeger
gosec:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import (
"go.uber.org/zap/zaptest"

"github.com/jaegertracing/jaeger/cmd/jaeger/internal/extension/jaegerstorage"
"github.com/jaegertracing/jaeger/internal/jiter"
"github.com/jaegertracing/jaeger/internal/storage/v1"
"github.com/jaegertracing/jaeger/internal/storage/v1/memory"
factoryMocks "github.com/jaegertracing/jaeger/internal/storage/v1/mocks"
"github.com/jaegertracing/jaeger/internal/storage/v2/api/tracestore"
tracestoreMocks "github.com/jaegertracing/jaeger/internal/storage/v2/api/tracestore/mocks"
"github.com/jaegertracing/jaeger/pkg/iter"
"github.com/jaegertracing/jaeger/pkg/otelsemconv"
)

Expand Down Expand Up @@ -152,7 +152,7 @@ func TestExporter(t *testing.T) {
getTracesIter := traceReader.GetTraces(ctx, tracestore.GetTraceParams{
TraceID: requiredTraceID,
})
requiredTrace, err := iter.FlattenWithErrors(getTracesIter)
requiredTrace, err := jiter.FlattenWithErrors(getTracesIter)
require.NoError(t, err)
resource := requiredTrace[0].ResourceSpans().At(0)
assert.Equal(t, spanID, resource.ScopeSpans().At(0).Spans().At(0).SpanID())
Expand Down
2 changes: 1 addition & 1 deletion cmd/jaeger/internal/integration/trace_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"io"
"iter"
"math"
"strings"

Expand All @@ -22,7 +23,6 @@ import (
"github.com/jaegertracing/jaeger/internal/storage/v1/api/spanstore"
"github.com/jaegertracing/jaeger/internal/storage/v2/api/tracestore"
"github.com/jaegertracing/jaeger/internal/storage/v2/v1adapter"
"github.com/jaegertracing/jaeger/pkg/iter"
"github.com/jaegertracing/jaeger/ports"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/query/app/apiv3/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"encoding/json"
"io"
"iter"
"net/http"
"os"
"path"
Expand All @@ -25,7 +26,6 @@ import (
"github.com/jaegertracing/jaeger/internal/storage/v2/api/tracestore"
tracestoremocks "github.com/jaegertracing/jaeger/internal/storage/v2/api/tracestore/mocks"
_ "github.com/jaegertracing/jaeger/pkg/gogocodec" // force gogo codec registration
"github.com/jaegertracing/jaeger/pkg/iter"
)

// Utility functions used from http_gateway_test.go.
Expand Down
2 changes: 1 addition & 1 deletion cmd/query/app/apiv3/grpc_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"errors"
"fmt"
"iter"

"go.opentelemetry.io/collector/pdata/ptrace"
"google.golang.org/grpc/codes"
Expand All @@ -17,7 +18,6 @@ import (
"github.com/jaegertracing/jaeger/internal/proto/api_v3"
"github.com/jaegertracing/jaeger/internal/storage/v2/api/tracestore"
"github.com/jaegertracing/jaeger/internal/storage/v2/v1adapter"
"github.com/jaegertracing/jaeger/pkg/iter"
)

// Handler implements api_v3.QueryServiceServer
Expand Down
2 changes: 1 addition & 1 deletion cmd/query/app/apiv3/grpc_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package apiv3

import (
"context"
"iter"
"net"
"testing"
"time"
Expand All @@ -22,7 +23,6 @@ import (
"github.com/jaegertracing/jaeger/internal/storage/v2/api/tracestore"
tracestoremocks "github.com/jaegertracing/jaeger/internal/storage/v2/api/tracestore/mocks"
_ "github.com/jaegertracing/jaeger/pkg/gogocodec" // force gogo codec registration
"github.com/jaegertracing/jaeger/pkg/iter"
)

var matchContext = mock.AnythingOfType("*context.valueCtx")
Expand Down
6 changes: 3 additions & 3 deletions cmd/query/app/apiv3/http_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import (

"github.com/jaegertracing/jaeger-idl/model/v1"
"github.com/jaegertracing/jaeger/cmd/query/app/querysvc/v2/querysvc"
"github.com/jaegertracing/jaeger/internal/jiter"
"github.com/jaegertracing/jaeger/internal/proto/api_v3"
"github.com/jaegertracing/jaeger/internal/storage/v1/api/spanstore"
"github.com/jaegertracing/jaeger/internal/storage/v2/api/tracestore"
"github.com/jaegertracing/jaeger/internal/storage/v2/v1adapter"
"github.com/jaegertracing/jaeger/pkg/iter"
)

const (
Expand Down Expand Up @@ -191,7 +191,7 @@ func (h *HTTPGateway) getTrace(w http.ResponseWriter, r *http.Request) {
request.RawTraces = rawTraces
}
getTracesIter := h.QueryService.GetTraces(r.Context(), request)
trc, err := iter.FlattenWithErrors(getTracesIter)
trc, err := jiter.FlattenWithErrors(getTracesIter)
h.returnTraces(trc, err, w)
}

Expand All @@ -202,7 +202,7 @@ func (h *HTTPGateway) findTraces(w http.ResponseWriter, r *http.Request) {
}

findTracesIter := h.QueryService.FindTraces(r.Context(), *queryParams)
traces, err := iter.FlattenWithErrors(findTracesIter)
traces, err := jiter.FlattenWithErrors(findTracesIter)
h.returnTraces(traces, err, w)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/query/app/apiv3/http_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package apiv3
import (
"errors"
"fmt"
"iter"
"net/http"
"net/http/httptest"
"net/url"
Expand All @@ -24,7 +25,6 @@ import (
dependencyStoreMocks "github.com/jaegertracing/jaeger/internal/storage/v2/api/depstore/mocks"
"github.com/jaegertracing/jaeger/internal/storage/v2/api/tracestore"
tracestoremocks "github.com/jaegertracing/jaeger/internal/storage/v2/api/tracestore/mocks"
"github.com/jaegertracing/jaeger/pkg/iter"
"github.com/jaegertracing/jaeger/pkg/jtracer"
"github.com/jaegertracing/jaeger/pkg/testutils"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/query/app/querysvc/v2/querysvc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package querysvc
import (
"context"
"errors"
"iter"
"time"

"go.opentelemetry.io/collector/pdata/pcommon"
Expand All @@ -16,7 +17,6 @@ import (
"github.com/jaegertracing/jaeger/internal/jptrace"
"github.com/jaegertracing/jaeger/internal/storage/v2/api/depstore"
"github.com/jaegertracing/jaeger/internal/storage/v2/api/tracestore"
"github.com/jaegertracing/jaeger/pkg/iter"
)

var errNoArchiveSpanStorage = errors.New("archive span storage was not configured")
Expand Down
17 changes: 9 additions & 8 deletions cmd/query/app/querysvc/v2/querysvc/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package querysvc
import (
"context"
"fmt"
"iter"
"testing"
"time"

Expand All @@ -17,11 +18,11 @@ import (

"github.com/jaegertracing/jaeger-idl/model/v1"
"github.com/jaegertracing/jaeger/cmd/query/app/querysvc/v2/adjuster"
"github.com/jaegertracing/jaeger/internal/jiter"
"github.com/jaegertracing/jaeger/internal/storage/v2/api/depstore"
depstoremocks "github.com/jaegertracing/jaeger/internal/storage/v2/api/depstore/mocks"
"github.com/jaegertracing/jaeger/internal/storage/v2/api/tracestore"
tracestoremocks "github.com/jaegertracing/jaeger/internal/storage/v2/api/tracestore/mocks"
"github.com/jaegertracing/jaeger/pkg/iter"
)

const millisToNanosMultiplier = int64(time.Millisecond / time.Nanosecond)
Expand Down Expand Up @@ -109,7 +110,7 @@ func TestGetTraces_ErrorInReader(t *testing.T) {
},
}
getTracesIter := tqs.queryService.GetTraces(context.Background(), params)
_, err := iter.FlattenWithErrors(getTracesIter)
_, err := jiter.FlattenWithErrors(getTracesIter)
require.ErrorIs(t, err, assert.AnError)
}

Expand All @@ -126,7 +127,7 @@ func TestGetTraces_Success(t *testing.T) {
},
}
getTracesIter := tqs.queryService.GetTraces(context.Background(), params)
gotTraces, err := iter.FlattenWithErrors(getTracesIter)
gotTraces, err := jiter.FlattenWithErrors(getTracesIter)
require.NoError(t, err)
require.Len(t, gotTraces, 1)

Expand Down Expand Up @@ -198,7 +199,7 @@ func TestGetTraces_WithRawTraces(t *testing.T) {
RawTraces: test.rawTraces,
}
getTracesIter := tqs.queryService.GetTraces(context.Background(), params)
gotTraces, err := iter.FlattenWithErrors(getTracesIter)
gotTraces, err := jiter.FlattenWithErrors(getTracesIter)
require.NoError(t, err)

require.Len(t, gotTraces, 1)
Expand Down Expand Up @@ -227,7 +228,7 @@ func TestGetTraces_TraceInArchiveStorage(t *testing.T) {
},
}
getTracesIter := tqs.queryService.GetTraces(context.Background(), params)
gotTraces, err := iter.FlattenWithErrors(getTracesIter)
gotTraces, err := jiter.FlattenWithErrors(getTracesIter)
require.NoError(t, err)
require.Len(t, gotTraces, 1)

Expand Down Expand Up @@ -283,7 +284,7 @@ func TestFindTraces_Success(t *testing.T) {

query := TraceQueryParams{TraceQueryParams: queryParams}
getTracesIter := tqs.queryService.FindTraces(context.Background(), query)
gotTraces, err := iter.FlattenWithErrors(getTracesIter)
gotTraces, err := jiter.FlattenWithErrors(getTracesIter)
require.NoError(t, err)
require.Len(t, gotTraces, 1)

Expand Down Expand Up @@ -366,7 +367,7 @@ func TestFindTraces_WithRawTraces_PerformsAdjustment(t *testing.T) {
RawTraces: test.rawTraces,
}
getTracesIter := tqs.queryService.FindTraces(context.Background(), query)
gotTraces, err := iter.FlattenWithErrors(getTracesIter)
gotTraces, err := jiter.FlattenWithErrors(getTracesIter)
require.NoError(t, err)

require.Len(t, gotTraces, 1)
Expand Down Expand Up @@ -502,7 +503,7 @@ func TestFindTraces_WithRawTraces_PerformsAggregation(t *testing.T) {
RawTraces: test.rawTraces,
}
getTracesIter := tqs.queryService.FindTraces(context.Background(), query)
gotTraces, err := iter.FlattenWithErrors(getTracesIter)
gotTraces, err := jiter.FlattenWithErrors(getTracesIter)
require.NoError(t, err)
assert.Equal(t, test.expected, gotTraces)
assert.Equal(t, test.expectedAdjustCalls, adjustCalls)
Expand Down
11 changes: 5 additions & 6 deletions pkg/iter/iter.go → internal/jiter/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
// SPDX-License-Identifier: Apache-2.0

// Package iter is a backport of Go 1.23 official "iter" package, until we upgrade.
package iter
package jiter

type (
Seq[V any] func(yield func(V) bool)
Seq2[K, V any] func(yield func(K, V) bool)
import (
"iter"
)

func CollectWithErrors[V any](seq Seq2[V, error]) ([]V, error) {
func CollectWithErrors[V any](seq iter.Seq2[V, error]) ([]V, error) {
var result []V
var err error
seq(func(v V, e error) bool {
Expand All @@ -26,7 +25,7 @@ func CollectWithErrors[V any](seq Seq2[V, error]) ([]V, error) {
return result, nil
}

func FlattenWithErrors[V any](seq Seq2[[]V, error]) ([]V, error) {
func FlattenWithErrors[V any](seq iter.Seq2[[]V, error]) ([]V, error) {
var result []V
var err error
seq(func(v []V, e error) bool {
Expand Down
7 changes: 4 additions & 3 deletions pkg/iter/iter_test.go → internal/jiter/iter_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright (c) 2024 The Jaeger Authors.
// SPDX-License-Identifier: Apache-2.0

package iter
package jiter

import (
"iter"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -13,7 +14,7 @@ import (
func TestCollectWithErrors(t *testing.T) {
tests := []struct {
name string
seq Seq2[string, error]
seq iter.Seq2[string, error]
expected []string
err error
}{
Expand Down Expand Up @@ -70,7 +71,7 @@ func TestCollectWithErrors(t *testing.T) {
func TestFlattenWithErrors(t *testing.T) {
tests := []struct {
name string
seq Seq2[[]string, error]
seq iter.Seq2[[]string, error]
expected []string
err error
}{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2024 The Jaeger Authors.
// SPDX-License-Identifier: Apache-2.0

package iter
package jiter

import (
"testing"
Expand Down
4 changes: 2 additions & 2 deletions internal/jptrace/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
package jptrace

import (
"iter"

"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"

"github.com/jaegertracing/jaeger/pkg/iter"
)

// AggregateTraces aggregates a sequence of trace batches into individual traces.
Expand Down
4 changes: 2 additions & 2 deletions internal/jptrace/spaniter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
package jptrace

import (
"go.opentelemetry.io/collector/pdata/ptrace"
"iter"

"github.com/jaegertracing/jaeger/pkg/iter"
"go.opentelemetry.io/collector/pdata/ptrace"
)

type SpanIterPos struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/storage/v2/api/tracestore/mocks/Reader.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/storage/v2/api/tracestore/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ package tracestore

import (
"context"
"iter"
"time"

"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"

"github.com/jaegertracing/jaeger/internal/storage/v1/api/spanstore"
"github.com/jaegertracing/jaeger/pkg/iter"
)

// Reader finds and loads traces and other data from storage.
Expand Down
2 changes: 1 addition & 1 deletion internal/storage/v2/v1adapter/spanreader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package v1adapter

import (
"context"
"iter"
"testing"
"time"

Expand All @@ -18,7 +19,6 @@ import (
"github.com/jaegertracing/jaeger/internal/storage/v1/api/spanstore"
"github.com/jaegertracing/jaeger/internal/storage/v2/api/tracestore"
tracestoremocks "github.com/jaegertracing/jaeger/internal/storage/v2/api/tracestore/mocks"
"github.com/jaegertracing/jaeger/pkg/iter"
)

func TestSpanReader_GetTrace(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion internal/storage/v2/v1adapter/tracereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package v1adapter
import (
"context"
"errors"
"iter"

"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
Expand All @@ -15,7 +16,6 @@ import (
"github.com/jaegertracing/jaeger/internal/storage/v1/api/spanstore"
"github.com/jaegertracing/jaeger/internal/storage/v2/api/depstore"
"github.com/jaegertracing/jaeger/internal/storage/v2/api/tracestore"
"github.com/jaegertracing/jaeger/pkg/iter"
)

var _ tracestore.Reader = (*TraceReader)(nil)
Expand Down
Loading

0 comments on commit c0f5b4b

Please sign in to comment.