github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/actor/middleware/opentracing/activespan.go (about)

     1  package opentracing
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  
     7  	"github.com/asynkron/protoactor-go/actor"
     8  	"github.com/opentracing/opentracing-go"
     9  )
    10  
    11  var activeSpan = sync.Map{}
    12  
    13  func getActiveSpan(pid *actor.PID) opentracing.Span {
    14  	value, ok := activeSpan.Load(pid)
    15  	if !ok {
    16  		return nil
    17  	}
    18  
    19  	span, _ := value.(opentracing.Span)
    20  
    21  	return span
    22  }
    23  
    24  func clearActiveSpan(pid *actor.PID) {
    25  	activeSpan.Delete(pid)
    26  }
    27  
    28  func setActiveSpan(pid *actor.PID, span opentracing.Span) {
    29  	activeSpan.Store(pid, span)
    30  }
    31  
    32  func GetActiveSpan(context actor.Context) opentracing.Span {
    33  	span := getActiveSpan(context.Self())
    34  	if span == nil {
    35  		// TODO: Fix finding the real span always or handle no-span better on receiving side
    36  		span = opentracing.StartSpan(fmt.Sprintf("%T/%T", context.Actor(), context.Message()))
    37  	}
    38  
    39  	return span
    40  }