Add Sentry to capture exceptions and console.error messages, add a checkbox in preference to enable/disable error reporting (#3252)

This commit is contained in:
chh authored and GitHub committed 2021-07-08 19:22:12 +03:00
1 parent 3b31dadf19
commit 05b1a2fc5d
14 files changed
+513 -71

No files matched your search

+115
View File
@@ -0,0 +1,115 @@
/**
* 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.
*/
import { CaptureConsole, Dedupe, Offline } from "@sentry/integrations";
import type { Event as SentryEvent } from "@sentry/types";
import { sentryDsn, sentryDsnIsValid, 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;
export const integrations = [
new CaptureConsole({ levels: ["error"] }),
new Dedupe(),
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"
}
};
};
export const environment = isProduction ? "production" : "development";
const logInitError = (reason: string) => {
logger.warn(`⚠️ [SENTRY-INIT]: ${reason}, Sentry is not initialized.`);
};
export const beforeSend = (event: SentryEvent) => {
const allowErrorReporting = UserStore.getInstance().allowErrorReporting;
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]: === START OF SENTRY EVENT ===");
logger.info(event);
logger.info("🔒 [SENTRY-BEFORE-SEND-HOOK]: === END OF SENTRY EVENT ===");
// 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;
}
const processType = process.type as typeof electronProcessType[number];
let Sentry;
try {
// if in main process
if (processType === "browser") {
Sentry = require("@sentry/electron/dist/main");
}
// if in renderer process
if (processType === "renderer") {
Sentry = require("@sentry/electron/dist/renderer");
}
} catch (error) {
logInitError(`Error loading Sentry module ${error?.message}`);
return;
}
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;
}
};
@@ -106,6 +106,19 @@ const allowTelemetry: PreferenceDescription<boolean> = {
},
};
const allowErrorReporting: PreferenceDescription<boolean> = {
fromStore(val) {
return val ?? true;
},
toStore(val) {
if (val === true) {
return undefined;
}
return val;
},
};
const downloadMirror: PreferenceDescription<string> = {
fromStore(val) {
return val ?? "default";
@@ -227,6 +240,7 @@ export const DESCRIPTORS = {
localeTimezone,
allowUntrustedCAs,
allowTelemetry,
allowErrorReporting,
downloadMirror,
downloadKubectlBinaries,
downloadBinariesPath,
+3
View File
@@ -59,6 +59,7 @@ export class UserStore extends BaseStore<UserStoreModel> /* implements UserStore
@observable seenContexts = observable.set<string>();
@observable newContexts = observable.set<string>();
@observable allowTelemetry: boolean;
@observable allowErrorReporting: boolean;
@observable allowUntrustedCAs: boolean;
@observable colorTheme: string;
@observable localeTimezone: string;
@@ -169,6 +170,7 @@ export class UserStore extends BaseStore<UserStoreModel> /* implements UserStore
this.localeTimezone = DESCRIPTORS.localeTimezone.fromStore(preferences?.localeTimezone);
this.allowUntrustedCAs = DESCRIPTORS.allowUntrustedCAs.fromStore(preferences?.allowUntrustedCAs);
this.allowTelemetry = DESCRIPTORS.allowTelemetry.fromStore(preferences?.allowTelemetry);
this.allowErrorReporting = DESCRIPTORS.allowErrorReporting.fromStore(preferences?.allowErrorReporting);
this.downloadMirror = DESCRIPTORS.downloadMirror.fromStore(preferences?.downloadMirror);
this.downloadKubectlBinaries = DESCRIPTORS.downloadKubectlBinaries.fromStore(preferences?.downloadKubectlBinaries);
this.downloadBinariesPath = DESCRIPTORS.downloadBinariesPath.fromStore(preferences?.downloadBinariesPath);
@@ -188,6 +190,7 @@ export class UserStore extends BaseStore<UserStoreModel> /* implements UserStore
localeTimezone: DESCRIPTORS.localeTimezone.toStore(this.localeTimezone),
allowUntrustedCAs: DESCRIPTORS.allowUntrustedCAs.toStore(this.allowUntrustedCAs),
allowTelemetry: DESCRIPTORS.allowTelemetry.toStore(this.allowTelemetry),
allowErrorReporting: DESCRIPTORS.allowErrorReporting.toStore(this.allowErrorReporting),
downloadMirror: DESCRIPTORS.downloadMirror.toStore(this.downloadMirror),
downloadKubectlBinaries: DESCRIPTORS.downloadKubectlBinaries.toStore(this.downloadKubectlBinaries),
downloadBinariesPath: DESCRIPTORS.downloadBinariesPath.toStore(this.downloadBinariesPath),
+2
View File
@@ -51,6 +51,8 @@ export * from "./tar";
export * from "./toggle-set";
export * from "./toJS";
export * from "./type-narrowing";
export * from "./isValidURL";
export * from "./isValidSentryDsn";
import * as iter from "./iter";
+39
View File
@@ -0,0 +1,39 @@
/**
* 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;
};
+39
View File
@@ -0,0 +1,39 @@
/**
* 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;
};
+6
View File
@@ -24,6 +24,8 @@ 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";
@@ -70,3 +72,7 @@ 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 sentryDsnIsValid = isValidURL(sentryDsn) && isValidSentryDsn(sentryDsn);