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

     1  package opentracing
     2  
     3  import (
     4  	"github.com/asynkron/protoactor-go/actor"
     5  	olog "github.com/opentracing/opentracing-go/log"
     6  	"log/slog"
     7  )
     8  
     9  func SpawnMiddleware() actor.SpawnMiddleware {
    10  	return func(next actor.SpawnFunc) actor.SpawnFunc {
    11  		return func(actorSystem *actor.ActorSystem, id string, props *actor.Props, parentContext actor.SpawnerContext) (pid *actor.PID, e error) {
    12  			self := parentContext.Self()
    13  			pid, err := next(actorSystem, id, props, parentContext)
    14  			if err != nil {
    15  				actorSystem.Logger().Debug("SPAWN got error trying to spawn", slog.Any("self", self), slog.Any("actor", parentContext.Actor()), slog.Any("error", err))
    16  				return pid, err
    17  			}
    18  			if self != nil {
    19  				span := getActiveSpan(self)
    20  				if span != nil {
    21  					setParentSpan(pid, span)
    22  					span.LogFields(olog.String("SpawnPID", pid.String()))
    23  					actorSystem.Logger().Debug("SPAWN found active span", slog.Any("self", self), slog.Any("actor", parentContext.Actor()), slog.Any("spawned-pid", pid))
    24  				} else {
    25  					actorSystem.Logger().Debug("SPAWN no active span on parent", slog.Any("self", self), slog.Any("actor", parentContext.Actor()), slog.Any("spawned-pid", pid))
    26  				}
    27  			} else {
    28  				actorSystem.Logger().Debug("SPAWN no parent pid", slog.Any("self", self))
    29  			}
    30  			return pid, err
    31  		}
    32  	}
    33  }