|
| 1 | +import * as os from "node:os"; |
| 2 | +import * as vscode from "vscode"; |
| 3 | + |
| 4 | +import { toError } from "../error/errorUtils"; |
| 5 | + |
| 6 | +/** Telemetry level, mirrors `coder.telemetry.level`. Ordered: off < local. */ |
| 7 | +export type TelemetryLevel = "off" | "local"; |
| 8 | + |
| 9 | +/** Caller properties. `result` is framework-managed on traced events. */ |
| 10 | +export type CallerProperties = Record<string, string> & { result?: never }; |
| 11 | + |
| 12 | +/** Caller measurements. `durationMs` is framework-managed on traced events. */ |
| 13 | +export type CallerMeasurements = Record<string, number> & { |
| 14 | + durationMs?: never; |
| 15 | +}; |
| 16 | + |
| 17 | +/** Session-stable resource attributes. Field names are inspired by OTel |
| 18 | + * resource attributes; they are camelCase TypeScript and not a 1:1 mapping. */ |
| 19 | +export interface SessionContext { |
| 20 | + readonly extensionVersion: string; |
| 21 | + readonly machineId: string; |
| 22 | + readonly sessionId: string; |
| 23 | + readonly osType: string; |
| 24 | + readonly osVersion: string; |
| 25 | + readonly hostArch: string; |
| 26 | + readonly platformName: string; |
| 27 | + readonly platformVersion: string; |
| 28 | +} |
| 29 | + |
| 30 | +/** Per-event context: session attributes plus the current deployment URL. */ |
| 31 | +export interface TelemetryContext extends SessionContext { |
| 32 | + readonly deploymentUrl: string; |
| 33 | +} |
| 34 | + |
| 35 | +export interface TelemetryEvent { |
| 36 | + readonly eventId: string; |
| 37 | + readonly eventName: string; |
| 38 | + readonly timestamp: string; |
| 39 | + readonly eventSequence: number; |
| 40 | + |
| 41 | + readonly context: TelemetryContext; |
| 42 | + |
| 43 | + readonly properties: Readonly<Record<string, string>>; |
| 44 | + readonly measurements: Readonly<Record<string, number>>; |
| 45 | + |
| 46 | + /** Shared by all events in a trace. Maps to OTel `trace_id`. */ |
| 47 | + readonly traceId?: string; |
| 48 | + /** Set on phase children only. Equals the parent event's `eventId`. Maps to OTel `parent_span_id`. */ |
| 49 | + readonly parentEventId?: string; |
| 50 | + |
| 51 | + readonly error?: Readonly<{ |
| 52 | + message: string; |
| 53 | + type?: string; |
| 54 | + code?: string; |
| 55 | + }>; |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Sink for telemetry events. `write` is sync and must buffer in memory; I/O |
| 60 | + * happens in `flush`/`dispose`. The service filters by `minLevel`; sinks can |
| 61 | + * still self-gate on other signals (e.g. deployment URL). |
| 62 | + */ |
| 63 | +export interface TelemetrySink { |
| 64 | + readonly name: string; |
| 65 | + readonly minLevel: TelemetryLevel; |
| 66 | + write(event: TelemetryEvent): void; |
| 67 | + flush(): Promise<void>; |
| 68 | + dispose(): Promise<void>; |
| 69 | +} |
| 70 | + |
| 71 | +/** Build session attributes. `extensionVersion` falls back to `"unknown"`. */ |
| 72 | +export function buildSession(ctx: vscode.ExtensionContext): SessionContext { |
| 73 | + // "unknown" only for malformed package.json or test fixtures missing `version`. |
| 74 | + const packageJson = ctx.extension.packageJSON as { version?: unknown }; |
| 75 | + const extensionVersion = |
| 76 | + typeof packageJson.version === "string" ? packageJson.version : "unknown"; |
| 77 | + |
| 78 | + return { |
| 79 | + extensionVersion, |
| 80 | + machineId: vscode.env.machineId, |
| 81 | + sessionId: vscode.env.sessionId, |
| 82 | + osType: detectOsType(), |
| 83 | + osVersion: os.release(), |
| 84 | + hostArch: process.arch, |
| 85 | + platformName: vscode.env.appName, |
| 86 | + platformVersion: vscode.version, |
| 87 | + }; |
| 88 | +} |
| 89 | + |
| 90 | +/** Normalize a thrown value into the event's `error` block. */ |
| 91 | +export function buildErrorBlock( |
| 92 | + value: unknown, |
| 93 | +): NonNullable<TelemetryEvent["error"]> { |
| 94 | + const err = toError(value); |
| 95 | + const rawCode = (value as { code?: unknown } | null | undefined)?.code; |
| 96 | + const hasCode = typeof rawCode === "string" || typeof rawCode === "number"; |
| 97 | + return { |
| 98 | + message: err.message, |
| 99 | + ...(err.name && err.name !== "Error" && { type: err.name }), |
| 100 | + ...(hasCode && { code: String(rawCode) }), |
| 101 | + }; |
| 102 | +} |
| 103 | + |
| 104 | +// Node uses "win32" on Windows; OTel's os.type is "windows". |
| 105 | +function detectOsType(): string { |
| 106 | + return process.platform === "win32" ? "windows" : process.platform; |
| 107 | +} |
0 commit comments