github.com/shyftnetwork/go-empyrean@v1.8.3-0.20191127201940-fbfca9338f04/swarm/spancontext/spancontext.go (about)

     1  package spancontext
     2  
     3  import (
     4  	"context"
     5  
     6  	opentracing "github.com/opentracing/opentracing-go"
     7  )
     8  
     9  func WithContext(ctx context.Context, sctx opentracing.SpanContext) context.Context {
    10  	return context.WithValue(ctx, "span_context", sctx)
    11  }
    12  
    13  func FromContext(ctx context.Context) opentracing.SpanContext {
    14  	sctx, ok := ctx.Value("span_context").(opentracing.SpanContext)
    15  	if ok {
    16  		return sctx
    17  	}
    18  
    19  	return nil
    20  }
    21  
    22  func StartSpan(ctx context.Context, name string) (context.Context, opentracing.Span) {
    23  	tracer := opentracing.GlobalTracer()
    24  
    25  	sctx := FromContext(ctx)
    26  
    27  	var sp opentracing.Span
    28  	if sctx != nil {
    29  		sp = tracer.StartSpan(
    30  			name,
    31  			opentracing.ChildOf(sctx))
    32  	} else {
    33  		sp = tracer.StartSpan(name)
    34  	}
    35  
    36  	nctx := context.WithValue(ctx, "span_context", sp.Context())
    37  
    38  	return nctx, sp
    39  }
    40  
    41  func StartSpanFrom(name string, sctx opentracing.SpanContext) opentracing.Span {
    42  	tracer := opentracing.GlobalTracer()
    43  
    44  	sp := tracer.StartSpan(
    45  		name,
    46  		opentracing.ChildOf(sctx))
    47  
    48  	return sp
    49  }