Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions internal/cursorediterator/primitives.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package cursorediterator

import (
"context"
"iter"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"

"github.com/authzed/spicedb/internal/telemetry/otelconv"
)

// join combines multiple iterator sequences into one.
Expand Down Expand Up @@ -60,3 +66,32 @@
callback(count)
}
}

// Spanned wraps an iterator sequence, recording any errors to the provided span,
// and ending the span when the iteration is complete.
func Spanned[I any](
ctx context.Context,
source iter.Seq2[I, error],
tracer trace.Tracer,
spanName string,
spanAttrs ...attribute.KeyValue,
) iter.Seq2[I, error] {
return func(yield func(I, error) bool) {
_, span := tracer.Start(ctx, spanName, trace.WithAttributes(spanAttrs...))
defer span.End()

itemCount := 0
for item, err := range source {
if err != nil {
span.RecordError(err)
}

Check warning on line 87 in internal/cursorediterator/primitives.go

View check run for this annotation

Codecov / codecov/patch

internal/cursorediterator/primitives.go#L78-L87

Added lines #L78 - L87 were not covered by tests

itemCount++
if !yield(item, err) {
break

Check warning on line 91 in internal/cursorediterator/primitives.go

View check run for this annotation

Codecov / codecov/patch

internal/cursorediterator/primitives.go#L89-L91

Added lines #L89 - L91 were not covered by tests
}
}

span.SetAttributes(attribute.Int(otelconv.AttrIteratorItemCount, itemCount))

Check warning on line 95 in internal/cursorediterator/primitives.go

View check run for this annotation

Codecov / codecov/patch

internal/cursorediterator/primitives.go#L95

Added line #L95 was not covered by tests
}
}
Loading
Loading