Changesets · Branches · Tags · Compare
| Changeset | a660fc8009f5d161343a1bde164cd9fa1db11445db7d617a72f94aa018b9caad |
|---|---|
| Author | Nate Sesti <33237525+sestinj@users.noreply.github.com> · 2025-09-14 20:20 -07:00 |
| Committer | GitHub <noreply@github.com> · 2025-09-14 20:20 -07:00 |
| Parents | c75ade3f86e5, bc35c780e873 |
19 files changed
modified core/indexing/docs/DocsService.ts +2 −2
| @@ -272,7 +272,7 @@ | ||
| 272 | 272 | id: doc.startUrl, |
| 273 | 273 | isReindexing: false, |
| 274 | 274 | title: doc.title, |
| 275 | - debugInfo: `max depth: ${doc.maxDepth}`, | |
| 275 | + debugInfo: `max depth: ${doc.maxDepth ?? "unlimited"}`, | |
| 276 | 276 | icon: doc.faviconUrl, |
| 277 | 277 | url: doc.startUrl, |
| 278 | 278 | }; |
| @@ -468,7 +468,7 @@ | ||
| 468 | 468 | embeddingsProviderId: provider.embeddingId, |
| 469 | 469 | isReindexing: forceReindex && indexExists, |
| 470 | 470 | title: siteIndexingConfig.title, |
| 471 | - debugInfo: `max depth: ${siteIndexingConfig.maxDepth}`, | |
| 471 | + debugInfo: `max depth: ${siteIndexingConfig.maxDepth ?? "unlimited"}`, | |
| 472 | 472 | icon: siteIndexingConfig.faviconUrl, |
| 473 | 473 | url: siteIndexingConfig.startUrl, |
| 474 | 474 | }; |
modified extensions/cli/src/auth/workos.helpers.ts +3 −2
| @@ -2,6 +2,7 @@ | ||
| 2 | 2 | |
| 3 | 3 | import { getApiClient } from "../config.js"; |
| 4 | 4 | import { safeStderr } from "../init.js"; |
| 5 | +import { gracefulExit } from "../util/exit.js"; | |
| 5 | 6 | |
| 6 | 7 | import type { |
| 7 | 8 | AuthConfig, |
| @@ -76,7 +77,7 @@ | ||
| 76 | 77 | "The --org flag is only supported in headless mode (with -p/--print flag)\n", |
| 77 | 78 | ), |
| 78 | 79 | ); |
| 79 | - process.exit(1); | |
| 80 | + await gracefulExit(1); | |
| 80 | 81 | } |
| 81 | 82 | |
| 82 | 83 | const apiClient = getApiClient(authConfig.accessToken); |
| @@ -107,7 +108,7 @@ | ||
| 107 | 108 | "The --org flag is only supported in headless mode (with -p/--print flag)\n", |
| 108 | 109 | ), |
| 109 | 110 | ); |
| 110 | - process.exit(1); | |
| 111 | + await gracefulExit(1); | |
| 111 | 112 | } |
| 112 | 113 | |
| 113 | 114 | const apiClient = getApiClient(authenticatedConfig.accessToken); |
modified extensions/cli/src/commands/chat.ts +3 −2
| @@ -22,6 +22,7 @@ | ||
| 22 | 22 | import { posthogService } from "../telemetry/posthogService.js"; |
| 23 | 23 | import { telemetryService } from "../telemetry/telemetryService.js"; |
| 24 | 24 | import { startTUIChat } from "../ui/index.js"; |
| 25 | +import { gracefulExit } from "../util/exit.js"; | |
| 25 | 26 | import { formatAnthropicError, formatError } from "../util/formatError.js"; |
| 26 | 27 | import { logger } from "../util/logger.js"; |
| 27 | 28 | import { |
| @@ -96,7 +97,7 @@ | ||
| 96 | 97 | return newSession.history; |
| 97 | 98 | } else { |
| 98 | 99 | logger.error(chalk.red(`Session with ID "${options.fork}" not found.`)); |
| 99 | - process.exit(1); | |
| 100 | + await gracefulExit(1); | |
| 100 | 101 | } |
| 101 | 102 | } |
| 102 | 103 | |
| @@ -545,7 +546,7 @@ | ||
| 545 | 546 | context: "chat_command_fatal", |
| 546 | 547 | headless: options.headless, |
| 547 | 548 | }); |
| 548 | - process.exit(1); | |
| 549 | + await gracefulExit(1); | |
| 549 | 550 | } finally { |
| 550 | 551 | // Stop active time tracking |
| 551 | 552 | telemetryService.stopActiveTime(); |
modified extensions/cli/src/commands/login.ts +2 −1
| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | import chalk from "chalk"; |
| 2 | 2 | |
| 3 | 3 | import { login as workosLogin } from "../auth/workos.js"; |
| 4 | +import { gracefulExit } from "../util/exit.js"; | |
| 4 | 5 | |
| 5 | 6 | export async function login() { |
| 6 | 7 | console.info(chalk.yellow("Logging in to Continue...")); |
| @@ -10,6 +11,6 @@ | ||
| 10 | 11 | console.info(chalk.green("Successfully logged in!")); |
| 11 | 12 | } catch (error: any) { |
| 12 | 13 | console.error(chalk.red(`Login failed: ${error.message}`)); |
| 13 | - process.exit(1); | |
| 14 | + await gracefulExit(1); | |
| 14 | 15 | } |
| 15 | 16 | } |
modified extensions/cli/src/commands/remote-test.ts +2 −1
| @@ -2,6 +2,7 @@ | ||
| 2 | 2 | |
| 3 | 3 | import { telemetryService } from "../telemetry/telemetryService.js"; |
| 4 | 4 | import { startRemoteTUIChat } from "../ui/index.js"; |
| 5 | +import { gracefulExit } from "../util/exit.js"; | |
| 5 | 6 | import { logger } from "../util/logger.js"; |
| 6 | 7 | |
| 7 | 8 | export async function remoteTest( |
| @@ -25,6 +26,6 @@ | ||
| 25 | 26 | logger.error( |
| 26 | 27 | chalk.red(`Failed to connect to remote environment: ${error.message}`), |
| 27 | 28 | ); |
| 28 | - process.exit(1); | |
| 29 | + await gracefulExit(1); | |
| 29 | 30 | } |
| 30 | 31 | } |
modified extensions/cli/src/commands/remote.ts +3 −2
| @@ -4,6 +4,7 @@ | ||
| 4 | 4 | import { env } from "../env.js"; |
| 5 | 5 | import { telemetryService } from "../telemetry/telemetryService.js"; |
| 6 | 6 | import { startRemoteTUIChat } from "../ui/index.js"; |
| 7 | +import { gracefulExit } from "../util/exit.js"; | |
| 7 | 8 | import { getRepoUrl } from "../util/git.js"; |
| 8 | 9 | import { logger } from "../util/logger.js"; |
| 9 | 10 | import { readStdinSync } from "../util/stdin.js"; |
| @@ -75,7 +76,7 @@ | ||
| 75 | 76 | console.error( |
| 76 | 77 | chalk.red("Not authenticated. Please run 'cn login' first."), |
| 77 | 78 | ); |
| 78 | - process.exit(1); | |
| 79 | + await gracefulExit(1); | |
| 79 | 80 | } |
| 80 | 81 | |
| 81 | 82 | const accessToken = getAccessToken(authConfig); |
| @@ -157,6 +158,6 @@ | ||
| 157 | 158 | logger.error( |
| 158 | 159 | chalk.red(`Failed to create remote environment: ${error.message}`), |
| 159 | 160 | ); |
| 160 | - process.exit(1); | |
| 161 | + await gracefulExit(1); | |
| 161 | 162 | } |
| 162 | 163 | } |
modified extensions/cli/src/commands/serve.test.ts +1 −0
| @@ -25,6 +25,7 @@ | ||
| 25 | 25 | ensureOrganization: vi.fn(), |
| 26 | 26 | getOrganizationId: vi.fn(), |
| 27 | 27 | isAuthenticated: vi.fn(() => false), |
| 28 | + isAuthenticatedConfig: vi.fn(() => false), | |
| 28 | 29 | })); |
| 29 | 30 | |
| 30 | 31 | vi.mock("../onboarding.js", () => ({ |
modified extensions/cli/src/commands/serve.ts +16 −5
| @@ -22,6 +22,7 @@ | ||
| 22 | 22 | import { createSession } from "../session.js"; |
| 23 | 23 | import { constructSystemMessage } from "../systemMessage.js"; |
| 24 | 24 | import { telemetryService } from "../telemetry/telemetryService.js"; |
| 25 | +import { gracefulExit } from "../util/exit.js"; | |
| 25 | 26 | import { formatError } from "../util/formatError.js"; |
| 26 | 27 | import { logger } from "../util/logger.js"; |
| 27 | 28 | import { readStdinSync } from "../util/stdin.js"; |
| @@ -302,12 +303,16 @@ | ||
| 302 | 303 | } |
| 303 | 304 | |
| 304 | 305 | // Give a moment for the response to be sent |
| 305 | - setTimeout(() => { | |
| 306 | + const handleExitResponse = () => { | |
| 306 | 307 | server.close(() => { |
| 307 | 308 | telemetryService.stopActiveTime(); |
| 308 | - process.exit(0); | |
| 309 | + gracefulExit(0).catch((err) => { | |
| 310 | + logger.error(`Graceful exit failed: ${formatError(err)}`); | |
| 311 | + process.exit(1); | |
| 312 | + }); | |
| 309 | 313 | }); |
| 310 | - }, 100); | |
| 314 | + }; | |
| 315 | + setTimeout(handleExitResponse, 100); | |
| 311 | 316 | }); |
| 312 | 317 | |
| 313 | 318 | const server = app.listen(port, () => { |
| @@ -435,7 +440,10 @@ | ||
| 435 | 440 | state.serverRunning = false; |
| 436 | 441 | server.close(() => { |
| 437 | 442 | telemetryService.stopActiveTime(); |
| 438 | - process.exit(0); | |
| 443 | + gracefulExit(0).catch((err) => { | |
| 444 | + logger.error(`Graceful exit failed: ${formatError(err)}`); | |
| 445 | + process.exit(1); | |
| 446 | + }); | |
| 439 | 447 | }); |
| 440 | 448 | if (inactivityChecker) { |
| 441 | 449 | clearInterval(inactivityChecker); |
| @@ -454,7 +462,10 @@ | ||
| 454 | 462 | } |
| 455 | 463 | server.close(() => { |
| 456 | 464 | telemetryService.stopActiveTime(); |
| 457 | - process.exit(0); | |
| 465 | + gracefulExit(0).catch((err) => { | |
| 466 | + logger.error(`Graceful exit failed: ${formatError(err)}`); | |
| 467 | + process.exit(1); | |
| 468 | + }); | |
| 458 | 469 | }); |
| 459 | 470 | }); |
| 460 | 471 | } |
modified extensions/cli/src/flags/flagValidator.ts +3 −1
| @@ -219,5 +219,7 @@ | ||
| 219 | 219 | safeStderr(`\nFound ${errors.length} validation errors.\n`); |
| 220 | 220 | } |
| 221 | 221 | |
| 222 | - process.exit(1); | |
| 222 | + // Use async graceful exit without awaiting (we must satisfy never) | |
| 223 | + import("../util/exit.js").then(({ gracefulExit }) => gracefulExit(1)); | |
| 224 | + throw new Error("Exiting due to validation errors"); | |
| 223 | 225 | } |
modified extensions/cli/src/freeTrialTransition.ts +5 −4
| @@ -11,6 +11,7 @@ | ||
| 11 | 11 | isValidAnthropicApiKey, |
| 12 | 12 | getApiKeyValidationError, |
| 13 | 13 | } from "./util/apiKeyValidation.js"; |
| 14 | +import { gracefulExit } from "./util/exit.js"; | |
| 14 | 15 | import { updateAnthropicModelInYaml } from "./util/yamlConfigUpdater.js"; |
| 15 | 16 | |
| 16 | 17 | const CONFIG_PATH = path.join(env.continueHome, "config.yaml"); |
| @@ -81,7 +82,7 @@ | ||
| 81 | 82 | |
| 82 | 83 | // Wait for user to acknowledge |
| 83 | 84 | readlineSync.question(chalk.dim("\nPress Enter to exit...")); |
| 84 | - process.exit(0); | |
| 85 | + await gracefulExit(0); | |
| 85 | 86 | } else if (choice === "2") { |
| 86 | 87 | // Option 2: Enter Anthropic API key |
| 87 | 88 | const apiKey = readlineSync.question( |
| @@ -93,7 +94,7 @@ | ||
| 93 | 94 | |
| 94 | 95 | if (!isValidAnthropicApiKey(apiKey)) { |
| 95 | 96 | console.log(chalk.red(`❌ ${getApiKeyValidationError(apiKey)}`)); |
| 96 | - process.exit(1); | |
| 97 | + await gracefulExit(1); | |
| 97 | 98 | } |
| 98 | 99 | |
| 99 | 100 | try { |
| @@ -108,7 +109,7 @@ | ||
| 108 | 109 | } |
| 109 | 110 | } catch (error) { |
| 110 | 111 | console.log(chalk.red(`❌ Error saving API key: ${error}`)); |
| 111 | - process.exit(1); | |
| 112 | + await gracefulExit(1); | |
| 112 | 113 | } |
| 113 | 114 | } |
| 114 | 115 | |
| @@ -137,5 +138,5 @@ | ||
| 137 | 138 | child.unref(); |
| 138 | 139 | |
| 139 | 140 | // Exit the current process |
| 140 | - process.exit(0); | |
| 141 | + await gracefulExit(0); | |
| 141 | 142 | } |
modified extensions/cli/src/index.ts +24 −7
| @@ -19,6 +19,8 @@ | ||
| 19 | 19 | import { configureConsoleForHeadless, safeStderr } from "./init.js"; |
| 20 | 20 | import { sentryService } from "./sentry.js"; |
| 21 | 21 | import { addCommonOptions, mergeParentOptions } from "./shared-options.js"; |
| 22 | +import { posthogService } from "./telemetry/posthogService.js"; | |
| 23 | +import { gracefulExit } from "./util/exit.js"; | |
| 22 | 24 | import { logger } from "./util/logger.js"; |
| 23 | 25 | import { readStdinSync } from "./util/stdin.js"; |
| 24 | 26 | import { getVersion } from "./version.js"; |
| @@ -62,8 +64,7 @@ | ||
| 62 | 64 | if (tuiUnmount) { |
| 63 | 65 | tuiUnmount(); |
| 64 | 66 | } |
| 65 | - await sentryService.flush(); | |
| 66 | - process.exit(0); | |
| 67 | + await gracefulExit(0); | |
| 67 | 68 | } else { |
| 68 | 69 | // First Ctrl+C or too much time elapsed - show exit message |
| 69 | 70 | lastCtrlCTime = now; |
| @@ -131,6 +132,8 @@ | ||
| 131 | 132 | .option("--resume", "Resume from last session") |
| 132 | 133 | .option("--fork <sessionId>", "Fork from an existing session ID") |
| 133 | 134 | .action(async (prompt, options) => { |
| 135 | + // Telemetry: record command invocation | |
| 136 | + await posthogService.capture("cliCommand", { command: "cn" }); | |
| 134 | 137 | // Handle piped input - detect it early and decide on mode |
| 135 | 138 | let stdinInput = null; |
| 136 | 139 | |
| @@ -217,7 +220,7 @@ | ||
| 217 | 220 | safeStderr(' cn -p "please review my current git diff"\n'); |
| 218 | 221 | safeStderr(' echo "hello" | cn -p\n'); |
| 219 | 222 | safeStderr(' cn -p "analyze the code in src/"\n'); |
| 220 | - process.exit(1); | |
| 223 | + await gracefulExit(1); | |
| 221 | 224 | } |
| 222 | 225 | |
| 223 | 226 | // Map --print to headless mode |
| @@ -231,6 +234,8 @@ | ||
| 231 | 234 | .command("login") |
| 232 | 235 | .description("Authenticate with Continue") |
| 233 | 236 | .action(async () => { |
| 237 | + // Telemetry: record command invocation | |
| 238 | + await posthogService.capture("cliCommand", { command: "login" }); | |
| 234 | 239 | await login(); |
| 235 | 240 | }); |
| 236 | 241 | |
| @@ -239,6 +244,8 @@ | ||
| 239 | 244 | .command("logout") |
| 240 | 245 | .description("Log out from Continue") |
| 241 | 246 | .action(async () => { |
| 247 | + // Telemetry: record command invocation | |
| 248 | + await posthogService.capture("cliCommand", { command: "logout" }); | |
| 242 | 249 | await logout(); |
| 243 | 250 | }); |
| 244 | 251 | |
| @@ -248,6 +255,8 @@ | ||
| 248 | 255 | .description("List recent chat sessions and select one to resume") |
| 249 | 256 | .option("--json", "Output in JSON format") |
| 250 | 257 | .action(async (options) => { |
| 258 | + // Telemetry: record command invocation | |
| 259 | + await posthogService.capture("cliCommand", { command: "ls" }); | |
| 251 | 260 | await listSessionsCommand({ |
| 252 | 261 | format: options.json ? "json" : undefined, |
| 253 | 262 | }); |
| @@ -280,6 +289,11 @@ | ||
| 280 | 289 | "Specify the repository URL to use in the remote environment", |
| 281 | 290 | ) |
| 282 | 291 | .action(async (prompt: string | undefined, options) => { |
| 292 | + // Telemetry: record command invocation | |
| 293 | + await posthogService.capture("cliCommand", { | |
| 294 | + command: "remote", | |
| 295 | + flagS: options.start, | |
| 296 | + }); | |
| 283 | 297 | await remote(prompt, options); |
| 284 | 298 | }); |
| 285 | 299 | |
| @@ -294,6 +308,8 @@ | ||
| 294 | 308 | ) |
| 295 | 309 | .option("--port <port>", "Port to run the server on (default: 8000)", "8000") |
| 296 | 310 | .action(async (prompt, options) => { |
| 311 | + // Telemetry: record command invocation | |
| 312 | + await posthogService.capture("cliCommand", { command: "serve" }); | |
| 297 | 313 | // Merge parent options with subcommand options |
| 298 | 314 | const mergedOptions = mergeParentOptions(program, options); |
| 299 | 315 | |
| @@ -311,6 +327,8 @@ | ||
| 311 | 327 | .description("Test remote TUI mode with a local server") |
| 312 | 328 | .option("--url <url>", "Server URL (default: http://localhost:8000)") |
| 313 | 329 | .action(async (prompt: string | undefined, options) => { |
| 330 | + // Telemetry: record command invocation | |
| 331 | + await posthogService.capture("cliCommand", { command: "remote-test" }); | |
| 314 | 332 | await remoteTest(prompt, options.url); |
| 315 | 333 | }); |
| 316 | 334 | |
| @@ -318,7 +336,7 @@ | ||
| 318 | 336 | program.on("command:*", () => { |
| 319 | 337 | console.error(`Error: Unknown command '${program.args.join(" ")}'\n`); |
| 320 | 338 | program.outputHelp(); |
| 321 | - process.exit(1); | |
| 339 | + void gracefulExit(1); | |
| 322 | 340 | }); |
| 323 | 341 | |
| 324 | 342 | // Parse arguments and handle errors |
| @@ -329,10 +347,9 @@ | ||
| 329 | 347 | sentryService.captureException( |
| 330 | 348 | error instanceof Error ? error : new Error(String(error)), |
| 331 | 349 | ); |
| 332 | - process.exit(1); | |
| 350 | + void gracefulExit(1); | |
| 333 | 351 | } |
| 334 | 352 | |
| 335 | 353 | process.on("SIGTERM", async () => { |
| 336 | - await sentryService.flush(); | |
| 337 | - process.exit(0); | |
| 354 | + await gracefulExit(0); | |
| 338 | 355 | }); |
modified extensions/cli/src/stream/streamChatResponse.helpers.ts +17 −1
| @@ -6,6 +6,7 @@ | ||
| 6 | 6 | import { toolPermissionManager } from "../permissions/permissionManager.js"; |
| 7 | 7 | import { ToolCallRequest } from "../permissions/types.js"; |
| 8 | 8 | import { services } from "../services/index.js"; |
| 9 | +import { posthogService } from "../telemetry/posthogService.js"; | |
| 9 | 10 | import { telemetryService } from "../telemetry/telemetryService.js"; |
| 10 | 11 | import { calculateTokenCost } from "../telemetry/utils.js"; |
| 11 | 12 | import { |
| @@ -64,7 +65,11 @@ | ||
| 64 | 65 | `If you don't want the tool to be included, use --exclude ${toolName}.`, |
| 65 | 66 | ); |
| 66 | 67 | |
| 67 | - process.exit(1); | |
| 68 | + // Use graceful exit to flush telemetry even in headless denial | |
| 69 | + // Note: We purposely trigger an async exit without awaiting in this sync path | |
| 70 | + import("../util/exit.js").then(({ gracefulExit }) => gracefulExit(1)); | |
| 71 | + // Throw to satisfy the never return type; process will exit shortly | |
| 72 | + throw new Error("Exiting due to headless permission requirement"); | |
| 68 | 73 | } |
| 69 | 74 | |
| 70 | 75 | // Helper function to request user permission |
| @@ -286,6 +291,17 @@ | ||
| 286 | 291 | costUsd: cost, |
| 287 | 292 | }); |
| 288 | 293 | |
| 294 | + // Mirror core metrics to PostHog for product analytics | |
| 295 | + try { | |
| 296 | + posthogService.capture("apiRequest", { | |
| 297 | + model: model.model, | |
| 298 | + durationMs: totalDuration, | |
| 299 | + inputTokens, | |
| 300 | + outputTokens, | |
| 301 | + costUsd: cost, | |
| 302 | + }); | |
| 303 | + } catch {} | |
| 304 | + | |
| 289 | 305 | return cost; |
| 290 | 306 | } |
| 291 | 307 |
modified extensions/cli/src/stream/streamChatResponse.ts +10 −0
| @@ -11,6 +11,7 @@ | ||
| 11 | 11 | import { getServiceSync, SERVICE_NAMES, services } from "../services/index.js"; |
| 12 | 12 | import { systemMessageService } from "../services/SystemMessageService.js"; |
| 13 | 13 | import type { ToolPermissionServiceState } from "../services/ToolPermissionService.js"; |
| 14 | +import { posthogService } from "../telemetry/posthogService.js"; | |
| 14 | 15 | import { telemetryService } from "../telemetry/telemetryService.js"; |
| 15 | 16 | import { ToolCall } from "../tools/index.js"; |
| 16 | 17 | import { |
| @@ -258,6 +259,15 @@ | ||
| 258 | 259 | error: error.message || String(error), |
| 259 | 260 | }); |
| 260 | 261 | |
| 262 | + try { | |
| 263 | + posthogService.capture("apiRequest", { | |
| 264 | + model: model.model, | |
| 265 | + durationMs: errorDuration, | |
| 266 | + success: false, | |
| 267 | + error: error.message || String(error), | |
| 268 | + }); | |
| 269 | + } catch {} | |
| 270 | + | |
| 261 | 271 | if (error.name === "AbortError" || abortController?.signal.aborted) { |
| 262 | 272 | logger.debug("Stream aborted by user"); |
| 263 | 273 | return { |
added extensions/cli/src/telemetry/headerUtils.ts +26 −0
| @@ -0,0 +1,26 @@ | ||
| 1 | +export function parseOtelHeaders(headersStr: string): Record<string, string> { | |
| 2 | + const headers: Record<string, string> = {}; | |
| 3 | + if (!headersStr) return headers; | |
| 4 | + | |
| 5 | + headersStr.split(",").forEach((header) => { | |
| 6 | + const [key, value] = header.split("="); | |
| 7 | + if (key && value) { | |
| 8 | + headers[key.trim()] = value.trim(); | |
| 9 | + } | |
| 10 | + }); | |
| 11 | + | |
| 12 | + return headers; | |
| 13 | +} | |
| 14 | + | |
| 15 | +export function detectTerminalType(): string | undefined { | |
| 16 | + if (process.env.TERM_PROGRAM) { | |
| 17 | + return process.env.TERM_PROGRAM; | |
| 18 | + } | |
| 19 | + if (process.env.VSCODE_PID) { | |
| 20 | + return "vscode"; | |
| 21 | + } | |
| 22 | + if (process.env.TMUX) { | |
| 23 | + return "tmux"; | |
| 24 | + } | |
| 25 | + return undefined; | |
| 26 | +} |
modified extensions/cli/src/telemetry/telemetryService.ts +21 −48
| @@ -12,8 +12,6 @@ | ||
| 12 | 12 | SEMRESATTRS_DEPLOYMENT_ENVIRONMENT, |
| 13 | 13 | SEMRESATTRS_HOST_NAME, |
| 14 | 14 | SEMRESATTRS_OS_TYPE, |
| 15 | - SEMRESATTRS_PROCESS_PID, | |
| 16 | - SEMRESATTRS_SERVICE_INSTANCE_ID, | |
| 17 | 15 | SEMRESATTRS_SERVICE_NAME, |
| 18 | 16 | SEMRESATTRS_SERVICE_VERSION, |
| 19 | 17 | } from "@opentelemetry/semantic-conventions"; |
| @@ -24,12 +22,13 @@ | ||
| 24 | 22 | import { logger } from "../util/logger.js"; |
| 25 | 23 | import { getVersion } from "../version.js"; |
| 26 | 24 | |
| 25 | +import { detectTerminalType, parseOtelHeaders } from "./headerUtils.js"; | |
| 26 | + | |
| 27 | 27 | export interface TelemetryConfig { |
| 28 | 28 | enabled: boolean; |
| 29 | 29 | sessionId: string; |
| 30 | 30 | organizationId?: string; |
| 31 | 31 | accountUuid?: string; |
| 32 | - includeSessionId: boolean; | |
| 33 | 32 | includeVersion: boolean; |
| 34 | 33 | includeAccountUuid: boolean; |
| 35 | 34 | } |
| @@ -80,7 +79,6 @@ | ||
| 80 | 79 | sessionId, |
| 81 | 80 | organizationId: process.env.ORGANIZATION_ID, |
| 82 | 81 | accountUuid: process.env.ACCOUNT_UUID, |
| 83 | - includeSessionId: process.env.OTEL_METRICS_INCLUDE_SESSION_ID !== "false", | |
| 84 | 82 | includeVersion: process.env.OTEL_METRICS_INCLUDE_VERSION === "true", |
| 85 | 83 | includeAccountUuid: |
| 86 | 84 | process.env.OTEL_METRICS_INCLUDE_ACCOUNT_UUID !== "false", |
| @@ -93,12 +91,10 @@ | ||
| 93 | 91 | const resource = resourceFromAttributes({ |
| 94 | 92 | [SEMRESATTRS_SERVICE_NAME]: "continue-cli", |
| 95 | 93 | [SEMRESATTRS_SERVICE_VERSION]: getVersion(), |
| 96 | - [SEMRESATTRS_SERVICE_INSTANCE_ID]: uuidv4(), | |
| 97 | 94 | [SEMRESATTRS_HOST_NAME]: os.hostname(), |
| 98 | 95 | [SEMRESATTRS_DEPLOYMENT_ENVIRONMENT]: |
| 99 | 96 | process.env.NODE_ENV || "development", |
| 100 | 97 | [SEMRESATTRS_OS_TYPE]: os.type(), |
| 101 | - [SEMRESATTRS_PROCESS_PID]: process.pid.toString(), | |
| 102 | 98 | }); |
| 103 | 99 | |
| 104 | 100 | // Configure exporters |
| @@ -113,11 +109,20 @@ | ||
| 113 | 109 | readers.push( |
| 114 | 110 | new PeriodicExportingMetricReader({ |
| 115 | 111 | exporter: new OTLPMetricExporter({ |
| 116 | - url: | |
| 117 | - process.env.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT || | |
| 118 | - `${process.env.OTEL_EXPORTER_OTLP_ENDPOINT}/v1/metrics` || | |
| 119 | - "http://localhost:4318/v1/metrics", | |
| 120 | - headers: this.parseHeaders( | |
| 112 | + url: (() => { | |
| 113 | + const metricsEndpoint = | |
| 114 | + process.env.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT; | |
| 115 | + if (metricsEndpoint && metricsEndpoint.trim().length > 0) { | |
| 116 | + return metricsEndpoint; | |
| 117 | + } | |
| 118 | + const base = process.env.OTEL_EXPORTER_OTLP_ENDPOINT; | |
| 119 | + if (base && base.trim().length > 0) { | |
| 120 | + const normalized = base.replace(/\/$/, ""); | |
| 121 | + return `${normalized}/v1/metrics`; | |
| 122 | + } | |
| 123 | + return "http://localhost:4318/v1/metrics"; | |
| 124 | + })(), | |
| 125 | + headers: parseOtelHeaders( | |
| 121 | 126 | process.env.OTEL_EXPORTER_OTLP_HEADERS || "", |
| 122 | 127 | ), |
| 123 | 128 | }), |
| @@ -183,20 +188,6 @@ | ||
| 183 | 188 | this.shutdownHandlersRegistered = true; |
| 184 | 189 | } |
| 185 | 190 | |
| 186 | - private parseHeaders(headersStr: string): Record<string, string> { | |
| 187 | - const headers: Record<string, string> = {}; | |
| 188 | - if (!headersStr) return headers; | |
| 189 | - | |
| 190 | - headersStr.split(",").forEach((header) => { | |
| 191 | - const [key, value] = header.split("="); | |
| 192 | - if (key && value) { | |
| 193 | - headers[key.trim()] = value.trim(); | |
| 194 | - } | |
| 195 | - }); | |
| 196 | - | |
| 197 | - return headers; | |
| 198 | - } | |
| 199 | - | |
| 200 | 191 | private initializeMetrics() { |
| 201 | 192 | if (!this.meter) return; |
| 202 | 193 | |
| @@ -308,10 +299,6 @@ | ||
| 308 | 299 | ) { |
| 309 | 300 | const attributes: Record<string, string> = { ...additionalAttributes }; |
| 310 | 301 | |
| 311 | - if (this.config.includeSessionId) { | |
| 312 | - attributes["session.id"] = this.config.sessionId; | |
| 313 | - } | |
| 314 | - | |
| 315 | 302 | if (this.config.includeVersion) { |
| 316 | 303 | attributes["app.version"] = getVersion(); |
| 317 | 304 | } |
| @@ -325,7 +312,7 @@ | ||
| 325 | 312 | } |
| 326 | 313 | |
| 327 | 314 | // Detect terminal type |
| 328 | - const terminalType = this.detectTerminalType(); | |
| 315 | + const terminalType = detectTerminalType(); | |
| 329 | 316 | if (terminalType) { |
| 330 | 317 | attributes["terminal.type"] = terminalType; |
| 331 | 318 | } |
| @@ -333,19 +320,6 @@ | ||
| 333 | 320 | return attributes; |
| 334 | 321 | } |
| 335 | 322 | |
| 336 | - private detectTerminalType(): string | undefined { | |
| 337 | - if (process.env.TERM_PROGRAM) { | |
| 338 | - return process.env.TERM_PROGRAM; | |
| 339 | - } | |
| 340 | - if (process.env.VSCODE_PID) { | |
| 341 | - return "vscode"; | |
| 342 | - } | |
| 343 | - if (process.env.TMUX) { | |
| 344 | - return "tmux"; | |
| 345 | - } | |
| 346 | - return undefined; | |
| 347 | - } | |
| 348 | - | |
| 349 | 323 | // Public methods for tracking metrics |
| 350 | 324 | |
| 351 | 325 | public recordSessionStart() { |
| @@ -435,13 +409,12 @@ | ||
| 435 | 409 | if (!this.isEnabled()) return; |
| 436 | 410 | |
| 437 | 411 | if (this.activeStartTime !== null) { |
| 438 | - this.totalActiveTime += Date.now() - this.activeStartTime; | |
| 412 | + const deltaMs = Date.now() - this.activeStartTime; | |
| 413 | + this.totalActiveTime += deltaMs; | |
| 439 | 414 | this.activeStartTime = null; |
| 440 | 415 | |
| 441 | - this.activeTimeCounter.add( | |
| 442 | - this.totalActiveTime / 1000, | |
| 443 | - this.getStandardAttributes(), | |
| 444 | - ); | |
| 416 | + // Record only the delta for this active window (in seconds) | |
| 417 | + this.activeTimeCounter.add(deltaMs / 1000, this.getStandardAttributes()); | |
| 445 | 418 | } |
| 446 | 419 | } |
| 447 | 420 |
modified extensions/cli/src/tools/exit.ts +3 −1
| @@ -9,6 +9,8 @@ | ||
| 9 | 9 | readonly: false, |
| 10 | 10 | isBuiltIn: true, |
| 11 | 11 | run: async (): Promise<string> => { |
| 12 | - process.exit(1); | |
| 12 | + const { gracefulExit } = await import("../util/exit.js"); | |
| 13 | + await gracefulExit(1); | |
| 14 | + return ""; | |
| 13 | 15 | }, |
| 14 | 16 | }; |
modified extensions/cli/src/ui/hooks/useChat.helpers.ts +1 −1
| @@ -67,7 +67,7 @@ | ||
| 67 | 67 | onClear, |
| 68 | 68 | }: ProcessSlashCommandResultOptions): string | null { |
| 69 | 69 | if (result.exit) { |
| 70 | - process.exit(0); | |
| 70 | + import("../../util/exit.js").then(({ gracefulExit }) => gracefulExit(0)); | |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | if (result.openMcpSelector) { |
modified extensions/cli/src/ui/hooks/useUserInput.ts +1 −1
| @@ -76,7 +76,7 @@ | ||
| 76 | 76 | // Handle Ctrl+D to exit |
| 77 | 77 | if (key.ctrl && input === "d") { |
| 78 | 78 | exit(); |
| 79 | - process.exit(0); | |
| 79 | + import("../../util/exit.js").then(({ gracefulExit }) => gracefulExit(0)); | |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | // Handle Ctrl+L to refresh screen (clear terminal artifacts) |
added extensions/cli/src/util/exit.ts +27 −0
| @@ -0,0 +1,27 @@ | ||
| 1 | +import { sentryService } from "../sentry.js"; | |
| 2 | +import { telemetryService } from "../telemetry/telemetryService.js"; | |
| 3 | + | |
| 4 | +import { logger } from "./logger.js"; | |
| 5 | + | |
| 6 | +/** | |
| 7 | + * Exit the process after flushing telemetry and error reporting. | |
| 8 | + * Use this instead of process.exit() to avoid losing metrics/logs. | |
| 9 | + */ | |
| 10 | +export async function gracefulExit(code: number = 0): Promise<void> { | |
| 11 | + try { | |
| 12 | + // Flush metrics (forceFlush + shutdown inside service) | |
| 13 | + await telemetryService.shutdown(); | |
| 14 | + } catch (err) { | |
| 15 | + logger.debug("Telemetry shutdown error (ignored)", err as any); | |
| 16 | + } | |
| 17 | + | |
| 18 | + try { | |
| 19 | + // Flush Sentry (best effort) | |
| 20 | + await sentryService.flush(); | |
| 21 | + } catch (err) { | |
| 22 | + logger.debug("Sentry flush error (ignored)", err as any); | |
| 23 | + } | |
| 24 | + | |
| 25 | + // Exit the process | |
| 26 | + process.exit(code); | |
| 27 | +} |