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