From 933cf0f1196938292ea3d63961feab758bf7df78 Mon Sep 17 00:00:00 2001 From: Eric Allam Date: Wed, 28 Feb 2024 17:01:31 +0000 Subject: [PATCH] v3: run.isTest now propagates through child tasks and the trace (#917) * run.isTest now propagates through child tasks and the trace * Try to fix the pr checks docker hub rate limit error --- .github/workflows/e2e.yml | 5 ++--- apps/webapp/app/v3/eventRepository.server.ts | 5 +++++ .../app/v3/marqs/devQueueConsumer.server.ts | 1 + apps/webapp/app/v3/otlpExporter.server.ts | 16 +++++++++++++++- apps/webapp/app/v3/services/testTask.server.ts | 14 ++++++-------- .../webapp/app/v3/services/triggerTask.server.ts | 5 ++--- packages/core/src/v3/schemas/api.ts | 1 + packages/core/src/v3/schemas/common.ts | 1 + .../core/src/v3/semanticInternalAttributes.ts | 1 + packages/core/src/v3/tasks/taskContextManager.ts | 1 + .../migration.sql | 2 ++ packages/database/prisma/schema.prisma | 3 ++- packages/trigger-sdk/src/v3/shared.ts | 4 ++++ 13 files changed, 43 insertions(+), 16 deletions(-) create mode 100644 packages/database/prisma/migrations/20240228161621_add_run_is_test_to_task_events/migration.sql diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index d01b8752c..0b8e39782 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -7,11 +7,10 @@ jobs: runs-on: buildjet-4vcpu-ubuntu-2204 steps: - name: 🐳 Login to Docker Hub - if: github.event_name == 'push' uses: docker/login-action@v2 with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_TOKEN }} + username: ${{ secrets.DOCKERHUB_USERNAME || vars.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN || vars.DOCKERHUB_TOKEN }} - name: ⬇️ Checkout repo uses: actions/checkout@v3 diff --git a/apps/webapp/app/v3/eventRepository.server.ts b/apps/webapp/app/v3/eventRepository.server.ts index 3a9328fc3..8fa5ccaa3 100644 --- a/apps/webapp/app/v3/eventRepository.server.ts +++ b/apps/webapp/app/v3/eventRepository.server.ts @@ -42,6 +42,7 @@ export type TraceAttributes = Partial< | "attemptId" | "isError" | "runId" + | "runIsTest" | "output" | "metadata" | "properties" @@ -376,6 +377,7 @@ export class EventRepository { [SemanticInternalAttributes.PROJECT_ID]: options.environment.projectId, [SemanticInternalAttributes.PROJECT_REF]: options.environment.project.externalRef, [SemanticInternalAttributes.RUN_ID]: options.attributes.runId, + [SemanticInternalAttributes.RUN_IS_TEST]: options.attributes.runIsTest ?? false, [SemanticInternalAttributes.TASK_SLUG]: options.taskSlug, [SemanticResourceAttributes.SERVICE_NAME]: "api server", [SemanticResourceAttributes.SERVICE_NAMESPACE]: "trigger.dev", @@ -410,6 +412,7 @@ export class EventRepository { projectId: options.environment.projectId, projectRef: options.environment.project.externalRef, runId: options.attributes.runId, + runIsTest: options.attributes.runIsTest ?? false, taskSlug: options.taskSlug, queueId: options.attributes.queueId, queueName: options.attributes.queueName, @@ -489,6 +492,7 @@ export class EventRepository { [SemanticInternalAttributes.PROJECT_ID]: options.environment.projectId, [SemanticInternalAttributes.PROJECT_REF]: options.environment.project.externalRef, [SemanticInternalAttributes.RUN_ID]: options.attributes.runId, + [SemanticInternalAttributes.RUN_IS_TEST]: options.attributes.runIsTest ?? false, [SemanticInternalAttributes.TASK_SLUG]: options.taskSlug, [SemanticResourceAttributes.SERVICE_NAME]: "api server", [SemanticResourceAttributes.SERVICE_NAMESPACE]: "trigger.dev", @@ -524,6 +528,7 @@ export class EventRepository { projectId: options.environment.projectId, projectRef: options.environment.project.externalRef, runId: options.attributes.runId, + runIsTest: options.attributes.runIsTest ?? false, taskSlug: options.taskSlug, queueId: options.attributes.queueId, queueName: options.attributes.queueName, diff --git a/apps/webapp/app/v3/marqs/devQueueConsumer.server.ts b/apps/webapp/app/v3/marqs/devQueueConsumer.server.ts index 96598ad28..7d9690b65 100644 --- a/apps/webapp/app/v3/marqs/devQueueConsumer.server.ts +++ b/apps/webapp/app/v3/marqs/devQueueConsumer.server.ts @@ -386,6 +386,7 @@ export class DevQueueConsumer { context: lockedTaskRun.context, createdAt: lockedTaskRun.createdAt, tags: lockedTaskRun.tags.map((tag) => tag.name), + isTest: lockedTaskRun.isTest, }, queue: { id: queue.friendlyId, diff --git a/apps/webapp/app/v3/otlpExporter.server.ts b/apps/webapp/app/v3/otlpExporter.server.ts index feed2ec40..ed23c7e7b 100644 --- a/apps/webapp/app/v3/otlpExporter.server.ts +++ b/apps/webapp/app/v3/otlpExporter.server.ts @@ -25,7 +25,6 @@ import { CreatableEventEnvironmentType, } from "./eventRepository.server"; import { logger } from "~/services/logger.server"; -import { env } from "~/env.server"; export type OTLPExporterConfig = { batchSize: number; @@ -263,6 +262,7 @@ function extractResourceProperties(attributes: KeyValue[]) { "unknown" ), runId: extractStringAttribute(attributes, SemanticInternalAttributes.RUN_ID, "unknown"), + runIsTest: extractBooleanAttribute(attributes, SemanticInternalAttributes.RUN_IS_TEST, false), attemptId: extractStringAttribute(attributes, SemanticInternalAttributes.ATTEMPT_ID), attemptNumber: extractNumberAttribute(attributes, SemanticInternalAttributes.ATTEMPT_NUMBER), taskSlug: extractStringAttribute(attributes, SemanticInternalAttributes.TASK_SLUG, "unknown"), @@ -506,6 +506,20 @@ function extractNumberAttribute( return isIntValue(attribute?.value) ? Number(attribute.value.value.intValue) : fallback; } +function extractBooleanAttribute(attributes: KeyValue[], name: string): boolean | undefined; +function extractBooleanAttribute(attributes: KeyValue[], name: string, fallback: boolean): boolean; +function extractBooleanAttribute( + attributes: KeyValue[], + name: string, + fallback?: boolean +): boolean | undefined { + const attribute = attributes.find((attribute) => attribute.key === name); + + if (!attribute) return fallback; + + return isBoolValue(attribute?.value) ? attribute.value.value.boolValue : fallback; +} + function isPartialSpan(span: Span): boolean { if (!span.attributes) return false; diff --git a/apps/webapp/app/v3/services/testTask.server.ts b/apps/webapp/app/v3/services/testTask.server.ts index 27289dfe5..d21c5a792 100644 --- a/apps/webapp/app/v3/services/testTask.server.ts +++ b/apps/webapp/app/v3/services/testTask.server.ts @@ -16,13 +16,11 @@ export class TestTaskService extends BaseService { } const triggerTaskService = new TriggerTaskService(); - return await triggerTaskService.call( - data.taskIdentifier, - authenticatedEnvironment, - { payload: data.payload }, - { - isTest: true, - } - ); + return await triggerTaskService.call(data.taskIdentifier, authenticatedEnvironment, { + payload: data.payload, + options: { + test: true, + }, + }); } } diff --git a/apps/webapp/app/v3/services/triggerTask.server.ts b/apps/webapp/app/v3/services/triggerTask.server.ts index 404966606..183bcd5b3 100644 --- a/apps/webapp/app/v3/services/triggerTask.server.ts +++ b/apps/webapp/app/v3/services/triggerTask.server.ts @@ -2,7 +2,6 @@ import { PRIMARY_VARIANT, SemanticInternalAttributes, TriggerTaskRequestBody, - flattenAttributes, } from "@trigger.dev/core/v3"; import { nanoid } from "nanoid"; import { createHash } from "node:crypto"; @@ -17,7 +16,6 @@ export type TriggerTaskServiceOptions = { idempotencyKey?: string; triggerVersion?: string; traceContext?: Record; - isTest?: boolean; }; export class TriggerTaskService extends BaseService { @@ -61,6 +59,7 @@ export class TriggerTaskService extends BaseService { icon: "play", variant: PRIMARY_VARIANT, }, + runIsTest: body.options?.test ?? false, }, incomplete: true, immediate: true, @@ -112,7 +111,7 @@ export class TriggerTaskService extends BaseService { lockedToVersionId: lockedToBackgroundWorker?.id, concurrencyKey: body.options?.concurrencyKey, queue: queueName, - isTest: options.isTest ?? false, + isTest: body.options?.test ?? false, }, }); diff --git a/packages/core/src/v3/schemas/api.ts b/packages/core/src/v3/schemas/api.ts index 2fe854b5e..010e69df4 100644 --- a/packages/core/src/v3/schemas/api.ts +++ b/packages/core/src/v3/schemas/api.ts @@ -41,6 +41,7 @@ export const TriggerTaskRequestBody = z.object({ lockToVersion: z.string().optional(), queue: QueueOptions.optional(), concurrencyKey: z.string().optional(), + test: z.boolean().optional(), }) .optional(), }); diff --git a/packages/core/src/v3/schemas/common.ts b/packages/core/src/v3/schemas/common.ts index b8f6e57a2..93b1e2fd0 100644 --- a/packages/core/src/v3/schemas/common.ts +++ b/packages/core/src/v3/schemas/common.ts @@ -51,6 +51,7 @@ export const TaskRun = z.object({ payloadType: z.string(), context: z.any(), tags: z.array(z.string()), + isTest: z.boolean().default(false), createdAt: z.coerce.date(), }); diff --git a/packages/core/src/v3/semanticInternalAttributes.ts b/packages/core/src/v3/semanticInternalAttributes.ts index 64b17fe01..f7b9916c5 100644 --- a/packages/core/src/v3/semanticInternalAttributes.ts +++ b/packages/core/src/v3/semanticInternalAttributes.ts @@ -11,6 +11,7 @@ export const SemanticInternalAttributes = { ATTEMPT_ID: "ctx.attempt.id", ATTEMPT_NUMBER: "ctx.attempt.number", RUN_ID: "ctx.run.id", + RUN_IS_TEST: "ctx.run.isTest", TASK_SLUG: "ctx.task.id", TASK_PATH: "ctx.task.filePath", TASK_EXPORT_NAME: "ctx.task.exportName", diff --git a/packages/core/src/v3/tasks/taskContextManager.ts b/packages/core/src/v3/tasks/taskContextManager.ts index 919572ed7..ab7168ae7 100644 --- a/packages/core/src/v3/tasks/taskContextManager.ts +++ b/packages/core/src/v3/tasks/taskContextManager.ts @@ -80,6 +80,7 @@ export class TaskContextManager { [SemanticInternalAttributes.PROJECT_REF]: this.ctx.project.ref, [SemanticInternalAttributes.PROJECT_NAME]: this.ctx.project.name, [SemanticInternalAttributes.RUN_ID]: this.ctx.run.id, + [SemanticInternalAttributes.RUN_IS_TEST]: this.ctx.run.isTest, [SemanticInternalAttributes.ORGANIZATION_SLUG]: this.ctx.organization.slug, [SemanticInternalAttributes.ORGANIZATION_NAME]: this.ctx.organization.name, }; diff --git a/packages/database/prisma/migrations/20240228161621_add_run_is_test_to_task_events/migration.sql b/packages/database/prisma/migrations/20240228161621_add_run_is_test_to_task_events/migration.sql new file mode 100644 index 000000000..e097ddd13 --- /dev/null +++ b/packages/database/prisma/migrations/20240228161621_add_run_is_test_to_task_events/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "TaskEvent" ADD COLUMN "runIsTest" BOOLEAN NOT NULL DEFAULT false; diff --git a/packages/database/prisma/schema.prisma b/packages/database/prisma/schema.prisma index 750bed849..53b5ebb9c 100644 --- a/packages/database/prisma/schema.prisma +++ b/packages/database/prisma/schema.prisma @@ -1724,7 +1724,8 @@ model TaskEvent { projectId String projectRef String - runId String + runId String + runIsTest Boolean @default(false) taskSlug String taskPath String? diff --git a/packages/trigger-sdk/src/v3/shared.ts b/packages/trigger-sdk/src/v3/shared.ts index ac5c6e696..04822da2c 100644 --- a/packages/trigger-sdk/src/v3/shared.ts +++ b/packages/trigger-sdk/src/v3/shared.ts @@ -165,6 +165,7 @@ export function createTask( options: { queue: params.queue, concurrencyKey: options?.concurrencyKey, + test: taskContextManager.ctx?.run.isTest, }, }); @@ -221,6 +222,7 @@ export function createTask( options: { queue: item.options?.queue ?? params.queue, concurrencyKey: item.options?.concurrencyKey, + test: taskContextManager.ctx?.run.isTest, }, })), }); @@ -287,6 +289,7 @@ export function createTask( lockToVersion: taskContextManager.worker?.version, // Lock to current version because we're waiting for it to finish queue: params.queue, concurrencyKey: options?.concurrencyKey, + test: taskContextManager.ctx?.run.isTest, }, }); @@ -357,6 +360,7 @@ export function createTask( lockToVersion: taskContextManager.worker?.version, queue: item.options?.queue ?? params.queue, concurrencyKey: item.options?.concurrencyKey, + test: taskContextManager.ctx?.run.isTest, }, })), dependentAttempt: ctx.attempt.id,