Elffuss BraidHub

kikocis/continue public

main · 4.3 KiBRawPermalink
1import os from "node:os";
2
3import { TeamAnalytics } from "../control-plane/TeamAnalytics.js";
4import { IdeInfo } from "../index.js";
5
6import type { PostHog as PostHogType } from "posthog-node";
7import { extractMinimalStackTraceInfo } from "./extractMinimalStackTraceInfo.js";
8import { TokensBatchingService } from "./TokensBatchingService.js";
9
10export enum PosthogFeatureFlag {
11 AutocompleteTimeout = "autocomplete-timeout",
12 RecentlyVisitedRangesNumSurroundingLines = "recently-visited-ranges-num-surrounding-lines",
13}
14
15export const EXPERIMENTS: {
16 [key in PosthogFeatureFlag]: {
17 [key: string]: { value: any };
18 };
19} = {
20 [PosthogFeatureFlag.AutocompleteTimeout]: {
21 control: { value: 150 },
22 "250": { value: 250 },
23 "350": { value: 350 },
24 "450": { value: 450 },
25 },
26 [PosthogFeatureFlag.RecentlyVisitedRangesNumSurroundingLines]: {
27 control: { value: null },
28 "5": { value: 5 },
29 "10": { value: 10 },
30 "15": { value: 15 },
31 "20": { value: 20 },
32 },
33};
34
35export class Telemetry {
36 // Set to undefined whenever telemetry is disabled
37 static client: PostHogType | undefined = undefined;
38 static uniqueId = "NOT_UNIQUE";
39 static os: string | undefined = undefined;
40 static ideInfo: IdeInfo | undefined = undefined;
41
42 /**
43 * Convenience method for capturing errors in a single event
44 */
45 static async captureError(errorName: string, error: unknown) {
46 if (!(error instanceof Error)) {
47 return;
48 }
49 await Telemetry.capture(
50 "extension_error_caught",
51 {
52 errorName,
53 message: error.message,
54 stack: extractMinimalStackTraceInfo(error.stack),
55 },
56 false,
57 );
58 }
59
60 static async capture(
61 event: string,
62 properties: { [key: string]: any },
63 sendToTeam: boolean = false,
64 isExtensionActivationError: boolean = false,
65 ) {
66 try {
67 const augmentedProperties = {
68 ...properties,
69 os: Telemetry.os,
70 extensionVersion: Telemetry.ideInfo?.extensionVersion,
71 ideName: Telemetry.ideInfo?.name,
72 ideType: Telemetry.ideInfo?.ideType,
73 };
74 const payload = {
75 distinctId: Telemetry.uniqueId,
76 event,
77 properties: augmentedProperties,
78 sendFeatureFlags: true,
79 };
80
81 // In cases where an extremely early fatal error occurs, we may not have initialized yet
82 if (isExtensionActivationError && !Telemetry.client) {
83 const client = await Telemetry.getTelemetryClient();
84 client?.capture(payload);
85 return;
86 }
87
88 if (process.env.NODE_ENV === "test") {
89 return;
90 }
91
92 Telemetry.client?.capture(payload);
93
94 if (sendToTeam) {
95 void TeamAnalytics.capture(event, properties);
96 }
97 } catch (e) {
98 console.error(`Failed to capture event: ${e}`);
99 }
100 }
101
102 static shutdownPosthogClient() {
103 TokensBatchingService.getInstance().shutdown();
104 Telemetry.client?.shutdown();
105 }
106
107 static async getTelemetryClient(): Promise<PostHogType | undefined> {
108 try {
109 const { PostHog } = await import("posthog-node");
110 return new PostHog("phc_JS6XFROuNbhJtVCEdTSYk6gl5ArRrTNMpCcguAXlSPs", {
111 host: "https://app.posthog.com",
112 });
113 } catch (e) {
114 console.error(`Failed to setup telemetry: ${e}`);
115 }
116 }
117
118 static async setup(allow: boolean, uniqueId: string, ideInfo: IdeInfo) {
119 Telemetry.uniqueId = uniqueId;
120 Telemetry.os = os.platform();
121 Telemetry.ideInfo = ideInfo;
122
123 if (!allow || process.env.NODE_ENV === "test") {
124 Telemetry.client = undefined;
125 } else if (!Telemetry.client) {
126 Telemetry.client = await Telemetry.getTelemetryClient();
127 }
128 }
129
130 private static featureValueCache: Record<string, any> = {};
131
132 static async getFeatureFlag(flag: PosthogFeatureFlag) {
133 const value = Telemetry.client?.getFeatureFlag(flag, Telemetry.uniqueId);
134
135 Telemetry.featureValueCache[flag] = value;
136 return value;
137 }
138
139 static async getValueForFeatureFlag(flag: PosthogFeatureFlag) {
140 try {
141 if (Telemetry.featureValueCache[flag]) {
142 return Telemetry.featureValueCache[flag];
143 }
144
145 const userGroup = await Telemetry.getFeatureFlag(flag);
146 if (typeof userGroup === "string") {
147 return EXPERIMENTS[flag][userGroup].value;
148 }
149
150 return undefined;
151 } catch {
152 return undefined;
153 }
154 }
155}