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