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,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<void> {
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",
});
}

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 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 { 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();

View File

@ -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();

View File

@ -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 {
<section id="telemetry">
<h2 data-testid="telemetry-header">Telemetry</h2>
{telemetryExtensions.map(this.renderExtension)}
{sentryIsInitialized ? (
{sentryDsn ? (
<React.Fragment key='sentry'>
<section id='sentry' className="small">
<SubTitle title='Automatic Error Reporting' />