github.com/instana/go-sensor@v1.62.2-0.20240520081010-4919868049e1/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  }