|
| 1 | +import { BuildServerMetadata } from "@trigger.dev/core/v3"; |
| 2 | + |
| 3 | +// Attribute names for the deployment telemetry events (see |
| 4 | +// DEPLOYMENT_TELEMETRY_ATTRIBUTES.md next to this file). This module is the single owner of these names — Axiom |
| 5 | +// queries, dashboards, and monitors reference them, so treat renames as |
| 6 | +// breaking changes. |
| 7 | +export const DeploymentTelemetryAttributes = { |
| 8 | + ORG_ID: "$trigger.org.id", |
| 9 | + PROJECT_ID: "$trigger.project.id", |
| 10 | + PROJECT_REF: "$trigger.project.ref", |
| 11 | + ENV_ID: "$trigger.env.id", |
| 12 | + ENV_TYPE: "$trigger.env.type", |
| 13 | + DEPLOYMENT_ID: "deployment.id", |
| 14 | + VERSION: "deployment.version", |
| 15 | + STATUS: "deployment.status", |
| 16 | + SUCCESS: "deployment.success", |
| 17 | + BUILD_PATH: "deployment.build_path", |
| 18 | + WORKER_TYPE: "deployment.worker_type", |
| 19 | + RUNTIME: "deployment.runtime", |
| 20 | + RUNTIME_VERSION: "deployment.runtime_version", |
| 21 | + CLI_VERSION: "deployment.cli_version", |
| 22 | + TRIGGERED_VIA: "deployment.triggered_via", |
| 23 | + COMMIT_SHA: "deployment.commit_sha", |
| 24 | + ERROR_NAME: "deployment.error.name", |
| 25 | + ERROR_MESSAGE: "deployment.error.message", |
| 26 | + CANCELED_REASON: "deployment.canceled_reason", |
| 27 | + DURATION_TOTAL_MS: "deployment.duration.total_ms", |
| 28 | + DURATION_QUEUE_MS: "deployment.duration.queue_ms", |
| 29 | + DURATION_INSTALL_MS: "deployment.duration.install_ms", |
| 30 | + DURATION_BUILDING_MS: "deployment.duration.building_ms", |
| 31 | + DURATION_DEPLOYING_MS: "deployment.duration.deploying_ms", |
| 32 | +} as const; |
| 33 | + |
| 34 | +export type DeploymentBuildPath = "local_bundle" | "native" | "depot"; |
| 35 | + |
| 36 | +/** |
| 37 | + * Classifies which build path produced a deployment, from its persisted |
| 38 | + * metadata. Everything that is not a native-build-server deployment falls into |
| 39 | + * the depot bucket — including rare `--local-build` deploys, whose flag is not |
| 40 | + * persisted. `externalBuildData` is NOT usable as a depot signal: init writes a |
| 41 | + * placeholder (`"-"` fields) for every path. |
| 42 | + */ |
| 43 | +export function deriveBuildPath(buildServerMetadata: unknown): DeploymentBuildPath { |
| 44 | + const metadata = BuildServerMetadata.safeParse(buildServerMetadata); |
| 45 | + |
| 46 | + if (metadata.success && metadata.data.isNativeBuild) { |
| 47 | + return metadata.data.fromBundle ? "local_bundle" : "native"; |
| 48 | + } |
| 49 | + |
| 50 | + return "depot"; |
| 51 | +} |
| 52 | + |
| 53 | +export type DeploymentTimestamps = { |
| 54 | + createdAt: Date; |
| 55 | + startedAt?: Date | null; |
| 56 | + installedAt?: Date | null; |
| 57 | + builtAt?: Date | null; |
| 58 | +}; |
| 59 | + |
| 60 | +export type DeploymentDurations = { |
| 61 | + totalMs: number; |
| 62 | + queueMs?: number; |
| 63 | + installMs?: number; |
| 64 | + buildingMs?: number; |
| 65 | + deployingMs?: number; |
| 66 | +}; |
| 67 | + |
| 68 | +/** |
| 69 | + * Derives per-phase durations from the persisted timestamp chain |
| 70 | + * (createdAt → startedAt → installedAt → builtAt → terminal). Chains are |
| 71 | + * path-shaped: depot never sets installedAt (the /progress route is |
| 72 | + * build-server-only) and PENDING-skipping deploys have queue ≈ 0 — each phase |
| 73 | + * is emitted only when both of its boundary timestamps exist and are ordered. |
| 74 | + */ |
| 75 | +export function deriveDeploymentDurations( |
| 76 | + timestamps: DeploymentTimestamps, |
| 77 | + terminalAt: Date |
| 78 | +): DeploymentDurations { |
| 79 | + const { createdAt, startedAt, installedAt, builtAt } = timestamps; |
| 80 | + const buildingFrom = installedAt ?? startedAt; |
| 81 | + |
| 82 | + return { |
| 83 | + totalMs: Math.max(terminalAt.getTime() - createdAt.getTime(), 0), |
| 84 | + queueMs: msBetween(createdAt, startedAt), |
| 85 | + installMs: msBetween(startedAt, installedAt), |
| 86 | + buildingMs: msBetween(buildingFrom, builtAt), |
| 87 | + deployingMs: msBetween(builtAt, terminalAt), |
| 88 | + }; |
| 89 | +} |
| 90 | + |
| 91 | +function msBetween(from?: Date | null, to?: Date | null): number | undefined { |
| 92 | + if (!from || !to) return undefined; |
| 93 | + const ms = to.getTime() - from.getTime(); |
| 94 | + return ms >= 0 ? ms : undefined; |
| 95 | +} |
0 commit comments