1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Fix Sentry integration bugs (#3325)

This commit is contained in:
chh 2021-07-12 16:42:51 +03:00 committed by GitHub
parent ff704b5d3d
commit f86360d641
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 66 deletions

View File

@ -20,43 +20,38 @@
*/ */
import { CaptureConsole, Dedupe, Offline } from "@sentry/integrations"; import { CaptureConsole, Dedupe, Offline } from "@sentry/integrations";
import * as Sentry from "@sentry/electron";
import { sentryDsn, isProduction } from "./vars"; import { sentryDsn, isProduction } from "./vars";
import { UserStore } from "./user-store"; import { UserStore } from "./user-store";
import logger from "../main/logger"; import logger from "../main/logger";
export let sentryIsInitialized = false;
/** /**
* This function bypasses webpack issues. * "Translate" 'browser' to 'main' as Lens developer more familiar with the term 'main'
*
* See: https://docs.sentry.io/platforms/javascript/guides/electron/#webpack-configuration
*/ */
async function requireSentry() { function mapProcessName(processType: string) {
switch (process.type) { if (processType === "browser") {
case "browser": return "main";
return import("@sentry/electron/dist/main");
case "renderer":
return import("@sentry/electron/dist/renderer");
default:
throw new Error(`Unsupported process type ${process.type}`);
} }
return processType;
} }
/** /**
* Initialize Sentry for the current process so to send errors for debugging. * Initialize Sentry for the current process so to send errors for debugging.
*/ */
export async function SentryInit(): Promise<void> { export function SentryInit() {
try { const processName = mapProcessName(process.type);
const Sentry = await requireSentry();
try {
Sentry.init({ Sentry.init({
beforeSend: event => { beforeSend: (event) => {
if (UserStore.getInstance().allowErrorReporting) { // default to false, in case instance of UserStore is not created (yet)
const allowErrorReporting = UserStore.getInstance(false)?.allowErrorReporting ?? false;
if (allowErrorReporting) {
return event; return event;
} }
logger.info(`🔒 [SENTRY-BEFORE-SEND-HOOK]: allowErrorReporting: false. Sentry event is caught but not sent to server.`); 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("🔒 [SENTRY-BEFORE-SEND-HOOK]: === START OF SENTRY EVENT ===");
logger.info(event); logger.info(event);
logger.info("🔒 [SENTRY-BEFORE-SEND-HOOK]: === END OF SENTRY EVENT ==="); logger.info("🔒 [SENTRY-BEFORE-SEND-HOOK]: === END OF SENTRY EVENT ===");
@ -73,20 +68,10 @@ export async function SentryInit(): Promise<void> {
], ],
initialScope: { initialScope: {
tags: { tags: {
// "translate" browser to 'main' as Lens developer more familiar with the term 'main'
"process": process.type === "browser" ? "main" : "renderer" "process": processName
} }
}, },
environment: isProduction ? "production" : "development", environment: isProduction ? "production" : "development",
}); });
sentryIsInitialized = true;
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}.`);
}
} }

View File

@ -71,4 +71,4 @@ export const supportUrl = "https://docs.k8slens.dev/latest/support/" as string;
export const appSemVer = new SemVer(packageInfo.version); export const appSemVer = new SemVer(packageInfo.version);
export const docsUrl = "https://docs.k8slens.dev/main/" as string; export const docsUrl = "https://docs.k8slens.dev/main/" as string;
export const sentryDsn = packageInfo.config?.sentryDsn; export const sentryDsn = packageInfo.config?.sentryDsn ?? "";

View File

@ -61,6 +61,10 @@ import { ExtensionsStore } from "../extensions/extensions-store";
import { FilesystemProvisionerStore } from "./extension-filesystem"; import { FilesystemProvisionerStore } from "./extension-filesystem";
import { SentryInit } from "../common/sentry"; 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 workingDir = path.join(app.getPath("appData"), appName);
const cleanup = disposer(); const cleanup = disposer();
@ -141,12 +145,6 @@ app.on("ready", async () => {
UserStore.createInstance().startMainReactions(); 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(); ClusterStore.createInstance().provideInitialFromMain();
HotbarStore.createInstance(); HotbarStore.createInstance();
ExtensionsStore.createInstance(); ExtensionsStore.createInstance();

View File

@ -87,11 +87,7 @@ export async function bootstrap(App: AppComponent) {
UserStore.createInstance(); UserStore.createInstance();
/** SentryInit();
* There is no point setting up sentry before UserStore is initialized as
* `allowErrorReporting` will always be falsy.
*/
await SentryInit();
await ClusterStore.createInstance().loadInitialOnRenderer(); await ClusterStore.createInstance().loadInitialOnRenderer();
HotbarStore.createInstance(); HotbarStore.createInstance();

View File

@ -41,7 +41,7 @@ import { FormSwitch, Switcher } from "../switch";
import { KubeconfigSyncs } from "./kubeconfig-syncs"; import { KubeconfigSyncs } from "./kubeconfig-syncs";
import { SettingLayout } from "../layout/setting-layout"; import { SettingLayout } from "../layout/setting-layout";
import { Checkbox } from "../checkbox"; import { Checkbox } from "../checkbox";
import { sentryIsInitialized } from "../../../common/sentry"; import { sentryDsn } from "../../../common/vars";
enum Pages { enum Pages {
Application = "application", Application = "application",
@ -259,7 +259,7 @@ export class Preferences extends React.Component {
<section id="telemetry"> <section id="telemetry">
<h2 data-testid="telemetry-header">Telemetry</h2> <h2 data-testid="telemetry-header">Telemetry</h2>
{telemetryExtensions.map(this.renderExtension)} {telemetryExtensions.map(this.renderExtension)}
{sentryIsInitialized ? ( {sentryDsn ? (
<React.Fragment key='sentry'> <React.Fragment key='sentry'>
<section id='sentry' className="small"> <section id='sentry' className="small">
<SubTitle title='Automatic Error Reporting' /> <SubTitle title='Automatic Error Reporting' />