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,43 +20,38 @@
|
||||
*/
|
||||
|
||||
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) {
|
||||
beforeSend: (event) => {
|
||||
// default to false, in case instance of UserStore is not created (yet)
|
||||
const allowErrorReporting = UserStore.getInstance(false)?.allowErrorReporting ?? false;
|
||||
|
||||
if (allowErrorReporting) {
|
||||
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(event);
|
||||
logger.info("🔒 [SENTRY-BEFORE-SEND-HOOK]: === END OF SENTRY EVENT ===");
|
||||
@ -73,20 +68,10 @@ export async function SentryInit(): Promise<void> {
|
||||
],
|
||||
initialScope: {
|
||||
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",
|
||||
});
|
||||
|
||||
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}.`);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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 ?? "";
|
||||
|
||||
@ -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();
|
||||
|
||||
@ -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();
|
||||
|
||||
@ -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' />
|
||||
|
||||
Loading…
Reference in New Issue
Block a user