github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/spancontext/spancontext.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 package spancontext 10 11 import ( 12 "context" 13 14 opentracing "github.com/opentracing/opentracing-go" 15 ) 16 17 func WithContext(ctx context.Context, sctx opentracing.SpanContext) context.Context { 18 return context.WithValue(ctx, "span_context", sctx) 19 } 20 21 func FromContext(ctx context.Context) opentracing.SpanContext { 22 sctx, ok := ctx.Value("span_context").(opentracing.SpanContext) 23 if ok { 24 return sctx 25 } 26 27 return nil 28 } 29 30 func StartSpan(ctx context.Context, name string) (context.Context, opentracing.Span) { 31 tracer := opentracing.GlobalTracer() 32 33 sctx := FromContext(ctx) 34 35 var sp opentracing.Span 36 if sctx != nil { 37 sp = tracer.StartSpan( 38 name, 39 opentracing.ChildOf(sctx)) 40 } else { 41 sp = tracer.StartSpan(name) 42 } 43 44 nctx := context.WithValue(ctx, "span_context", sp.Context()) 45 46 return nctx, sp 47 } 48 49 func StartSpanFrom(name string, sctx opentracing.SpanContext) opentracing.Span { 50 tracer := opentracing.GlobalTracer() 51 52 sp := tracer.StartSpan( 53 name, 54 opentracing.ChildOf(sctx)) 55 56 return sp 57 }