Skip to content

Commit 441c274

Browse files
authored
Pass Context through span processors (#6534)
## Which problem is this PR solving? - Context is lost in the process, see #6491 (comment) ## Description of the changes - Add Context argument to handler and span processor methods ## How was this change tested? - CI --------- Signed-off-by: Yuri Shkuro <github@ysh.us>
1 parent f60bd09 commit 441c274

File tree

11 files changed

+47
-33
lines changed

11 files changed

+47
-33
lines changed

cmd/collector/app/collector_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ func TestAggregator(t *testing.T) {
263263
},
264264
},
265265
}
266-
_, err := c.spanProcessor.ProcessSpans(processor.SpansV1{
266+
_, err := c.spanProcessor.ProcessSpans(context.Background(), processor.SpansV1{
267267
Spans: spans,
268268
Details: processor.Details{
269269
SpanFormat: processor.JaegerSpanFormat,

cmd/collector/app/handler/grpc_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (c *batchConsumer) consume(ctx context.Context, batch *model.Batch) error {
7575
span.Process = batch.Process
7676
}
7777
}
78-
_, err = c.spanProcessor.ProcessSpans(processor.SpansV1{
78+
_, err = c.spanProcessor.ProcessSpans(ctx, processor.SpansV1{
7979
Spans: batch.Spans,
8080
Details: processor.Details{
8181
InboundTransport: c.spanOptions.InboundTransport,

cmd/collector/app/handler/grpc_handler_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
"github.com/jaegertracing/jaeger/proto-gen/api_v2"
2626
)
2727

28+
var _ processor.SpanProcessor = (*mockSpanProcessor)(nil)
29+
2830
type mockSpanProcessor struct {
2931
expectedError error
3032
mux sync.Mutex
@@ -34,7 +36,7 @@ type mockSpanProcessor struct {
3436
spanFormat processor.SpanFormat
3537
}
3638

37-
func (p *mockSpanProcessor) ProcessSpans(batch processor.Batch) ([]bool, error) {
39+
func (p *mockSpanProcessor) ProcessSpans(_ context.Context, batch processor.Batch) ([]bool, error) {
3840
p.mux.Lock()
3941
defer p.mux.Unlock()
4042
batch.GetSpans(func(spans []*model.Span) {

cmd/collector/app/handler/http_thrift_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (aH *APIHandler) SaveSpan(w http.ResponseWriter, r *http.Request) {
7575
}
7676
batches := []*tJaeger.Batch{batch}
7777
opts := SubmitBatchOptions{InboundTransport: processor.HTTPTransport}
78-
if _, err = aH.jaegerBatchesHandler.SubmitBatches(batches, opts); err != nil {
78+
if _, err = aH.jaegerBatchesHandler.SubmitBatches(r.Context(), batches, opts); err != nil {
7979
http.Error(w, fmt.Sprintf("Cannot submit Jaeger batch: %v", err), http.StatusInternalServerError)
8080
return
8181
}

cmd/collector/app/handler/http_thrift_handler_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,18 @@ import (
2525
"github.com/jaegertracing/jaeger/thrift-gen/jaeger"
2626
)
2727

28-
var httpClient = &http.Client{Timeout: 2 * time.Second}
28+
var (
29+
httpClient = &http.Client{Timeout: 2 * time.Second}
30+
_ JaegerBatchesHandler = (*mockJaegerHandler)(nil)
31+
)
2932

3033
type mockJaegerHandler struct {
3134
err error
3235
mux sync.Mutex
3336
batches []*jaeger.Batch
3437
}
3538

36-
func (p *mockJaegerHandler) SubmitBatches(batches []*jaeger.Batch, _ SubmitBatchOptions) ([]*jaeger.BatchSubmitResponse, error) {
39+
func (p *mockJaegerHandler) SubmitBatches(_ context.Context, batches []*jaeger.Batch, _ SubmitBatchOptions) ([]*jaeger.BatchSubmitResponse, error) {
3740
p.mux.Lock()
3841
defer p.mux.Unlock()
3942
p.batches = append(p.batches, batches...)

cmd/collector/app/handler/thrift_span_handler.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package handler
66

77
import (
8+
"context"
9+
810
"go.uber.org/zap"
911

1012
"github.com/jaegertracing/jaeger/cmd/collector/app/processor"
@@ -24,13 +26,13 @@ type SubmitBatchOptions struct {
2426
// ZipkinSpansHandler consumes and handles zipkin spans
2527
type ZipkinSpansHandler interface {
2628
// SubmitZipkinBatch records a batch of spans in Zipkin Thrift format
27-
SubmitZipkinBatch(spans []*zipkincore.Span, options SubmitBatchOptions) ([]*zipkincore.Response, error)
29+
SubmitZipkinBatch(ctx context.Context, spans []*zipkincore.Span, options SubmitBatchOptions) ([]*zipkincore.Response, error)
2830
}
2931

3032
// JaegerBatchesHandler consumes and handles Jaeger batches
3133
type JaegerBatchesHandler interface {
3234
// SubmitBatches records a batch of spans in Jaeger Thrift format
33-
SubmitBatches(batches []*jaeger.Batch, options SubmitBatchOptions) ([]*jaeger.BatchSubmitResponse, error)
35+
SubmitBatches(ctx context.Context, batches []*jaeger.Batch, options SubmitBatchOptions) ([]*jaeger.BatchSubmitResponse, error)
3436
}
3537

3638
type jaegerBatchesHandler struct {
@@ -46,15 +48,15 @@ func NewJaegerSpanHandler(logger *zap.Logger, modelProcessor processor.SpanProce
4648
}
4749
}
4850

49-
func (jbh *jaegerBatchesHandler) SubmitBatches(batches []*jaeger.Batch, options SubmitBatchOptions) ([]*jaeger.BatchSubmitResponse, error) {
51+
func (jbh *jaegerBatchesHandler) SubmitBatches(ctx context.Context, batches []*jaeger.Batch, options SubmitBatchOptions) ([]*jaeger.BatchSubmitResponse, error) {
5052
responses := make([]*jaeger.BatchSubmitResponse, 0, len(batches))
5153
for _, batch := range batches {
5254
mSpans := make([]*model.Span, 0, len(batch.Spans))
5355
for _, span := range batch.Spans {
5456
mSpan := jConv.ToDomainSpan(span, batch.Process)
5557
mSpans = append(mSpans, mSpan)
5658
}
57-
oks, err := jbh.modelProcessor.ProcessSpans(processor.SpansV1{
59+
oks, err := jbh.modelProcessor.ProcessSpans(ctx, processor.SpansV1{
5860
Spans: mSpans,
5961
Details: processor.Details{
6062
InboundTransport: options.InboundTransport,
@@ -98,7 +100,7 @@ func NewZipkinSpanHandler(logger *zap.Logger, modelHandler processor.SpanProcess
98100
}
99101

100102
// SubmitZipkinBatch records a batch of spans already in Zipkin Thrift format.
101-
func (h *zipkinSpanHandler) SubmitZipkinBatch(spans []*zipkincore.Span, options SubmitBatchOptions) ([]*zipkincore.Response, error) {
103+
func (h *zipkinSpanHandler) SubmitZipkinBatch(ctx context.Context, spans []*zipkincore.Span, options SubmitBatchOptions) ([]*zipkincore.Response, error) {
102104
mSpans := make([]*model.Span, 0, len(spans))
103105
convCount := make([]int, len(spans))
104106
for i, span := range spans {
@@ -108,7 +110,7 @@ func (h *zipkinSpanHandler) SubmitZipkinBatch(spans []*zipkincore.Span, options
108110
convCount[i] = len(converted)
109111
mSpans = append(mSpans, converted...)
110112
}
111-
bools, err := h.modelProcessor.ProcessSpans(processor.SpansV1{
113+
bools, err := h.modelProcessor.ProcessSpans(ctx, processor.SpansV1{
112114
Spans: mSpans,
113115
Details: processor.Details{
114116
InboundTransport: options.InboundTransport,

cmd/collector/app/handler/thrift_span_handler_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package handler
66

77
import (
8+
"context"
89
"encoding/json"
910
"errors"
1011
"os"
@@ -36,7 +37,7 @@ func TestJaegerSpanHandler(t *testing.T) {
3637
for _, tc := range testChunks {
3738
logger := zap.NewNop()
3839
h := NewJaegerSpanHandler(logger, &shouldIErrorProcessor{tc.expectedErr != nil})
39-
res, err := h.SubmitBatches([]*jaeger.Batch{
40+
res, err := h.SubmitBatches(context.Background(), []*jaeger.Batch{
4041
{
4142
Process: &jaeger.Process{ServiceName: "someServiceName"},
4243
Spans: []*jaeger.Span{{SpanId: 21345}},
@@ -57,9 +58,12 @@ type shouldIErrorProcessor struct {
5758
shouldError bool
5859
}
5960

60-
var errTestError = errors.New("Whoops")
61+
var (
62+
_ processor.SpanProcessor = (*shouldIErrorProcessor)(nil)
63+
errTestError = errors.New("Whoops")
64+
)
6165

62-
func (s *shouldIErrorProcessor) ProcessSpans(batch processor.Batch) ([]bool, error) {
66+
func (s *shouldIErrorProcessor) ProcessSpans(_ context.Context, batch processor.Batch) ([]bool, error) {
6367
if s.shouldError {
6468
return nil, errTestError
6569
}
@@ -121,7 +125,7 @@ func TestZipkinSpanHandler(t *testing.T) {
121125
},
122126
}
123127
}
124-
res, err := h.SubmitZipkinBatch(spans, SubmitBatchOptions{})
128+
res, err := h.SubmitZipkinBatch(context.Background(), spans, SubmitBatchOptions{})
125129
if tc.expectedErr != nil {
126130
assert.Nil(t, res)
127131
assert.Equal(t, tc.expectedErr, err)

cmd/collector/app/processor/processor.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package processor
55

66
import (
7+
"context"
78
"io"
89

910
"go.opentelemetry.io/collector/pdata/ptrace"
@@ -29,7 +30,7 @@ type Batch interface {
2930
// SpanProcessor handles spans
3031
type SpanProcessor interface {
3132
// ProcessSpans processes spans and return with either a list of true/false success or an error
32-
ProcessSpans(spans Batch) ([]bool, error)
33+
ProcessSpans(ctx context.Context, spans Batch) ([]bool, error)
3334
io.Closer
3435
}
3536

cmd/collector/app/server/test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ func (*mockSpanProcessor) Close() error {
2626
return nil
2727
}
2828

29-
func (*mockSpanProcessor) ProcessSpans(_ processor.Batch) ([]bool, error) {
29+
func (*mockSpanProcessor) ProcessSpans(_ context.Context, _ processor.Batch) ([]bool, error) {
3030
return []bool{}, nil
3131
}

cmd/collector/app/span_processor.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ const (
3636
minRequiredChange = 1.2
3737
)
3838

39+
var _ processor.SpanProcessor = (*spanProcessor)(nil)
40+
3941
type spanProcessor struct {
4042
queue *queue.BoundedQueue[queueItem]
4143
otelExporter exporter.Traces
@@ -239,19 +241,18 @@ func (sp *spanProcessor) countSpansInQueue(span *model.Span, _ string /* tenant
239241
sp.spansProcessed.Add(1)
240242
}
241243

242-
// TODO pass Context
243-
func (sp *spanProcessor) ProcessSpans(batch processor.Batch) ([]bool, error) {
244+
func (sp *spanProcessor) ProcessSpans(ctx context.Context, batch processor.Batch) ([]bool, error) {
244245
// We call preProcessSpans on a batch, it's responsibility of implementation
245246
// to understand v1/v2 distinction. Jaeger itself does not use pre-processors.
246247
sp.preProcessSpans(batch)
247248

248249
var batchOks []bool
249250
var batchErr error
250251
batch.GetSpans(func(spans []*model.Span) {
251-
batchOks, batchErr = sp.processSpans(batch, spans)
252+
batchOks, batchErr = sp.processSpans(ctx, batch, spans)
252253
}, func(traces ptrace.Traces) {
253254
// TODO verify if the context will survive all the way to the consumer threads.
254-
ctx := tenancy.WithTenant(context.Background(), batch.GetTenant())
255+
ctx := tenancy.WithTenant(ctx, batch.GetTenant())
255256

256257
// the exporter will eventually call pushTraces from consumer threads.
257258
if err := sp.otelExporter.ConsumeTraces(ctx, traces); err != nil {
@@ -266,7 +267,7 @@ func (sp *spanProcessor) ProcessSpans(batch processor.Batch) ([]bool, error) {
266267
return batchOks, batchErr
267268
}
268269

269-
func (sp *spanProcessor) processSpans(batch processor.Batch, spans []*model.Span) ([]bool, error) {
270+
func (sp *spanProcessor) processSpans(_ context.Context, batch processor.Batch, spans []*model.Span) ([]bool, error) {
270271
sp.metrics.BatchSize.Update(int64(len(spans)))
271272
retMe := make([]bool, len(spans))
272273

0 commit comments

Comments
 (0)