@@ -4,7 +4,10 @@ import (
44 "fmt"
55 "slices"
66
7+ "go.opentelemetry.io/otel/attribute"
8+ "go.opentelemetry.io/otel/trace"
79 "go.temporal.io/api/serviceerror"
10+ "go.temporal.io/server/common/telemetry"
811)
912
1013// ErrInvalidTransition is returned from [Transition.Apply] on an invalid state transition.
@@ -44,8 +47,24 @@ func (t Transition[S, SM, E]) Possible(sm SM) bool {
4447
4548// Apply applies a transition event to the given state machine changing the state machine's state to the transition's
4649// Destination on success.
47- func (t Transition [S , SM , E ]) Apply (sm SM , ctx MutableContext , event E ) error {
50+ func (t Transition [S , SM , E ]) Apply (sm SM , ctx MutableContext , event E ) ( retErr error ) {
4851 prevState := sm .StateMachineState ()
52+
53+ // Defer to always emit the transition telemetry event.
54+ if telemetry .DebugMode () {
55+ defer func () {
56+ attrs := []attribute.KeyValue {
57+ attribute .String ("chasm.transition.source" , fmt .Sprintf ("%v" , prevState )),
58+ attribute .String ("chasm.transition.destination" , fmt .Sprintf ("%v" , t .Destination )),
59+ }
60+ if retErr != nil {
61+ attrs = append (attrs , attribute .String ("chasm.transition.error" , retErr .Error ()))
62+ }
63+ span := trace .SpanFromContext (ctx .goContext ())
64+ span .AddEvent ("chasm.transition" , trace .WithAttributes (attrs ... ))
65+ }()
66+ }
67+
4968 if ! t .Possible (sm ) {
5069 return fmt .Errorf ("%w from %v" , ErrInvalidTransition , prevState )
5170 }
0 commit comments