Skip to content

Commit

Permalink
new span without context
Browse files Browse the repository at this point in the history
Signed-off-by: zhongzc <[email protected]>
  • Loading branch information
zhongzc committed Jun 7, 2020
1 parent 8a7889c commit 375fe4e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@
# vendor/

.idea

*.prof
2 changes: 1 addition & 1 deletion minitrace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func tracedFunc(l int, b *testing.B) {
ctx, handle := TraceEnable(context.Background(), 0)

for i := 1; i < l; i++ {
_, handle := NewSpan(ctx, uint32(i))
handle := NewSpan(ctx, uint32(i))
handle.Finish()
}

Expand Down
16 changes: 12 additions & 4 deletions trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ func TraceEnable(ctx context.Context, event uint32) (context.Context, TraceHandl
return nCtx, TraceHandle{SpanHandle{spanCtx, 0}}
}

func NewSpan(ctx context.Context, event uint32) (context.Context, SpanHandle) {
func NewSpanWithContext(ctx context.Context, event uint32) (context.Context, SpanHandle) {
handle := NewSpan(ctx, event)
if handle.spanContext != nil {
return context.WithValue(ctx, key, handle.spanContext), handle
}
return ctx, handle
}

func NewSpan(ctx context.Context, event uint32) SpanHandle {
switch v := ctx.Value(key).(type) {
case *spanContext:
id := atomic.AddUint64(&v.tracingContext.maxId, 1)
Expand All @@ -58,7 +66,7 @@ func NewSpan(ctx context.Context, event uint32) (context.Context, SpanHandle) {
currentId: id,
currentGid: goid,
}
return context.WithValue(ctx, key, spanCtx), SpanHandle{spanCtx, index}
return SpanHandle{spanCtx, index}
} else {
tracedSpans := &localSpans{
spans: []Span{span},
Expand All @@ -71,11 +79,11 @@ func NewSpan(ctx context.Context, event uint32) (context.Context, SpanHandle) {
currentId: id,
currentGid: goid,
}
return context.WithValue(ctx, key, spanCtx), SpanHandle{spanCtx, 0}
return SpanHandle{spanCtx, 0}
}
}

return ctx, SpanHandle{}
return SpanHandle{}
}

type SpanHandle struct {
Expand Down

0 comments on commit 375fe4e

Please sign in to comment.