@@ -545,18 +545,7 @@ private void handleTaskScheduled(HistoryEvent e) {
545545
546546 TaskScheduledEvent taskScheduled = e .getTaskScheduled ();
547547
548- // Store scheduling metadata for retroactive client span at completion time.
549- // Use orchestrationSpanContext as parent so the client span is a child of the orchestration span.
550- Instant scheduledTime = e .hasTimestamp ()
551- ? DataConverter .getInstantFromTimestamp (e .getTimestamp ())
552- : null ;
553- TraceContext spanParent = this .orchestrationSpanContext != null
554- ? this .orchestrationSpanContext : this .parentTraceContext ;
555- this .scheduledTaskInfoMap .put (taskId , new ScheduledTaskInfo (
556- taskScheduled .getName (),
557- scheduledTime ,
558- spanParent ,
559- TracingHelper .TYPE_ACTIVITY ));
548+ storeSchedulingMetadata (taskId , taskScheduled .getName (), TracingHelper .TYPE_ACTIVITY , e );
560549
561550 // The history shows that this orchestrator created a durable task in a previous execution.
562551 // We can therefore remove it from the map of pending actions. If we can't find the pending
@@ -595,17 +584,7 @@ private void handleTaskCompleted(HistoryEvent e) {
595584
596585 // Emit a retroactive Client span covering scheduling-to-completion duration.
597586 // Matches .NET SDK's EmitTraceActivityForTaskCompleted pattern.
598- ScheduledTaskInfo info = this .scheduledTaskInfoMap .remove (taskId );
599- if (info != null ) {
600- TracingHelper .emitRetroactiveClientSpan (
601- info .spanType + ":" + info .taskName ,
602- info .parentTraceContext ,
603- info .spanType ,
604- info .taskName ,
605- this .instanceId ,
606- taskId ,
607- info .scheduledTime );
608- }
587+ emitClientSpanIfTracked (taskId );
609588 }
610589 CompletableTask task = record .getTask ();
611590 try {
@@ -631,17 +610,7 @@ private void handleTaskFailed(HistoryEvent e) {
631610 // TODO: Log task failure, including the number of bytes in the result
632611
633612 // Emit a retroactive Client span covering scheduling-to-failure duration.
634- ScheduledTaskInfo info = this .scheduledTaskInfoMap .remove (taskId );
635- if (info != null ) {
636- TracingHelper .emitRetroactiveClientSpan (
637- info .spanType + ":" + info .taskName ,
638- info .parentTraceContext ,
639- info .spanType ,
640- info .taskName ,
641- this .instanceId ,
642- taskId ,
643- info .scheduledTime );
644- }
613+ emitClientSpanIfTracked (taskId );
645614 }
646615
647616 CompletableTask <?> task = record .getTask ();
@@ -798,17 +767,8 @@ private void handleSubOrchestrationCreated(HistoryEvent e) {
798767 int taskId = e .getEventId ();
799768 SubOrchestrationInstanceCreatedEvent subOrchestrationInstanceCreated = e .getSubOrchestrationInstanceCreated ();
800769
801- // Store scheduling metadata for retroactive client span at completion time
802- Instant scheduledTime = e .hasTimestamp ()
803- ? DataConverter .getInstantFromTimestamp (e .getTimestamp ())
804- : null ;
805- TraceContext spanParent = this .orchestrationSpanContext != null
806- ? this .orchestrationSpanContext : this .parentTraceContext ;
807- this .scheduledTaskInfoMap .put (taskId , new ScheduledTaskInfo (
808- subOrchestrationInstanceCreated .getName (),
809- scheduledTime ,
810- spanParent ,
811- TracingHelper .TYPE_ORCHESTRATION ));
770+ storeSchedulingMetadata (taskId , subOrchestrationInstanceCreated .getName (),
771+ TracingHelper .TYPE_ORCHESTRATION , e );
812772
813773 OrchestratorAction taskAction = this .pendingActions .remove (taskId );
814774 if (taskAction == null ) {
@@ -841,17 +801,7 @@ private void handleSubOrchestrationCompleted(HistoryEvent e) {
841801 rawResult != null ? rawResult : "(null)" ));
842802
843803 // Emit a retroactive Client span covering scheduling-to-completion duration.
844- ScheduledTaskInfo info = this .scheduledTaskInfoMap .remove (taskId );
845- if (info != null ) {
846- TracingHelper .emitRetroactiveClientSpan (
847- info .spanType + ":" + info .taskName ,
848- info .parentTraceContext ,
849- info .spanType ,
850- info .taskName ,
851- this .instanceId ,
852- taskId ,
853- info .scheduledTime );
854- }
804+ emitClientSpanIfTracked (taskId );
855805 }
856806 CompletableTask task = record .getTask ();
857807 try {
@@ -877,17 +827,7 @@ private void handleSubOrchestrationFailed(HistoryEvent e){
877827 // TODO: Log task failure, including the number of bytes in the result
878828
879829 // Emit a retroactive Client span covering scheduling-to-failure duration.
880- ScheduledTaskInfo info = this .scheduledTaskInfoMap .remove (taskId );
881- if (info != null ) {
882- TracingHelper .emitRetroactiveClientSpan (
883- info .spanType + ":" + info .taskName ,
884- info .parentTraceContext ,
885- info .spanType ,
886- info .taskName ,
887- this .instanceId ,
888- taskId ,
889- info .scheduledTime );
890- }
830+ emitClientSpanIfTracked (taskId );
891831 }
892832
893833 CompletableTask <?> task = record .getTask ();
@@ -1104,6 +1044,35 @@ public Class<V> getDataType() {
11041044 }
11051045 }
11061046
1047+ /**
1048+ * Emits a retroactive Client span for a completed/failed task, if scheduling metadata was tracked.
1049+ */
1050+ private void emitClientSpanIfTracked (int taskId ) {
1051+ ScheduledTaskInfo info = this .scheduledTaskInfoMap .remove (taskId );
1052+ if (info != null ) {
1053+ TracingHelper .emitRetroactiveClientSpan (
1054+ info .spanType + ":" + info .taskName ,
1055+ info .parentTraceContext ,
1056+ info .spanType ,
1057+ info .taskName ,
1058+ this .instanceId ,
1059+ taskId ,
1060+ info .scheduledTime );
1061+ }
1062+ }
1063+
1064+ /**
1065+ * Stores scheduling metadata for a task so a retroactive client span can be emitted at completion time.
1066+ */
1067+ private void storeSchedulingMetadata (int taskId , String taskName , String spanType , HistoryEvent e ) {
1068+ Instant scheduledTime = e .hasTimestamp ()
1069+ ? DataConverter .getInstantFromTimestamp (e .getTimestamp ()) : null ;
1070+ TraceContext spanParent = this .orchestrationSpanContext != null
1071+ ? this .orchestrationSpanContext : this .parentTraceContext ;
1072+ this .scheduledTaskInfoMap .put (taskId , new ScheduledTaskInfo (
1073+ taskName , scheduledTime , spanParent , spanType ));
1074+ }
1075+
11071076 /**
11081077 * Stores scheduling metadata for retroactive client span creation at completion time.
11091078 * Matches .NET SDK pattern where client spans are emitted with the full scheduling-to-completion duration.
0 commit comments