Elffuss BraidHub

kikocis/continue public

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