Elffuss BraidHub

kikocis/continue public

b334cb9dcd5e · 4.2 KiBRawPermalink
1import * as Sentry from "@sentry/react";
2import { HubSessionInfo } from "core/control-plane/AuthTypes";
3import {
4 anonymizeSentryEvent,
5 anonymizeUserInfo,
6} from "core/util/sentry/anonymization";
7import { SENTRY_DSN } from "core/util/sentry/constants";
8import posthog from "posthog-js";
9import { PostHogProvider } from "posthog-js/react";
10import { PropsWithChildren, useEffect } from "react";
11import { useAuth } from "../context/Auth";
12import { useAppSelector } from "../redux/hooks";
13import { isPrerelease } from "../util/index";
14import { isContinueTeamMember } from "../util/isContinueTeamMember";
15import { getLocalStorage } from "../util/localStorage";
16
17const SAMPLE_REATE = 0.1;
18const TRACES_SAMPLE_RATE = 0.25;
19
20const TelemetryProviders = ({ children }: PropsWithChildren) => {
21 const allowAnonymousTelemetry = useAppSelector(
22 (store) => store?.config?.config?.allowAnonymousTelemetry,
23 );
24 const { session } = useAuth();
25
26 // TODO: Remove Continue team member check once Sentry is ready for all users
27 const hasContinueEmail = isContinueTeamMember(
28 (session as HubSessionInfo)?.account?.id,
29 );
30
31 useEffect(() => {
32 if (allowAnonymousTelemetry && hasContinueEmail) {
33 // Initialize PostHog (existing logic)
34 posthog.init("phc_JS6XFROuNbhJtVCEdTSYk6gl5ArRrTNMpCcguAXlSPs", {
35 api_host: "https://app.posthog.com",
36 disable_session_recording: true,
37 autocapture: false,
38 // We need to manually track pageviews since we're a SPA
39 capture_pageleave: false,
40 capture_pageview: false,
41 });
42 posthog.identify(window.vscMachineId);
43 posthog.opt_in_capturing();
44
45 // Initialize Sentry (new for GUI - enhanced React setup)
46 Sentry.init({
47 dsn: SENTRY_DSN,
48 environment: process.env.NODE_ENV,
49 integrations: [
50 // Performance monitoring
51 Sentry.browserTracingIntegration(),
52 ],
53
54 // For basic error tracking, a lower sample rate should be fine
55 sampleRate: SAMPLE_REATE,
56 tracesSampleRate: TRACES_SAMPLE_RATE,
57
58 // Privacy-conscious default
59 sendDefaultPii: false,
60
61 // Strip sensitive data and add basic properties
62 beforeSend(event) {
63 // Apply comprehensive anonymization using shared logic
64 const anonymizedEvent = anonymizeSentryEvent(event);
65 if (!anonymizedEvent) return null;
66
67 // Add basic properties available in GUI environment
68 if (!anonymizedEvent.tags) anonymizedEvent.tags = {};
69
70 // Add environment information
71 anonymizedEvent.tags.environment = process.env.NODE_ENV;
72
73 // Add ideInfo properties spread out as top-level properties (like PostHog)
74 const extensionVersion = getLocalStorage("extensionVersion");
75 const ideType = getLocalStorage("ide");
76
77 if (extensionVersion) {
78 anonymizedEvent.tags.extensionVersion = extensionVersion;
79 }
80
81 if (ideType) {
82 anonymizedEvent.tags.ideType = ideType;
83 }
84
85 // Add isPrerelease information
86 anonymizedEvent.tags.isPrerelease = isPrerelease();
87
88 return anonymizedEvent;
89 },
90 });
91
92 // Set anonymized user context for Sentry
93 const anonymizedUser = anonymizeUserInfo({
94 id: window.vscMachineId || "anonymous",
95 });
96 Sentry.setUser(anonymizedUser);
97 } else {
98 // Disable PostHog
99 posthog.opt_out_capturing();
100
101 // Disable Sentry properly - close the client to stop all network requests
102 try {
103 const client = Sentry.getClient();
104 if (client) {
105 client.close();
106 }
107 Sentry.getCurrentScope().clear();
108 } catch (error) {
109 console.error("Error disabling Sentry:", error);
110 }
111 }
112 }, [allowAnonymousTelemetry, hasContinueEmail]);
113
114 // Conditionally wrap with ErrorBoundary when telemetry is enabled and user is Continue team member
115 const content =
116 allowAnonymousTelemetry && hasContinueEmail ? (
117 <Sentry.ErrorBoundary showDialog={false}>{children}</Sentry.ErrorBoundary>
118 ) : (
119 children
120 );
121
122 // Only PostHog needs a provider wrapper - Sentry works globally once initialized
123 return <PostHogProvider client={posthog}>{content}</PostHogProvider>;
124};
125
126export default TelemetryProviders;