github.com/mier85/go-sensor@v1.30.1-0.20220920111756-9bf41b3bc7e0/context.go (about)

     1  // (c) Copyright IBM Corp. 2021
     2  // (c) Copyright Instana Inc. 2017
     3  
     4  package instana
     5  
     6  import (
     7  	"context"
     8  
     9  	ot "github.com/opentracing/opentracing-go"
    10  )
    11  
    12  type contextKey struct{}
    13  
    14  var activeSpanKey contextKey
    15  
    16  // ContextWithSpan returns a new context.Context holding a reference to an active span
    17  func ContextWithSpan(ctx context.Context, sp ot.Span) context.Context {
    18  	return context.WithValue(ctx, activeSpanKey, sp)
    19  }
    20  
    21  // SpanFromContext retrieves previously stored active span from context. If there is no
    22  // span, this method returns false.
    23  func SpanFromContext(ctx context.Context) (ot.Span, bool) {
    24  	sp, ok := ctx.Value(activeSpanKey).(ot.Span)
    25  	if !ok {
    26  		return nil, false
    27  	}
    28  
    29  	return sp, true
    30  }