| 1 | import dns from "dns/promises"; |
| 2 | import os from "node:os"; |
| 3 | |
| 4 | import node_machine_id from "node-machine-id"; |
| 5 | import type { PostHog as PostHogType } from "posthog-node"; |
| 6 | |
| 7 | import { isAuthenticatedConfig, loadAuthConfig } from "../auth/workos.js"; |
| 8 | import { isGitHubActions } from "../util/git.js"; |
| 9 | import { logger } from "../util/logger.js"; |
| 10 | import { getVersion } from "../version.js"; |
| 11 | |
| 12 | export class PosthogService { |
| 13 | private os: string | undefined; |
| 14 | private uniqueId: string; |
| 15 | |
| 16 | constructor() { |
| 17 | this.os = os.platform(); |
| 18 | this.uniqueId = this.getEventUserId(); |
| 19 | } |
| 20 | |
| 21 | private _hasInternetConnection: boolean | undefined = undefined; |
| 22 | private async hasInternetConnection() { |
| 23 | const refetchConnection = async () => { |
| 24 | try { |
| 25 | await dns.lookup("app.posthog.com"); |
| 26 | this._hasInternetConnection = true; |
| 27 | } catch { |
| 28 | this._hasInternetConnection = false; |
| 29 | } |
| 30 | }; |
| 31 | |
| 32 | if (typeof this._hasInternetConnection !== "undefined") { |
| 33 | void refetchConnection(); // check in background if connection became available |
| 34 | return this._hasInternetConnection; |
| 35 | } |
| 36 | |
| 37 | await refetchConnection(); |
| 38 | return this._hasInternetConnection; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Check if running in headless mode (-p/--print flags) |
| 43 | */ |
| 44 | private isHeadlessMode(): boolean { |
| 45 | const args = process.argv.slice(2); |
| 46 | return args.includes("-p") || args.includes("--print"); |
| 47 | } |
| 48 | |
| 49 | get isEnabled() { |
| 50 | return process.env.CONTINUE_CLI_ENABLE_TELEMETRY !== "0"; |
| 51 | } |
| 52 | |
| 53 | private _client: PostHogType | undefined; |
| 54 | private async getClient() { |
| 55 | if (!(await this.hasInternetConnection())) { |
| 56 | this._client = undefined; |
| 57 | logger.warn("No internet connection, skipping telemetry"); |
| 58 | } else if (this.isEnabled) { |
| 59 | if (!this._client) { |
| 60 | const { PostHog } = await import("posthog-node"); |
| 61 | this._client = new PostHog( |
| 62 | "phc_JS6XFROuNbhJtVCEdTSYk6gl5ArRrTNMpCcguAXlSPs", |
| 63 | { |
| 64 | host: "https://app.posthog.com", |
| 65 | }, |
| 66 | ); |
| 67 | logger.debug("Initialized telemetry"); |
| 68 | } |
| 69 | } else { |
| 70 | this._client = undefined; |
| 71 | } |
| 72 | return this._client; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * - Continue user id if signed in |
| 77 | * - Unique machine id if not signed in |
| 78 | */ |
| 79 | private getEventUserId(): string { |
| 80 | const authConfig = loadAuthConfig(); |
| 81 | |
| 82 | if (isAuthenticatedConfig(authConfig)) { |
| 83 | return authConfig.userId; |
| 84 | } |
| 85 | |
| 86 | // Fall back to unique machine id if not signed in |
| 87 | return node_machine_id.machineIdSync(); |
| 88 | } |
| 89 | |
| 90 | async capture(event: string, properties: { [key: string]: any }) { |
| 91 | try { |
| 92 | const client = await this.getClient(); |
| 93 | if (!client) { |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | const augmentedProperties = { |
| 98 | ...properties, |
| 99 | os: this.os, |
| 100 | extensionVersion: getVersion(), |
| 101 | ideName: "cn", |
| 102 | ideType: "cli", |
| 103 | isHeadless: this.isHeadlessMode(), |
| 104 | isGitHubCI: isGitHubActions(), |
| 105 | }; |
| 106 | const payload = { |
| 107 | distinctId: this.uniqueId, |
| 108 | event, |
| 109 | properties: augmentedProperties, |
| 110 | sendFeatureFlags: true, |
| 111 | }; |
| 112 | |
| 113 | client?.capture(payload); |
| 114 | } catch (e) { |
| 115 | logger.debug(`Failed to capture telemetry event '${event}': ${e}`); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | async shutdown() { |
| 120 | try { |
| 121 | const client = await this.getClient(); |
| 122 | if (client) { |
| 123 | // Set a timeout for shutdown to prevent hanging |
| 124 | const shutdownPromise = client.shutdown(); |
| 125 | const timeoutPromise = new Promise((_, reject) => |
| 126 | setTimeout(() => reject(new Error("Shutdown timeout")), 5000), |
| 127 | ); |
| 128 | |
| 129 | await Promise.race([shutdownPromise, timeoutPromise]); |
| 130 | } |
| 131 | } catch (e) { |
| 132 | logger.debug(`Failed to shutdown PostHog client: ${e}`); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | export const posthogService = new PosthogService(); |