|
| 1 | +import { SpanStatusCode } from "@opentelemetry/api"; |
| 2 | +import { type WorkerDeploymentStatus } from "@trigger.dev/database"; |
| 3 | +import { logger } from "~/services/logger.server"; |
| 4 | +import { tracer } from "~/v3/tracer.server"; |
| 5 | + |
| 6 | +type TerminalDeploymentStatus = Extract< |
| 7 | + WorkerDeploymentStatus, |
| 8 | + "DEPLOYED" | "FAILED" | "TIMED_OUT" | "CANCELED" |
| 9 | +>; |
| 10 | + |
| 11 | +/** |
| 12 | + * Records a deployment's terminal status as a `deployment.outcome` span so |
| 13 | + * deploy success/failure is queryable from traces (no DB read). Call after each |
| 14 | + * terminal-status write. Org/project/env are best-effort; never throws. |
| 15 | + */ |
| 16 | +export function recordDeploymentOutcome(params: { |
| 17 | + status: TerminalDeploymentStatus; |
| 18 | + deploymentFriendlyId: string; |
| 19 | + organizationId?: string; |
| 20 | + projectId?: string; |
| 21 | + environmentId?: string; |
| 22 | + environmentType?: string; |
| 23 | + reason?: string; |
| 24 | +}): void { |
| 25 | + try { |
| 26 | + const span = tracer.startSpan("deployment.outcome", { |
| 27 | + attributes: { |
| 28 | + "$trigger.org.id": params.organizationId, |
| 29 | + "$trigger.project.id": params.projectId, |
| 30 | + "$trigger.env.id": params.environmentId, |
| 31 | + "$trigger.env.type": params.environmentType, |
| 32 | + "deployment.outcome.status": params.status, |
| 33 | + "deployment.outcome.success": params.status === "DEPLOYED", |
| 34 | + "deployment.outcome.deployment_id": params.deploymentFriendlyId, |
| 35 | + "deployment.outcome.reason": params.reason, |
| 36 | + }, |
| 37 | + }); |
| 38 | + |
| 39 | + if (params.status !== "DEPLOYED") { |
| 40 | + span.setStatus({ code: SpanStatusCode.ERROR, message: params.reason }); |
| 41 | + } |
| 42 | + |
| 43 | + span.end(); |
| 44 | + } catch (error) { |
| 45 | + logger.debug("recordDeploymentOutcome failed", { |
| 46 | + deploymentFriendlyId: params.deploymentFriendlyId, |
| 47 | + error: error instanceof Error ? error.message : String(error), |
| 48 | + }); |
| 49 | + } |
| 50 | +} |
0 commit comments