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
32 changes: 32 additions & 0 deletions output/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,35 @@ func (a *writerAsMetricConsumer) ConsumeMetrics(ctx context.Context, points []em
}
return nil
}

// WriterAsTraceConsumer wraps a TraceWriter so it can be used in
// contexts that expect an embed.TraceConsumer. The adapter pushes each
// span in the batch through TraceWriter.WriteTrace in order, returning
// the first error it encounters.
//
// Standalone CLI trace-generator wiring uses this adapter to bridge
// modules that talk to embed.TraceConsumer with existing outputs that
// implement TraceWriter.
//
// Panics on nil writer — a nil writer is a programming bug, not a
// runtime condition, and catching it at construction surfaces the
// failure at the boundary rather than deep in ConsumeTraces.
func WriterAsTraceConsumer(w TraceWriter) embed.TraceConsumer {
if w == nil {
panic("output.WriterAsTraceConsumer: writer cannot be nil")
}
return &writerAsTraceConsumer{w: w}
}
Comment on lines +83 to +88

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nil writer not guarded in constructor. Calling adapter with nil TraceWriter panics on WriteTrace

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks. Added a nil-guard panic at construction time in WriterAsTraceConsumer with a clear message, plus a unit test for the panic path. Also applied the same symmetric fix to WriterAsLogConsumer (identical to the one in #229's branch) so the protection lands on main whichever stack merges first — patch-id should drop the duplicate when the second one rebases.


type writerAsTraceConsumer struct {
w TraceWriter
}

func (a *writerAsTraceConsumer) ConsumeTraces(ctx context.Context, spans []embed.Span) error {
for i := range spans {
if err := a.w.WriteTrace(ctx, spans[i]); err != nil {
return err
}
}
return nil
}
61 changes: 61 additions & 0 deletions output/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,67 @@ func TestWriterAsMetricConsumerStopsOnFirstError(t *testing.T) {
}
}

type recordingTraceWriter struct {
mu sync.Mutex
spans []output.TraceRecord
err error
}

func (w *recordingTraceWriter) WriteTrace(_ context.Context, rec output.TraceRecord) error {
w.mu.Lock()
defer w.mu.Unlock()
if w.err != nil {
return w.err
}
w.spans = append(w.spans, rec)
return nil
}

func TestWriterAsTraceConsumerPushesEachSpan(t *testing.T) {
w := &recordingTraceWriter{}
c := output.WriterAsTraceConsumer(w)

batch := []embed.Span{
{Name: "one"},
{Name: "two"},
{Name: "three"},
}
if err := c.ConsumeTraces(context.Background(), batch); err != nil {
t.Fatalf("ConsumeTraces: %v", err)
}
if got, want := len(w.spans), 3; got != want {
t.Fatalf("recorded %d, want %d", got, want)
}
for i, want := range []string{"one", "two", "three"} {
if w.spans[i].Name != want {
t.Errorf("span %d: %q, want %q", i, w.spans[i].Name, want)
}
}
}

func TestWriterAsTraceConsumerStopsOnFirstError(t *testing.T) {
wantErr := errors.New("trace boom")
w := &recordingTraceWriter{err: wantErr}
c := output.WriterAsTraceConsumer(w)

err := c.ConsumeTraces(context.Background(), []embed.Span{
{Name: "one"},
{Name: "two"},
})
if !errors.Is(err, wantErr) {
t.Fatalf("expected wantErr, got %v", err)
}
}

func TestWriterAsTraceConsumerPanicsOnNilWriter(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatal("expected panic on nil writer, got none")
}
}()
_ = output.WriterAsTraceConsumer(nil)
}

func TestWriterAsLogConsumerPanicsOnNilWriter(t *testing.T) {
defer func() {
if r := recover(); r == nil {
Expand Down
Loading