Skip to content

Commit

Permalink
Use trace.SetCurrentSpan to store the span in the context in SDK. (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
rghetia authored Sep 19, 2019
1 parent 3362421 commit 8af3bfc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 16 deletions.
12 changes: 0 additions & 12 deletions sdk/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package trace

import (
"context"
crand "crypto/rand"
"encoding/binary"
"math/rand"
Expand Down Expand Up @@ -62,14 +61,3 @@ func Register() apitrace.Tracer {
})
return tr
}

type contextKey struct{}

func fromContext(ctx context.Context) *span {
s, _ := ctx.Value(contextKey{}).(*span)
return s
}

func newContext(parent context.Context, s *span) context.Context {
return context.WithValue(parent, contextKey{}, s)
}
10 changes: 6 additions & 4 deletions sdk/trace/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ func (tr *tracer) Start(ctx context.Context, name string, o ...apitrace.SpanOpti
parent = opts.Reference.SpanContext
remoteParent = true
} else {
if p := fromContext(ctx); p != nil {
p.addChild()
parent = p.spanContext
if p := apitrace.CurrentSpan(ctx); p != nil {
if sdkSpan, ok := p.(*span); ok {
sdkSpan.addChild()
parent = sdkSpan.spanContext
}
}
}

Expand All @@ -70,7 +72,7 @@ func (tr *tracer) Start(ctx context.Context, name string, o ...apitrace.SpanOpti

ctx, end := startExecutionTracerTask(ctx, name)
span.executionTracerTaskEnd = end
return newContext(ctx, span), span
return apitrace.SetCurrentSpan(ctx, span), span
}

func (tr *tracer) WithSpan(ctx context.Context, name string, body func(ctx context.Context) error) error {
Expand Down

0 comments on commit 8af3bfc

Please sign in to comment.