Elffuss BraidHub

kikocis/continue public

Merge pull request #7528 from uinstinct/cli-telemetry-no-connection

feat(cli): skip telemetry if there is no internet connection
Changeset41d2e879b49adbe122b367fe45387ae236ed0fd97cd6c6512b5bd8fbb8b8d933
AuthorTing-Wai <40179554+tingwai@users.noreply.github.com> · 2025-09-08 16:24 -07:00
CommitterGitHub <noreply@github.com> · 2025-09-08 16:24 -07:00
Parents384664fb42b4, b7bed744edb6

Browse the files at this changeset

1 file changed

modified extensions/cli/src/telemetry/posthogService.ts +33 −2
@@ -1,3 +1,4 @@
1+import dns from "dns/promises";
12 import os from "node:os";
23
34 import node_machine_id from "node-machine-id";
@@ -15,13 +16,36 @@
1516 this.uniqueId = this.getEventUserId();
1617 }
1718
19+ private _hasInternetConnection: boolean | undefined = undefined;
20+ private async hasInternetConnection() {
21+ const refetchConnection = async () => {
22+ try {
23+ await dns.lookup("app.posthog.com");
24+ this._hasInternetConnection = true;
25+ } catch {
26+ this._hasInternetConnection = false;
27+ }
28+ };
29+
30+ if (typeof this._hasInternetConnection !== "undefined") {
31+ void refetchConnection(); // check in background if connection became available
32+ return this._hasInternetConnection;
33+ }
34+
35+ await refetchConnection();
36+ return this._hasInternetConnection;
37+ }
38+
1839 get isEnabled() {
1940 return process.env.CONTINUE_CLI_ENABLE_TELEMETRY !== "0";
2041 }
2142
2243 private _client: PostHogType | undefined;
2344 private async getClient() {
24- if (this.isEnabled) {
45+ if (!(await this.hasInternetConnection())) {
46+ this._client = undefined;
47+ logger.warn("No internet connection, skipping telemetry");
48+ } else if (this.isEnabled) {
2549 if (!this._client) {
2650 const { PostHog } = await import("posthog-node");
2751 this._client = new PostHog(
@@ -30,6 +54,7 @@
3054 host: "https://app.posthog.com",
3155 },
3256 );
57+ logger.debug("Initialized telemetry");
3358 }
3459 } else {
3560 this._client = undefined;
@@ -83,7 +108,13 @@
83108 try {
84109 const client = await this.getClient();
85110 if (client) {
86- await client.shutdown();
111+ // Set a timeout for shutdown to prevent hanging
112+ const shutdownPromise = client.shutdown();
113+ const timeoutPromise = new Promise((_, reject) =>
114+ setTimeout(() => reject(new Error("Shutdown timeout")), 5000),
115+ );
116+
117+ await Promise.race([shutdownPromise, timeoutPromise]);
87118 }
88119 } catch (e) {
89120 logger.debug(`Failed to shutdown PostHog client: ${e}`);