Skip to content

Commit

Permalink
Fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-sili committed Feb 12, 2025
1 parent 98a9e7c commit 87f5b85
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion exporter/exporterhelper/internal/batch_sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ func TestBatchSender_BatchCancelled(t *testing.T) {
require.NoError(t, be.Shutdown(context.Background()))
})
}
runTest("enable_queue_batcher", true)
// When queue_batcher is enabled, we don't cancel the whole batch when the first request is canceled.
runTest("disable_queue_batcher", false)
}

Expand Down
27 changes: 16 additions & 11 deletions exporter/exporterhelper/internal/batcher/merged_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,41 @@ import (

// mergedContext links the underlying context to all incoming span contexts.
type mergedContext struct {
deadline time.Time
deadlineOk bool
ctx context.Context
deadline time.Time
deadlineOk bool
deadlineCtx context.Context
ctx context.Context
}

func NewMergedContext(ctx context.Context) mergedContext {
deadline, ok := ctx.Deadline()
return mergedContext{
deadline: deadline,
deadlineOk: ok,
ctx: ctx,
deadline: deadline,
deadlineOk: ok,
deadlineCtx: ctx,
ctx: ctx,
}
}

// Merge links the span from incoming context to the span from the first context.
func (mc *mergedContext) Merge(other context.Context) mergedContext {
deadline, deadlineOk := mc.Deadline()
ctx := mc.deadlineCtx
if otherDeadline, ok := other.Deadline(); ok {
deadlineOk = true
if deadline.Before(otherDeadline) {
deadline = otherDeadline
ctx = other
}
}

link := trace.LinkFromContext(other)
trace.SpanFromContext(mc.ctx).AddLink(link)
return mergedContext{
deadline: deadline,
deadlineOk: deadlineOk,
ctx: mc.ctx,
deadline: deadline,
deadlineOk: deadlineOk,
deadlineCtx: ctx,
ctx: mc.ctx,
}
}

Expand All @@ -50,11 +55,11 @@ func (mc mergedContext) Deadline() (time.Time, bool) {
}

func (mc mergedContext) Done() <-chan struct{} {
return nil
return mc.deadlineCtx.Done()
}

func (mc mergedContext) Err() error {
return nil
return mc.deadlineCtx.Err()
}

// Value delegates to the first context.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ func TestMergedContextDeadline(t *testing.T) {
deadline, ok = mergedContext.Deadline()
require.True(t, ok)
require.Equal(t, now.Add(300), deadline)

time.Sleep(300)
require.Equal(t, ctx3.Err(), mergedContext.Err())
}

func TestMergedContextLink(t *testing.T) {
tracerProvider := componenttest.NewTelemetry().NewTelemetrySettings().TracerProvider
tracer := tracerProvider.Tracer("go.opentelemetry.io/collector/exporter/exporterhelper")

ctx1 := context.WithValue(context.Background(), "key", "value")
ctx1 := context.Background()
ctx2, span2 := tracer.Start(ctx1, "span2")
defer span2.End()

Expand Down

0 comments on commit 87f5b85

Please sign in to comment.