55 LogLevel,
66 RunExecutionData,
77 TaskRunExecution,
8+ TaskRunExecutionMetrics,
89 TaskRunExecutionResult,
910 TaskRunFailedExecutionResult,
1011} from "@trigger.dev/core/v3";
@@ -475,17 +476,21 @@ export class DevRunController {
475476 private async startAndExecuteRunAttempt({
476477 runFriendlyId,
477478 snapshotFriendlyId,
479+ dequeuedAt,
478480 isWarmStart = false,
479481 }: {
480482 runFriendlyId: string;
481483 snapshotFriendlyId: string;
484+ dequeuedAt?: Date;
482485 isWarmStart?: boolean;
483486 }) {
484487 this.subscribeToRunNotifications({
485488 run: { friendlyId: runFriendlyId },
486489 snapshot: { friendlyId: snapshotFriendlyId },
487490 });
488491
492+ const attemptStartedAt = Date.now();
493+
489494 const start = await this.httpClient.dev.startRunAttempt(runFriendlyId, snapshotFriendlyId);
490495
491496 if (!start.success) {
@@ -495,6 +500,8 @@ export class DevRunController {
495500 return;
496501 }
497502
503+ const attemptDuration = Date.now() - attemptStartedAt;
504+
498505 const { run, snapshot, execution, envVars } = start.data;
499506
500507 eventBus.emit("runStarted", this.opts.worker, execution);
@@ -508,8 +515,28 @@ export class DevRunController {
508515 // This is the only case where incrementing the attempt number is allowed
509516 this.enterRunPhase(run, snapshot);
510517
518+ const metrics = [
519+ {
520+ name: "start",
521+ event: "create_attempt",
522+ timestamp: attemptStartedAt,
523+ duration: attemptDuration,
524+ },
525+ ].concat(
526+ dequeuedAt
527+ ? [
528+ {
529+ name: "start",
530+ event: "dequeue",
531+ timestamp: dequeuedAt.getTime(),
532+ duration: 0,
533+ },
534+ ]
535+ : []
536+ );
537+
511538 try {
512- return await this.executeRun({ run, snapshot, execution, envVars });
539+ return await this.executeRun({ run, snapshot, execution, envVars, metrics });
513540 } catch (error) {
514541 // TODO: Handle the case where we're in the warm start phase or executing a new run
515542 // This can happen if we kill the run while it's still executing, e.g. after receiving an attempt number mismatch
@@ -566,7 +593,10 @@ export class DevRunController {
566593 snapshot,
567594 execution,
568595 envVars,
569- }: WorkloadRunAttemptStartResponseBody) {
596+ metrics,
597+ }: WorkloadRunAttemptStartResponseBody & {
598+ metrics?: TaskRunExecutionMetrics;
599+ }) {
570600 if (!this.opts.worker.serverWorker) {
571601 throw new Error(`No server worker for Dev ${run.friendlyId}`);
572602 }
@@ -594,6 +624,7 @@ export class DevRunController {
594624 payload: {
595625 execution,
596626 traceContext: execution.run.traceContext ?? {},
627+ metrics,
597628 },
598629 messageId: run.friendlyId,
599630 });
@@ -753,6 +784,7 @@ export class DevRunController {
753784 await this.startAndExecuteRunAttempt({
754785 runFriendlyId: dequeueMessage.run.friendlyId,
755786 snapshotFriendlyId: dequeueMessage.snapshot.friendlyId,
787+ dequeuedAt: dequeueMessage.dequeuedAt,
756788 }).finally(async () => {});
757789 }
758790
0 commit comments