|
| 1 | +import { BuildServerMetadata } from "@trigger.dev/core/v3"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Attribute names for the `deployment.finished` and `deployment.initialized` |
| 5 | + * telemetry events (emitted by services/recordDeploymentFinished.server.ts). |
| 6 | + * This module is the single owner of these names — external queries, |
| 7 | + * dashboards, and monitors reference them, so treat renames as breaking. |
| 8 | + * |
| 9 | + * Query gotchas: dedup with `arg_max(_time, *) by deployment.id` (job retries |
| 10 | + * can double-emit); the span's `_time` is the deployment's createdAt, so a |
| 11 | + * TIMED_OUT event lands backdated by up to the full deploy timeout — monitor |
| 12 | + * windows must exceed it; phase durations are omitted (not zero) when a |
| 13 | + * boundary timestamp is missing, and `total_ms` excludes local-bundle's |
| 14 | + * pre-init client work (esbuild + upload) until the CLI reports timings. |
| 15 | + */ |
| 16 | +export const DeploymentTelemetryAttributes = { |
| 17 | + ORG_ID: "$trigger.org.id", |
| 18 | + PROJECT_ID: "$trigger.project.id", |
| 19 | + // Project external ref ("proj_…") |
| 20 | + PROJECT_REF: "$trigger.project.ref", |
| 21 | + ENV_ID: "$trigger.env.id", |
| 22 | + // PRODUCTION / STAGING / PREVIEW / DEVELOPMENT |
| 23 | + ENV_TYPE: "$trigger.env.type", |
| 24 | + // Deployment friendly id — the dedup key |
| 25 | + DEPLOYMENT_ID: "deployment.id", |
| 26 | + VERSION: "deployment.version", |
| 27 | + // finished: terminal status; initialized: initial status (PENDING/BUILDING) |
| 28 | + STATUS: "deployment.status", |
| 29 | + // status === DEPLOYED; CANCELED is excluded from failure rates |
| 30 | + SUCCESS: "deployment.success", |
| 31 | + // depot / native / native_local_bundle (see deriveBuildPath) |
| 32 | + BUILD_PATH: "deployment.build_path", |
| 33 | + // V1 / MANAGED (run engine) |
| 34 | + WORKER_TYPE: "deployment.worker_type", |
| 35 | + RUNTIME: "deployment.runtime", |
| 36 | + // Set at indexing; null for pre-index failures |
| 37 | + RUNTIME_VERSION: "deployment.runtime_version", |
| 38 | + // From x-trigger-cli-version at init; null for pre-column history |
| 39 | + CLI_VERSION: "deployment.cli_version", |
| 40 | + TRIGGERED_VIA: "deployment.triggered_via", |
| 41 | + COMMIT_SHA: "deployment.commit_sha", |
| 42 | + // error.* only on FAILED/TIMED_OUT; CANCELED uses canceled_reason |
| 43 | + ERROR_NAME: "deployment.error.name", |
| 44 | + ERROR_MESSAGE: "deployment.error.message", |
| 45 | + CANCELED_REASON: "deployment.canceled_reason", |
| 46 | + // createdAt → terminal (also the span's own duration) |
| 47 | + DURATION_TOTAL_MS: "deployment.duration.total_ms", |
| 48 | + // createdAt → startedAt; ≈0 when created directly in BUILDING (depot) |
| 49 | + DURATION_QUEUE_MS: "deployment.duration.queue_ms", |
| 50 | + // startedAt → installedAt; build-server paths only (depot never sets it) |
| 51 | + DURATION_INSTALL_MS: "deployment.duration.install_ms", |
| 52 | + // (installedAt ?? startedAt) → builtAt |
| 53 | + DURATION_BUILDING_MS: "deployment.duration.building_ms", |
| 54 | + // builtAt → terminal; for depot dominated by the server-side registry push |
| 55 | + DURATION_DEPLOYING_MS: "deployment.duration.deploying_ms", |
| 56 | +} as const; |
| 57 | + |
| 58 | +export type DeploymentBuildPath = "native_local_bundle" | "native" | "depot"; |
| 59 | + |
| 60 | +/** |
| 61 | + * Everything that is not a native-build-server deployment falls into the depot |
| 62 | + * bucket, including rare `--local-build` deploys (their flag is not persisted). |
| 63 | + * `externalBuildData` is NOT a usable depot signal: init writes a placeholder |
| 64 | + * for every path. |
| 65 | + */ |
| 66 | +export function deriveBuildPath(buildServerMetadata: unknown): DeploymentBuildPath { |
| 67 | + const metadata = BuildServerMetadata.safeParse(buildServerMetadata); |
| 68 | + |
| 69 | + if (metadata.success && metadata.data.isNativeBuild) { |
| 70 | + return metadata.data.fromBundle ? "native_local_bundle" : "native"; |
| 71 | + } |
| 72 | + |
| 73 | + return "depot"; |
| 74 | +} |
| 75 | + |
| 76 | +export type DeploymentTimestamps = { |
| 77 | + createdAt: Date; |
| 78 | + startedAt?: Date | null; |
| 79 | + installedAt?: Date | null; |
| 80 | + builtAt?: Date | null; |
| 81 | +}; |
| 82 | + |
| 83 | +export type DeploymentDurations = { |
| 84 | + totalMs: number; |
| 85 | + queueMs?: number; |
| 86 | + installMs?: number; |
| 87 | + buildingMs?: number; |
| 88 | + deployingMs?: number; |
| 89 | +}; |
| 90 | + |
| 91 | +/** |
| 92 | + * Timestamp chains are path-shaped (e.g. depot never sets installedAt), so |
| 93 | + * each phase is derived only when both of its boundary timestamps exist and |
| 94 | + * are ordered. |
| 95 | + */ |
| 96 | +export function deriveDeploymentDurations( |
| 97 | + timestamps: DeploymentTimestamps, |
| 98 | + terminalAt: Date |
| 99 | +): DeploymentDurations { |
| 100 | + const { createdAt, startedAt, installedAt, builtAt } = timestamps; |
| 101 | + const buildingFrom = installedAt ?? startedAt; |
| 102 | + |
| 103 | + return { |
| 104 | + totalMs: Math.max(terminalAt.getTime() - createdAt.getTime(), 0), |
| 105 | + queueMs: msBetween(createdAt, startedAt), |
| 106 | + installMs: msBetween(startedAt, installedAt), |
| 107 | + buildingMs: msBetween(buildingFrom, builtAt), |
| 108 | + deployingMs: msBetween(builtAt, terminalAt), |
| 109 | + }; |
| 110 | +} |
| 111 | + |
| 112 | +function msBetween(from?: Date | null, to?: Date | null): number | undefined { |
| 113 | + if (!from || !to) return undefined; |
| 114 | + const ms = to.getTime() - from.getTime(); |
| 115 | + return ms >= 0 ? ms : undefined; |
| 116 | +} |
0 commit comments