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:
parent
ff704b5d3d
commit
f86360d641
@ -20,73 +20,58 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
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 => {
|
// default to false, in case instance of UserStore is not created (yet)
|
||||||
if (UserStore.getInstance().allowErrorReporting) {
|
const allowErrorReporting = UserStore.getInstance(false)?.allowErrorReporting ?? false;
|
||||||
return event;
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info(`🔒 [SENTRY-BEFORE-SEND-HOOK]: allowErrorReporting: false. Sentry event is caught but not sent to server.`);
|
if (allowErrorReporting) {
|
||||||
logger.info("🔒 [SENTRY-BEFORE-SEND-HOOK]: === START OF SENTRY EVENT ===");
|
return event;
|
||||||
logger.info(event);
|
}
|
||||||
logger.info("🔒 [SENTRY-BEFORE-SEND-HOOK]: === END OF SENTRY EVENT ===");
|
|
||||||
|
|
||||||
// if return null, the event won't be sent
|
logger.info(`🔒 [SENTRY-BEFORE-SEND-HOOK]: allowErrorReporting: ${allowErrorReporting}. Sentry event is caught but not sent to server.`);
|
||||||
// ref https://github.com/getsentry/sentry-javascript/issues/2039
|
logger.info("🔒 [SENTRY-BEFORE-SEND-HOOK]: === START OF SENTRY EVENT ===");
|
||||||
return null;
|
logger.info(event);
|
||||||
},
|
logger.info("🔒 [SENTRY-BEFORE-SEND-HOOK]: === END OF SENTRY EVENT ===");
|
||||||
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",
|
|
||||||
});
|
|
||||||
|
|
||||||
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.`);
|
"process": processName
|
||||||
} catch (error) {
|
}
|
||||||
logger.warn(`⚠️ [SENTRY-INIT]: Sentry.init() error: ${error?.message ?? error}.`);
|
},
|
||||||
}
|
environment: isProduction ? "production" : "development",
|
||||||
} catch (error) {
|
});
|
||||||
logger.warn(`⚠️ [SENTRY-INIT]: Error loading Sentry module ${error?.message ?? error}.`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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 ?? "";
|
||||||
|
|||||||
@ -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();
|
||||||
|
|||||||
@ -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();
|
||||||
|
|||||||
@ -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' />
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user