Skip to content

Commit

Permalink
fix extract Extension
Browse files Browse the repository at this point in the history
  • Loading branch information
ryota0624 committed Jan 11, 2025
1 parent b68ef11 commit a53537e
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 28 deletions.
18 changes: 9 additions & 9 deletions actor/middleware/otel/tracing/context_decorator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type ActorContext struct {
var _ actor.Context = (*ActorContext)(nil)

func (ac *ActorContext) Receive(envelope *actor.MessageEnvelope) {
traceExt, ok := ac.ActorSystem().Extensions.Get(extensionID).(*TraceExtension)
traceExt, ok := ExtensionFromActorSystem(ac.ActorSystem())
if !ok {
ac.Logger().Debug("TraceExtension not registered")
ac.Context.Receive(envelope)
Expand Down Expand Up @@ -69,7 +69,7 @@ func (ac *ActorContext) Receive(envelope *actor.MessageEnvelope) {
}

func (ac *ActorContext) Send(pid *actor.PID, message interface{}) {
traceExt, ok := ac.ActorSystem().Extensions.Get(extensionID).(*TraceExtension)
traceExt, ok := ExtensionFromActorSystem(ac.ActorSystem())
if !ok {
ac.Logger().Debug("TraceExtension not registered")
ac.Context.Send(pid, message)
Expand All @@ -84,7 +84,7 @@ func (ac *ActorContext) Send(pid *actor.PID, message interface{}) {
}

func (ac *ActorContext) Request(pid *actor.PID, message interface{}) {
traceExt, ok := ac.ActorSystem().Extensions.Get(extensionID).(*TraceExtension)
traceExt, ok := ExtensionFromActorSystem(ac.ActorSystem())
if !ok {
ac.Logger().Debug("TraceExtension not registered")
ac.Context.Request(pid, message)
Expand All @@ -99,7 +99,7 @@ func (ac *ActorContext) Request(pid *actor.PID, message interface{}) {
}

func (ac *ActorContext) RequestWithCustomSender(pid *actor.PID, message interface{}, sender *actor.PID) {
traceExt, ok := ac.ActorSystem().Extensions.Get(extensionID).(*TraceExtension)
traceExt, ok := ExtensionFromActorSystem(ac.ActorSystem())
if !ok {
ac.Logger().Debug("TraceExtension not registered")
ac.Context.RequestWithCustomSender(pid, message, sender)
Expand All @@ -126,7 +126,7 @@ func messageToEnvelop(message interface{}, t *ActorContext, sender *actor.PID) *
}

func (ac *ActorContext) RequestFuture(pid *actor.PID, message interface{}, timeout time.Duration) *actor.Future {
traceExt, ok := ac.ActorSystem().Extensions.Get(extensionID).(*TraceExtension)
traceExt, ok := ExtensionFromActorSystem(ac.ActorSystem())
if !ok {
ac.Logger().Debug("TraceExtension not registered")
return ac.Context.RequestFuture(pid, message, timeout)
Expand All @@ -142,7 +142,7 @@ func (ac *ActorContext) RequestFuture(pid *actor.PID, message interface{}, timeo

}
func (ac *ActorContext) Respond(response interface{}) {
traceExt, ok := ac.ActorSystem().Extensions.Get(extensionID).(*TraceExtension)
traceExt, ok := ExtensionFromActorSystem(ac.ActorSystem())
if !ok {
ac.Logger().Debug("TraceExtension not registered")
ac.Context.Respond(response)
Expand All @@ -165,7 +165,7 @@ func ContextDecorator() func(next actor.ContextDecoratorFunc) actor.ContextDecor
}

func (ac *ActorContext) Spawn(props *actor.Props) *actor.PID {
traceExt, ok := ac.ActorSystem().Extensions.Get(extensionID).(*TraceExtension)
traceExt, ok := ExtensionFromActorSystem(ac.ActorSystem())
if !ok {
ac.Logger().Debug("TraceExtension not registered")
return ac.Context.Spawn(props)
Expand All @@ -180,7 +180,7 @@ func (ac *ActorContext) Spawn(props *actor.Props) *actor.PID {
}

func (ac *ActorContext) SpawnPrefix(props *actor.Props, prefix string) *actor.PID {
traceExt, ok := ac.ActorSystem().Extensions.Get(extensionID).(*TraceExtension)
traceExt, ok := ExtensionFromActorSystem(ac.ActorSystem())
if !ok {
ac.Logger().Debug("TraceExtension not registered")
return ac.Context.SpawnPrefix(props, prefix)
Expand All @@ -197,7 +197,7 @@ func (ac *ActorContext) SpawnPrefix(props *actor.Props, prefix string) *actor.PI
}

func (ac *ActorContext) SpawnNamed(props *actor.Props, id string) (*actor.PID, error) {
traceExt, ok := ac.ActorSystem().Extensions.Get(extensionID).(*TraceExtension)
traceExt, ok := ExtensionFromActorSystem(ac.ActorSystem())
if !ok {
ac.Logger().Debug("TraceExtension not registered")
return ac.Context.SpawnNamed(props, id)
Expand Down
9 changes: 9 additions & 0 deletions actor/middleware/otel/tracing/extension.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tracing

import (
"github.com/asynkron/protoactor-go/actor"
"github.com/asynkron/protoactor-go/extensions"
"go.opentelemetry.io/otel/trace"
)
Expand All @@ -15,6 +16,14 @@ func (ext *TraceExtension) Tracer() trace.Tracer {
return ext.TracerProvider.Tracer("protoactor")
}

func ExtensionFromActorSystem(system *actor.ActorSystem) (*TraceExtension, bool) {
t, ok := system.Extensions.Get(extensionID).(*TraceExtension)
if !ok {
return nil, false
}
return t, true
}

func NewTraceExtension(
provider trace.TracerProvider,
) *TraceExtension {
Expand Down
8 changes: 7 additions & 1 deletion actor/middleware/otel/tracing/sender_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ func RootContextSenderMiddleware() actor.SenderMiddleware {
return
}

traceExt, ok := ExtensionFromActorSystem(c.ActorSystem())
if !ok {
c.Logger().Debug("TraceExtension not registered")
next(c, target, envelope)
return
}

ctxWithParentSpan := context2.Background()
spanContext, err := extractSpanContextFromSenderFuncArgs(c, envelope)
if errors.Is(err, ErrSpanContextNotFound) {
Expand All @@ -46,7 +53,6 @@ func RootContextSenderMiddleware() actor.SenderMiddleware {
ctxWithParentSpan = trace.ContextWithSpanContext(ctxWithParentSpan, spanContext)
}

traceExt := c.ActorSystem().Extensions.Get(extensionID).(*TraceExtension)
ctxWithCurrentSpan, span := traceExt.Tracer().Start(ctxWithParentSpan, fmt.Sprintf("message_send/%T", envelope.Message))
defer span.End()
span.SetAttributes(attribute.String("SenderActorPID", c.Self().String()))
Expand Down
8 changes: 6 additions & 2 deletions actor/middleware/otel/tracing/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ func setSpanContextToEnvelope(spanCtx trace.SpanContext, envelope *actor.Message
}

// TraceableRootContext creates a RootContext with tracing capabilities
func TraceableRootContext(rootContext actor.RootContext, spanContext trace.SpanContext) *actor.RootContext {
return rootContext.Copy().WithSenderMiddleware(RootContextSenderMiddleware()).WithSpawnMiddleware(RootContextSpawnMiddleware()).WithHeaders(MapFromSpanContext(spanContext))
func TraceableRootContext(rootContext *actor.RootContext) *actor.RootContext {
return rootContext.Copy().WithSenderMiddleware(RootContextSenderMiddleware()).WithSpawnMiddleware(RootContextSpawnMiddleware())
}

func WithSpanRootContext(rootContext *actor.RootContext, span trace.Span) *actor.RootContext {
return rootContext.Copy().WithHeaders(MapFromSpanContext(span.SpanContext()))
}
9 changes: 8 additions & 1 deletion actor/middleware/otel/tracing/spawn_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ import (
func rootContextSpawnMiddleware() actor.SpawnMiddleware {
return func(next actor.SpawnFunc) actor.SpawnFunc {
return func(actorSystem *actor.ActorSystem, id string, props *actor.Props, parentContext actor.SpawnerContext) (pid *actor.PID, e error) {
traceExt, ok := ExtensionFromActorSystem(actorSystem)
if !ok {
actorSystem.Logger().Debug("TraceExtension not registered")
pid, err := next(actorSystem, id, props, parentContext)
return pid, err
}

rootContext, ok := parentContext.(*actor.RootContext)
if !ok {
parentContext.Logger().Debug("Context is not rootContext", slog.Any("self", parentContext.Self()))
Expand All @@ -34,7 +41,7 @@ func rootContextSpawnMiddleware() actor.SpawnMiddleware {
ctxWithParentSpan = trace.ContextWithSpanContext(ctxWithParentSpan, spanCtx)
}
}
traceExt := actorSystem.Extensions.Get(extensionID).(*TraceExtension)

_, span := traceExt.Tracer().Start(ctxWithParentSpan, fmt.Sprintf("spawn/%s", id))
defer span.End()
span.SetAttributes(attribute.String("SpawnActorPID", pid.String()))
Expand Down
3 changes: 2 additions & 1 deletion examples/opentelemetry-trace/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/asynkron/protoactor-go v0.0.0-20240822202345-3c0e61ca19c9
go.opentelemetry.io/otel v1.33.0
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.33.0
go.opentelemetry.io/otel/sdk v1.33.0

)

Expand All @@ -33,8 +34,8 @@ require (
github.com/prometheus/procfs v0.11.1 // indirect
github.com/twmb/murmur3 v1.1.8 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/otel/exporters/prometheus v0.44.0 // indirect
go.opentelemetry.io/otel/metric v1.33.0 // indirect
go.opentelemetry.io/otel/sdk v1.33.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.33.0 // indirect
go.opentelemetry.io/otel/trace v1.33.0 // indirect
golang.org/x/sys v0.28.0 // indirect
Expand Down
11 changes: 0 additions & 11 deletions examples/opentelemetry-trace/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/Workiva/go-datastructures v1.1.3 h1:LRdRrug9tEuKk7TGfz/sct5gjVj44G9pfqDt4qm7ghw=
github.com/Workiva/go-datastructures v1.1.3/go.mod h1:1yZL+zfsztete+ePzZz/Zb1/t5BnDuE2Ya2MMGhzP6A=
github.com/Workiva/go-datastructures v1.1.5 h1:5YfhQ4ry7bZc2Mc7R0YZyYwpf5c6t1cEFvdAhd6Mkf4=
github.com/Workiva/go-datastructures v1.1.5/go.mod h1:1yZL+zfsztete+ePzZz/Zb1/t5BnDuE2Ya2MMGhzP6A=
github.com/asynkron/goconsole v0.0.0-20160504192649-bfa12eebf716 h1:SgyG4sXkrlalMoCfp20LiNPNhfJS7ez3opNdtihIxPc=
Expand All @@ -19,12 +17,8 @@ github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ4
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand Down Expand Up @@ -73,8 +67,6 @@ go.opentelemetry.io/otel/metric v1.33.0 h1:r+JOocAyeRVXD8lZpjdQjzMadVZp2M4WmQ+5W
go.opentelemetry.io/otel/metric v1.33.0/go.mod h1:L9+Fyctbp6HFTddIxClbQkjtubW6O9QS3Ann/M82u6M=
go.opentelemetry.io/otel/sdk v1.33.0 h1:iax7M131HuAm9QkZotNHEfstof92xM+N8sr3uHXc2IM=
go.opentelemetry.io/otel/sdk v1.33.0/go.mod h1:A1Q5oi7/9XaMlIWzPSxLRWOI8nG3FnzHJNbiENQuihM=
go.opentelemetry.io/otel/sdk/metric v1.21.0 h1:smhI5oD714d6jHE6Tie36fPx4WDFIg+Y6RfAY4ICcR0=
go.opentelemetry.io/otel/sdk/metric v1.21.0/go.mod h1:FJ8RAsoPGv/wYMgBdUJXOm+6pzFY3YdljnXtv1SBE8Q=
go.opentelemetry.io/otel/sdk/metric v1.33.0 h1:Gs5VK9/WUJhNXZgn8MR6ITatvAmKeIuCtNbsP3JkNqU=
go.opentelemetry.io/otel/sdk/metric v1.33.0/go.mod h1:dL5ykHZmm1B1nVRk9dDjChwDmt81MjVp3gLkQRwKf/Q=
go.opentelemetry.io/otel/trace v1.33.0 h1:cCJuF7LRjUFso9LPnEAHJDB2pqzp+hbO8eu1qqW2d/s=
Expand All @@ -101,10 +93,7 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.0.0-20201022035929-9cf592e881e9/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
7 changes: 4 additions & 3 deletions examples/opentelemetry-trace/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func main() {
traceProvider := sdktrace.NewTracerProvider(
sdktrace.WithBatcher(traceConsoleExporter),
)

defer func() {
err := traceProvider.Shutdown(context2.Background())
if err != nil {
Expand All @@ -47,11 +48,11 @@ func main() {
props := actor.PropsFromProducer(func() actor.Actor {
return &helloActor{}
})

root := tracing.TraceableRootContext(system.Root)
otel.SetTracerProvider(traceProvider)

pid := system.Root.Spawn(props)
system.Root.Request(pid, &hello{Who: "with tracing"})
pid := root.Spawn(props)
root.Request(pid, &hello{Who: "with tracing"})
time.Sleep(100 * time.Millisecond)
_, _ = console.ReadLine()
}

0 comments on commit a53537e

Please sign in to comment.