Skip to content

Commit

Permalink
create new context for each bundle (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevencl1013 committed Jul 28, 2020
1 parent 7f25d82 commit 2752da7
Showing 1 changed file with 4 additions and 21 deletions.
25 changes: 4 additions & 21 deletions exporter/trace/trace.go
Expand Up @@ -57,16 +57,8 @@ func newTraceExporter(o *options) (*traceExporter, error) {
client: client,
o: o,
}
b := bundler.NewBundler((*contextAndSpans)(nil), func(bundle interface{}) {
ctxSpans := bundle.([]*contextAndSpans)
ctxToSpansMap := make(map[context.Context][]*tracepb.Span)
// upload spans with same context in batch
for _, cs := range ctxSpans {
ctxToSpansMap[cs.ctx] = append(ctxToSpansMap[cs.ctx], cs.spans...)
}
for ctx, spans := range ctxToSpansMap {
e.uploadFn(ctx, spans)
}
b := bundler.NewBundler((*tracepb.Span)(nil), func(bundle interface{}) {
e.uploadFn(context.Background(), bundle.([]*tracepb.Span))
})
if o.BundleDelayThreshold > 0 {
b.DelayThreshold = o.BundleDelayThreshold
Expand Down Expand Up @@ -108,13 +100,10 @@ func (e *traceExporter) checkBundlerError(err error) {
}

// ExportSpan exports a SpanData to Stackdriver Trace.
func (e *traceExporter) ExportSpan(ctx context.Context, sd *export.SpanData) {
func (e *traceExporter) ExportSpan(_ context.Context, sd *export.SpanData) {
protoSpan := protoFromSpanData(sd, e.projectID, e.o.DisplayNameFormatter)
protoSize := proto.Size(protoSpan)
err := e.bundler.Add(&contextAndSpans{
ctx: ctx,
spans: []*tracepb.Span{protoSpan},
}, protoSize)
err := e.bundler.Add(protoSpan, protoSize)
e.checkBundlerError(err)
}

Expand Down Expand Up @@ -152,12 +141,6 @@ func (e *traceExporter) Flush() {
e.bundler.Flush()
}

// contextAndSpan stores both a context and spans for use with a bundler.
type contextAndSpans struct {
ctx context.Context
spans []*tracepb.Span
}

// overflowLogger ensures that at most one overflow error log message is
// written every 5 seconds.
type overflowLogger struct {
Expand Down

0 comments on commit 2752da7

Please sign in to comment.