From 375fe4edc21aa016bd26efd8fa8eb17d86563793 Mon Sep 17 00:00:00 2001 From: zhongzc Date: Mon, 8 Jun 2020 00:13:42 +0800 Subject: [PATCH] new span without context Signed-off-by: zhongzc --- .gitignore | 2 ++ minitrace_test.go | 2 +- trace.go | 16 ++++++++++++---- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 398baf2..b0276ee 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,5 @@ # vendor/ .idea + +*.prof \ No newline at end of file diff --git a/minitrace_test.go b/minitrace_test.go index fa19a81..a5adbff 100644 --- a/minitrace_test.go +++ b/minitrace_test.go @@ -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() } diff --git a/trace.go b/trace.go index c77bb48..49eeda9 100644 --- a/trace.go +++ b/trace.go @@ -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) @@ -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}, @@ -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 {