mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Cleanup sentry integration
- Don't setup sentry until after UserStore has been initialized. It won't send anything and furthermore UserStore.getInstance() will throw - Remove isValidURL as we already have input_validators.isUrl - Remove isValidSentryDsn as Sentry will report that for us. Furthermore, the user setting can just assume that it being set in the package.json is good enough. - Move to using import() for the sentry parts as it types correctly in TS. Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
05b1a2fc5d
commit
dd5cd89ba5
@ -21,12 +21,14 @@
|
||||
|
||||
import { CaptureConsole, Dedupe, Offline } from "@sentry/integrations";
|
||||
import type { Event as SentryEvent } from "@sentry/types";
|
||||
import { sentryDsn, sentryDsnIsValid, isProduction } from "./vars";
|
||||
import { sentryDsn, isProduction } from "./vars";
|
||||
import { UserStore } from "./user-store";
|
||||
import logger from "../main/logger";
|
||||
|
||||
// https://www.electronjs.org/docs/api/process#processtype-readonly
|
||||
const electronProcessType = ["browser", "renderer", "worker"] as const;
|
||||
type ElectronProcessType = "browser" | "renderer" | "worker";
|
||||
|
||||
const processType = process.type as ElectronProcessType;
|
||||
|
||||
export const integrations = [
|
||||
new CaptureConsole({ levels: ["error"] }),
|
||||
@ -34,27 +36,25 @@ export const integrations = [
|
||||
new Offline()
|
||||
];
|
||||
|
||||
const initialScope = (processType: typeof electronProcessType[number]) => {
|
||||
return {
|
||||
tags: {
|
||||
// "translate" browser to 'main' as Lens developer more familiar with the term 'main'
|
||||
"process": processType === "browser" ? "main" : "renderer"
|
||||
}
|
||||
};
|
||||
const initialScope = {
|
||||
tags: {
|
||||
// "translate" browser to 'main' as Lens developer more familiar with the term 'main'
|
||||
"process": processType === "browser" ? "main" : "renderer"
|
||||
}
|
||||
};
|
||||
|
||||
export const environment = isProduction ? "production" : "development";
|
||||
const environment = isProduction ? "production" : "development";
|
||||
|
||||
const logInitError = (reason: string) => {
|
||||
function logInitError(reason: string) {
|
||||
logger.warn(`⚠️ [SENTRY-INIT]: ${reason}, Sentry is not initialized.`);
|
||||
};
|
||||
}
|
||||
|
||||
export const beforeSend = (event: SentryEvent) => {
|
||||
const allowErrorReporting = UserStore.getInstance().allowErrorReporting;
|
||||
function beforeSend(event: SentryEvent) {
|
||||
if (UserStore.getInstance().allowErrorReporting) {
|
||||
return event;
|
||||
}
|
||||
|
||||
if (allowErrorReporting) return event;
|
||||
|
||||
logger.info(`🔒 [SENTRY-BEFORE-SEND-HOOK]: allowErrorReporting: ${allowErrorReporting}. Sentry event is caught but not sent to server.`);
|
||||
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 ===");
|
||||
@ -62,54 +62,37 @@ export const beforeSend = (event: SentryEvent) => {
|
||||
// if return null, the event won't be sent
|
||||
// ref https://github.com/getsentry/sentry-javascript/issues/2039
|
||||
return null;
|
||||
};
|
||||
}
|
||||
|
||||
export const SentryInit = () => {
|
||||
if (!sentryDsnIsValid) {
|
||||
logInitError("Sentry DSN is not valid");
|
||||
|
||||
return;
|
||||
async function requireSentry() {
|
||||
if (processType === "browser") {
|
||||
return import("@sentry/electron/dist/main");
|
||||
}
|
||||
|
||||
const processType = process.type as typeof electronProcessType[number];
|
||||
if (processType === "renderer") {
|
||||
return import("@sentry/electron/dist/renderer");
|
||||
}
|
||||
|
||||
let Sentry;
|
||||
throw new Error(`Unsupported process type ${processType}`);
|
||||
}
|
||||
|
||||
export async function SentryInit() {
|
||||
try {
|
||||
// if in main process
|
||||
if (processType === "browser") {
|
||||
Sentry = require("@sentry/electron/dist/main");
|
||||
}
|
||||
const Sentry = await requireSentry();
|
||||
|
||||
// if in renderer process
|
||||
if (processType === "renderer") {
|
||||
Sentry = require("@sentry/electron/dist/renderer");
|
||||
try {
|
||||
Sentry.init({
|
||||
beforeSend,
|
||||
dsn: sentryDsn,
|
||||
integrations,
|
||||
initialScope,
|
||||
environment
|
||||
});
|
||||
logger.info(`✔️ [SENTRY-INIT]: Sentry for ${processType} is initialized.`);
|
||||
} catch (error) {
|
||||
return logInitError(`Sentry.init() error ${error?.message}`);
|
||||
}
|
||||
} catch (error) {
|
||||
logInitError(`Error loading Sentry module ${error?.message}`);
|
||||
|
||||
return;
|
||||
return logInitError(`Error loading Sentry module ${error?.message ?? error}`);
|
||||
}
|
||||
|
||||
|
||||
if (!Sentry) {
|
||||
logInitError(`Unsupported process type ${processType}`);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
Sentry.init({
|
||||
beforeSend,
|
||||
dsn: sentryDsn,
|
||||
integrations,
|
||||
initialScope: initialScope(processType),
|
||||
environment
|
||||
});
|
||||
logger.info(`✔️ [SENTRY-INIT]: Sentry for ${processType} is initialized.`);
|
||||
} catch (error) {
|
||||
logInitError(`Sentry.init() error ${error?.message}`);
|
||||
|
||||
return;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@ -1,39 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 OpenLens Authors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
// Regular expression used to parse a Sentry Dsn.
|
||||
// from https://github.com/getsentry/sentry-javascript/blob/f624f442fd76bf655de6def9fa4d7631735ebba0/packages/utils/src/dsn.ts#L6
|
||||
const DSN_REGEX = /^(?:(\w+):)\/\/(?:(\w+)(?::(\w+))?@)([\w.-]+)(?::(\d+))?\/(.+)/;
|
||||
|
||||
/**
|
||||
* check if string is a valid Sentry dsn
|
||||
*
|
||||
* @param {(string | undefined | null)} string
|
||||
* @return {boolean}
|
||||
*/
|
||||
export const isValidSentryDsn = (string: string | undefined | null) => {
|
||||
|
||||
const valid = DSN_REGEX.exec(string);
|
||||
|
||||
return valid;
|
||||
|
||||
};
|
||||
@ -1,39 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 OpenLens Authors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* check if string is valid URL
|
||||
*
|
||||
* @param {(string | undefined | null)} url
|
||||
* @return {boolean}
|
||||
*/
|
||||
export const isValidURL = (url: string | undefined | null) => {
|
||||
try {
|
||||
new URL(url);
|
||||
|
||||
return true;
|
||||
} catch {
|
||||
// ignore TypeError and return false
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
@ -24,8 +24,6 @@ import path from "path";
|
||||
import { SemVer } from "semver";
|
||||
import packageInfo from "../../package.json";
|
||||
import { defineGlobal } from "./utils/defineGlobal";
|
||||
import { isValidURL } from "./utils/isValidURL";
|
||||
import { isValidSentryDsn } from "./utils/isValidSentryDsn";
|
||||
|
||||
export const isMac = process.platform === "darwin";
|
||||
export const isWindows = process.platform === "win32";
|
||||
@ -71,8 +69,6 @@ export const slackUrl = "https://join.slack.com/t/k8slens/shared_invite/enQtOTc5
|
||||
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 docsUrl = "https://docs.k8slens.dev/main/" as string;
|
||||
|
||||
export const sentryDsn = packageInfo.config?.sentryDsn;
|
||||
export const sentryDsnIsValid = isValidURL(sentryDsn) && isValidSentryDsn(sentryDsn);
|
||||
|
||||
|
||||
@ -61,8 +61,6 @@ import { ExtensionsStore } from "../extensions/extensions-store";
|
||||
import { FilesystemProvisionerStore } from "./extension-filesystem";
|
||||
import { SentryInit } from "../common/sentry";
|
||||
|
||||
SentryInit();
|
||||
|
||||
const workingDir = path.join(app.getPath("appData"), appName);
|
||||
const cleanup = disposer();
|
||||
|
||||
@ -141,8 +139,15 @@ app.on("ready", async () => {
|
||||
|
||||
logger.info("💾 Loading stores");
|
||||
|
||||
ClusterStore.createInstance().provideInitialFromMain();
|
||||
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();
|
||||
FilesystemProvisionerStore.createInstance();
|
||||
|
||||
@ -47,6 +47,7 @@ import { WeblinkStore } from "../common/weblink-store";
|
||||
import { ExtensionsStore } from "../extensions/extensions-store";
|
||||
import { FilesystemProvisionerStore } from "../main/extension-filesystem";
|
||||
import { ThemeStore } from "./theme.store";
|
||||
import { SentryInit } from "../common/sentry";
|
||||
|
||||
configurePackages();
|
||||
|
||||
@ -85,6 +86,13 @@ export async function bootstrap(App: AppComponent) {
|
||||
ExtensionDiscovery.createInstance().init();
|
||||
|
||||
UserStore.createInstance();
|
||||
|
||||
/**
|
||||
* There is no point setting up sentry before UserStore is initialized as
|
||||
* `allowErrorReporting` will always be falsy.
|
||||
*/
|
||||
await SentryInit();
|
||||
|
||||
await ClusterStore.createInstance().loadInitialOnRenderer();
|
||||
HotbarStore.createInstance();
|
||||
ExtensionsStore.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 { sentryDsnIsValid } from "../../../common/vars";
|
||||
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)}
|
||||
{sentryDsnIsValid ? (
|
||||
{sentryDsn ? (
|
||||
<React.Fragment key='sentry'>
|
||||
<section id='sentry' className="small">
|
||||
<SubTitle title='Automatic Error Reporting' />
|
||||
|
||||
@ -36,9 +36,6 @@ import { registerIpcHandlers } from "./ipc";
|
||||
import { ipcRenderer } from "electron";
|
||||
import { IpcRendererNavigationEvents } from "./navigation/events";
|
||||
import { catalogEntityRegistry } from "./api/catalog-entity-registry";
|
||||
import { SentryInit } from "../common/sentry";
|
||||
|
||||
SentryInit();
|
||||
|
||||
@observer
|
||||
export class LensApp extends React.Component {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user