|
| 1 | +import { WorkspaceUpdateCancelledError } from "../api/updateParameters"; |
| 2 | + |
| 3 | +import type { |
| 4 | + Workspace, |
| 5 | + WorkspaceAgent, |
| 6 | + WorkspaceAgentLifecycle, |
| 7 | + WorkspaceAgentStatus, |
| 8 | + WorkspaceBuild, |
| 9 | + WorkspaceBuildParameter, |
| 10 | + WorkspaceStatus, |
| 11 | +} from "coder/site/src/api/typesGenerated"; |
| 12 | + |
| 13 | +import type { TelemetryReporter } from "../telemetry/reporter"; |
| 14 | + |
| 15 | +/** Sentinel for `from*` before any state is observed. `"unknown"` is a real server-reported value, so avoid it. */ |
| 16 | +const INITIAL_STATE = "none"; |
| 17 | + |
| 18 | +/** Statuses where a provisioner job is actively running. */ |
| 19 | +const PROVISIONING_STATUSES: ReadonlySet<WorkspaceStatus> = new Set([ |
| 20 | + "pending", |
| 21 | + "starting", |
| 22 | + "stopping", |
| 23 | + "canceling", |
| 24 | + "deleting", |
| 25 | +]); |
| 26 | + |
| 27 | +interface ObservedWorkspaceState { |
| 28 | + readonly status: WorkspaceStatus; |
| 29 | + readonly transition: WorkspaceBuild["transition"]; |
| 30 | + readonly reason: WorkspaceBuild["reason"]; |
| 31 | + readonly observedAtMs: number; |
| 32 | +} |
| 33 | + |
| 34 | +interface ObservedAgentState { |
| 35 | + readonly status: WorkspaceAgentStatus; |
| 36 | + readonly lifecycleState: WorkspaceAgentLifecycle; |
| 37 | + readonly observedAtMs: number; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Emits `workspace.state_transitioned` as a workspace progresses through |
| 42 | + * statuses, plus `observedBuildDurationMs` when a provisioner run resolves. |
| 43 | + * Construct one per workspace; `WorkspaceMonitor` is the sole call site. |
| 44 | + */ |
| 45 | +export class WorkspaceStateTelemetry { |
| 46 | + private observed: ObservedWorkspaceState | undefined; |
| 47 | + /** Set on first observation of a provisioning status; cleared when the build resolves. */ |
| 48 | + private buildStartedAtMs: number | undefined; |
| 49 | + |
| 50 | + public constructor( |
| 51 | + private readonly telemetry: TelemetryReporter, |
| 52 | + private readonly workspaceName: string, |
| 53 | + ) {} |
| 54 | + |
| 55 | + public observe(workspace: Workspace): void { |
| 56 | + const { status, transition, reason } = workspace.latest_build; |
| 57 | + const previous = this.observed; |
| 58 | + if ( |
| 59 | + previous?.status === status && |
| 60 | + previous.transition === transition && |
| 61 | + previous.reason === reason |
| 62 | + ) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + const now = performance.now(); |
| 67 | + const measurements: Record<string, number> = previous |
| 68 | + ? { observedDurationMs: now - previous.observedAtMs } |
| 69 | + : {}; |
| 70 | + |
| 71 | + const wasProvisioning = |
| 72 | + previous && PROVISIONING_STATUSES.has(previous.status); |
| 73 | + const isProvisioning = PROVISIONING_STATUSES.has(status); |
| 74 | + if (isProvisioning) { |
| 75 | + this.buildStartedAtMs ??= now; |
| 76 | + } else { |
| 77 | + if (wasProvisioning && this.buildStartedAtMs !== undefined) { |
| 78 | + measurements.observedBuildDurationMs = now - this.buildStartedAtMs; |
| 79 | + } |
| 80 | + this.buildStartedAtMs = undefined; |
| 81 | + } |
| 82 | + |
| 83 | + this.telemetry.log( |
| 84 | + "workspace.state_transitioned", |
| 85 | + { |
| 86 | + workspaceName: this.workspaceName, |
| 87 | + from: previous?.status ?? INITIAL_STATE, |
| 88 | + to: status, |
| 89 | + transition, |
| 90 | + reason, |
| 91 | + }, |
| 92 | + measurements, |
| 93 | + ); |
| 94 | + this.observed = { status, transition, reason, observedAtMs: now }; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Emits `workspace.agent.state_transitioned` as the agent's `status` and |
| 100 | + * `lifecycle_state` change. The agent has two state dimensions so the event |
| 101 | + * carries qualified `fromStatus`/`toStatus` and `fromLifecycleState`/ |
| 102 | + * `toLifecycleState` properties. Construct one per workspace. |
| 103 | + */ |
| 104 | +export class WorkspaceAgentTelemetry { |
| 105 | + private observed: ObservedAgentState | undefined; |
| 106 | + |
| 107 | + public constructor( |
| 108 | + private readonly telemetry: TelemetryReporter, |
| 109 | + private readonly workspaceName: string, |
| 110 | + ) {} |
| 111 | + |
| 112 | + public observe(agent: WorkspaceAgent): void { |
| 113 | + const previous = this.observed; |
| 114 | + if ( |
| 115 | + previous?.status === agent.status && |
| 116 | + previous.lifecycleState === agent.lifecycle_state |
| 117 | + ) { |
| 118 | + return; |
| 119 | + } |
| 120 | + const now = performance.now(); |
| 121 | + |
| 122 | + this.telemetry.log( |
| 123 | + "workspace.agent.state_transitioned", |
| 124 | + { |
| 125 | + workspaceName: this.workspaceName, |
| 126 | + agentName: agent.name, |
| 127 | + fromStatus: previous?.status ?? INITIAL_STATE, |
| 128 | + toStatus: agent.status, |
| 129 | + fromLifecycleState: previous?.lifecycleState ?? INITIAL_STATE, |
| 130 | + toLifecycleState: agent.lifecycle_state, |
| 131 | + }, |
| 132 | + previous ? { observedDurationMs: now - previous.observedAtMs } : {}, |
| 133 | + ); |
| 134 | + this.observed = { |
| 135 | + status: agent.status, |
| 136 | + lifecycleState: agent.lifecycle_state, |
| 137 | + observedAtMs: now, |
| 138 | + }; |
| 139 | + } |
| 140 | + |
| 141 | + public reset(): void { |
| 142 | + this.observed = undefined; |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +/** |
| 147 | + * Wraps user-initiated workspace operations (start, update) as traced spans. |
| 148 | + * Stateless; safe to construct per call site. |
| 149 | + */ |
| 150 | +export class WorkspaceOperationTelemetry { |
| 151 | + public constructor( |
| 152 | + private readonly telemetry: TelemetryReporter, |
| 153 | + private readonly workspaceName: string, |
| 154 | + ) {} |
| 155 | + |
| 156 | + public traceUpdateTriggered<T>(fn: () => Promise<T>): Promise<T> { |
| 157 | + return this.telemetry.trace("workspace.update.triggered", fn, { |
| 158 | + workspaceName: this.workspaceName, |
| 159 | + }); |
| 160 | + } |
| 161 | + |
| 162 | + public traceStartTriggered<T>(fn: () => Promise<T>): Promise<T> { |
| 163 | + return this.telemetry.trace("workspace.start.triggered", fn, { |
| 164 | + workspaceName: this.workspaceName, |
| 165 | + }); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Records dismissal as `result: "aborted"`. The framework treats any throw |
| 170 | + * as `result: "error"`, so we return inside the span and rethrow outside. |
| 171 | + */ |
| 172 | + public async traceUpdatePrompted( |
| 173 | + fn: () => Promise<WorkspaceBuildParameter[]>, |
| 174 | + ): Promise<WorkspaceBuildParameter[]> { |
| 175 | + let cancel: WorkspaceUpdateCancelledError | undefined; |
| 176 | + const parameters = await this.telemetry.trace( |
| 177 | + "workspace.update.prompted", |
| 178 | + async (span) => { |
| 179 | + try { |
| 180 | + return await fn(); |
| 181 | + } catch (error) { |
| 182 | + if (error instanceof WorkspaceUpdateCancelledError) { |
| 183 | + span.markAborted(); |
| 184 | + cancel = error; |
| 185 | + return []; |
| 186 | + } |
| 187 | + throw error; |
| 188 | + } |
| 189 | + }, |
| 190 | + { workspaceName: this.workspaceName }, |
| 191 | + ); |
| 192 | + if (cancel) throw cancel; |
| 193 | + return parameters; |
| 194 | + } |
| 195 | +} |
0 commit comments