diff --git a/src/common/sentry.ts b/src/common/sentry.ts index 36435b86c9..da0e749994 100644 --- a/src/common/sentry.ts +++ b/src/common/sentry.ts @@ -20,73 +20,58 @@ */ import { CaptureConsole, Dedupe, Offline } from "@sentry/integrations"; +import * as Sentry from "@sentry/electron"; import { sentryDsn, isProduction } from "./vars"; import { UserStore } from "./user-store"; import logger from "../main/logger"; -export let sentryIsInitialized = false; - /** - * This function bypasses webpack issues. - * - * See: https://docs.sentry.io/platforms/javascript/guides/electron/#webpack-configuration + * "Translate" 'browser' to 'main' as Lens developer more familiar with the term 'main' */ -async function requireSentry() { - switch (process.type) { - case "browser": - return import("@sentry/electron/dist/main"); - case "renderer": - return import("@sentry/electron/dist/renderer"); - default: - throw new Error(`Unsupported process type ${process.type}`); +function mapProcessName(processType: string) { + if (processType === "browser") { + return "main"; } + + return processType; } /** * Initialize Sentry for the current process so to send errors for debugging. */ -export async function SentryInit(): Promise { - try { - const Sentry = await requireSentry(); +export function SentryInit() { + const processName = mapProcessName(process.type); - try { - Sentry.init({ - beforeSend: event => { - if (UserStore.getInstance().allowErrorReporting) { - return event; - } + Sentry.init({ + beforeSend: (event) => { + // default to false, in case instance of UserStore is not created (yet) + const allowErrorReporting = UserStore.getInstance(false)?.allowErrorReporting ?? false; - logger.info(`🔒 [SENTRY-BEFORE-SEND-HOOK]: allowErrorReporting: false. Sentry event is caught but not sent to server.`); - logger.info("🔒 [SENTRY-BEFORE-SEND-HOOK]: === START OF SENTRY EVENT ==="); - logger.info(event); - logger.info("🔒 [SENTRY-BEFORE-SEND-HOOK]: === END OF SENTRY EVENT ==="); + if (allowErrorReporting) { + return event; + } - // if return null, the event won't be sent - // ref https://github.com/getsentry/sentry-javascript/issues/2039 - return null; - }, - dsn: sentryDsn, - integrations: [ - new CaptureConsole({ levels: ["error"] }), - new Dedupe(), - new Offline() - ], - initialScope: { - tags: { - // "translate" browser to 'main' as Lens developer more familiar with the term 'main' - "process": process.type === "browser" ? "main" : "renderer" - } - }, - environment: isProduction ? "production" : "development", - }); + logger.info(`🔒 [SENTRY-BEFORE-SEND-HOOK]: allowErrorReporting: ${allowErrorReporting}. Sentry event is caught but not sent to server.`); + logger.info("🔒 [SENTRY-BEFORE-SEND-HOOK]: === START OF SENTRY EVENT ==="); + logger.info(event); + logger.info("🔒 [SENTRY-BEFORE-SEND-HOOK]: === END OF SENTRY EVENT ==="); - sentryIsInitialized = true; + // if return null, the event won't be sent + // ref https://github.com/getsentry/sentry-javascript/issues/2039 + return null; + }, + dsn: sentryDsn, + integrations: [ + new CaptureConsole({ levels: ["error"] }), + new Dedupe(), + new Offline() + ], + initialScope: { + tags: { - logger.info(`✔️ [SENTRY-INIT]: Sentry for ${process.type} is initialized.`); - } catch (error) { - logger.warn(`⚠️ [SENTRY-INIT]: Sentry.init() error: ${error?.message ?? error}.`); - } - } catch (error) { - logger.warn(`⚠️ [SENTRY-INIT]: Error loading Sentry module ${error?.message ?? error}.`); - } + "process": processName + } + }, + environment: isProduction ? "production" : "development", + }); } diff --git a/src/common/vars.ts b/src/common/vars.ts index 74dcb0abf3..0dea82cab6 100644 --- a/src/common/vars.ts +++ b/src/common/vars.ts @@ -71,4 +71,4 @@ export const supportUrl = "https://docs.k8slens.dev/latest/support/" as string; export const appSemVer = new SemVer(packageInfo.version); export const docsUrl = "https://docs.k8slens.dev/main/" as string; -export const sentryDsn = packageInfo.config?.sentryDsn; +export const sentryDsn = packageInfo.config?.sentryDsn ?? ""; diff --git a/src/main/index.ts b/src/main/index.ts index 20e1c8d19c..91a9b7c68a 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -61,6 +61,10 @@ import { ExtensionsStore } from "../extensions/extensions-store"; import { FilesystemProvisionerStore } from "./extension-filesystem"; import { SentryInit } from "../common/sentry"; +// This has to be called before start using winton-based logger +// For example, before any logger.log +SentryInit(); + const workingDir = path.join(app.getPath("appData"), appName); const cleanup = disposer(); @@ -141,12 +145,6 @@ app.on("ready", async () => { UserStore.createInstance().startMainReactions(); - /** - * There is no point setting up sentry before UserStore is initialized as - * `allowErrorReporting` will always be falsy. - */ - await SentryInit(); - ClusterStore.createInstance().provideInitialFromMain(); HotbarStore.createInstance(); ExtensionsStore.createInstance(); diff --git a/src/renderer/bootstrap.tsx b/src/renderer/bootstrap.tsx index 21dbca9c1a..cde700e2b5 100644 --- a/src/renderer/bootstrap.tsx +++ b/src/renderer/bootstrap.tsx @@ -87,11 +87,7 @@ export async function bootstrap(App: AppComponent) { UserStore.createInstance(); - /** - * There is no point setting up sentry before UserStore is initialized as - * `allowErrorReporting` will always be falsy. - */ - await SentryInit(); + SentryInit(); await ClusterStore.createInstance().loadInitialOnRenderer(); HotbarStore.createInstance(); diff --git a/src/renderer/components/+preferences/preferences.tsx b/src/renderer/components/+preferences/preferences.tsx index 285c9b2c75..6e9ae5f210 100644 --- a/src/renderer/components/+preferences/preferences.tsx +++ b/src/renderer/components/+preferences/preferences.tsx @@ -41,7 +41,7 @@ import { FormSwitch, Switcher } from "../switch"; import { KubeconfigSyncs } from "./kubeconfig-syncs"; import { SettingLayout } from "../layout/setting-layout"; import { Checkbox } from "../checkbox"; -import { sentryIsInitialized } from "../../../common/sentry"; +import { sentryDsn } from "../../../common/vars"; enum Pages { Application = "application", @@ -259,7 +259,7 @@ export class Preferences extends React.Component {

Telemetry

{telemetryExtensions.map(this.renderExtension)} - {sentryIsInitialized ? ( + {sentryDsn ? (