88 type CompleteRunAttemptResult ,
99 HeartbeatService ,
1010 type RunExecutionData ,
11+ type TaskRunExecutionMetrics ,
1112 type TaskRunExecutionResult ,
1213 type TaskRunFailedExecutionResult ,
1314 WorkerManifest ,
@@ -50,6 +51,9 @@ const Env = z.object({
5051 TRIGGER_RUNNER_ID : z . string ( ) ,
5152 TRIGGER_METADATA_URL : z . string ( ) . optional ( ) ,
5253
54+ // Timeline metrics
55+ TRIGGER_POD_SCHEDULED_AT_MS : z . coerce . date ( ) ,
56+
5357 // May be overridden
5458 TRIGGER_SUPERVISOR_API_PROTOCOL : z . enum ( [ "http" , "https" ] ) ,
5559 TRIGGER_SUPERVISOR_API_DOMAIN : z . string ( ) ,
@@ -734,10 +738,14 @@ class ManagedRunController {
734738 private async startAndExecuteRunAttempt ( {
735739 runFriendlyId,
736740 snapshotFriendlyId,
741+ dequeuedAt,
742+ podScheduledAt,
737743 isWarmStart = false ,
738744 } : {
739745 runFriendlyId : string ;
740746 snapshotFriendlyId : string ;
747+ dequeuedAt ?: Date ;
748+ podScheduledAt ?: Date ;
741749 isWarmStart ?: boolean ;
742750 } ) {
743751 if ( ! this . socket ) {
@@ -749,6 +757,8 @@ class ManagedRunController {
749757 snapshot : { friendlyId : snapshotFriendlyId } ,
750758 } ) ;
751759
760+ const attemptStartedAt = Date . now ( ) ;
761+
752762 const start = await this . httpClient . startRunAttempt ( runFriendlyId , snapshotFriendlyId , {
753763 isWarmStart,
754764 } ) ;
@@ -760,28 +770,58 @@ class ManagedRunController {
760770 return ;
761771 }
762772
773+ const attemptDuration = Date . now ( ) - attemptStartedAt ;
774+
763775 const { run, snapshot, execution, envVars } = start . data ;
764776
765777 logger . debug ( "[ManagedRunController] Started run" , {
766778 runId : run . friendlyId ,
767779 snapshot : snapshot . friendlyId ,
768780 } ) ;
769781
770- // TODO: We may already be executing this run, this may be a new attempt
771- // This is the only case where incrementing the attempt number is allowed
772782 this . enterRunPhase ( run , snapshot ) ;
773783
784+ const metrics = [
785+ {
786+ name : "start" ,
787+ event : "create_attempt" ,
788+ timestamp : attemptStartedAt ,
789+ duration : attemptDuration ,
790+ } ,
791+ ]
792+ . concat (
793+ dequeuedAt
794+ ? [
795+ {
796+ name : "start" ,
797+ event : "dequeue" ,
798+ timestamp : dequeuedAt . getTime ( ) ,
799+ duration : 0 ,
800+ } ,
801+ ]
802+ : [ ]
803+ )
804+ . concat (
805+ podScheduledAt
806+ ? [
807+ {
808+ name : "start" ,
809+ event : "pod_scheduled" ,
810+ timestamp : podScheduledAt . getTime ( ) ,
811+ duration : 0 ,
812+ } ,
813+ ]
814+ : [ ]
815+ ) satisfies TaskRunExecutionMetrics ;
816+
774817 const taskRunEnv = {
775818 ...gatherProcessEnv ( ) ,
776819 ...envVars ,
777820 } ;
778821
779822 try {
780- return await this . executeRun ( { run, snapshot, envVars : taskRunEnv , execution } ) ;
823+ return await this . executeRun ( { run, snapshot, envVars : taskRunEnv , execution, metrics } ) ;
781824 } catch ( error ) {
782- // TODO: Handle the case where we're in the warm start phase or executing a new run
783- // This can happen if we kill the run while it's still executing, e.g. after receiving an attempt number mismatch
784-
785825 console . error ( "Error while executing attempt" , {
786826 error,
787827 } ) ;
@@ -810,8 +850,6 @@ class ManagedRunController {
810850 error : completionResult . error ,
811851 } ) ;
812852
813- // TODO: Maybe we should keep retrying for a while longer
814-
815853 this . waitForNextRun ( ) ;
816854 return ;
817855 }
@@ -923,6 +961,7 @@ class ManagedRunController {
923961 this . startAndExecuteRunAttempt ( {
924962 runFriendlyId : nextRun . run . friendlyId ,
925963 snapshotFriendlyId : nextRun . snapshot . friendlyId ,
964+ dequeuedAt : nextRun . dequeuedAt ,
926965 isWarmStart : true ,
927966 } ) . finally ( ( ) => { } ) ;
928967 return ;
@@ -1032,7 +1071,10 @@ class ManagedRunController {
10321071 snapshot,
10331072 envVars,
10341073 execution,
1035- } : WorkloadRunAttemptStartResponseBody ) {
1074+ metrics,
1075+ } : WorkloadRunAttemptStartResponseBody & {
1076+ metrics ?: TaskRunExecutionMetrics ;
1077+ } ) {
10361078 this . snapshotPoller . start ( ) ;
10371079
10381080 if ( ! this . taskRunProcess || ! this . taskRunProcess . isPreparedForNextRun ) {
@@ -1058,6 +1100,7 @@ class ManagedRunController {
10581100 payload : {
10591101 execution,
10601102 traceContext : execution . run . traceContext ?? { } ,
1103+ metrics,
10611104 } ,
10621105 messageId : run . friendlyId ,
10631106 env : envVars ,
@@ -1212,6 +1255,8 @@ class ManagedRunController {
12121255 this . startAndExecuteRunAttempt ( {
12131256 runFriendlyId : env . TRIGGER_RUN_ID ,
12141257 snapshotFriendlyId : env . TRIGGER_SNAPSHOT_ID ,
1258+ dequeuedAt : new Date ( ) ,
1259+ podScheduledAt : env . TRIGGER_POD_SCHEDULED_AT_MS ,
12151260 } ) . finally ( ( ) => { } ) ;
12161261 return ;
12171262 }
0 commit comments