1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Add sentryDsn to package.json, and do not init Sentry (and error report checkbox) if dsn is not valid

Signed-off-by: Hung-Han (Henry) Chen <chenhungh@gmail.com>
This commit is contained in:
Hung-Han (Henry) Chen 2021-07-06 14:38:08 +03:00
parent 4fb7113593
commit 16d19c7d00
No known key found for this signature in database
GPG Key ID: 54B44603D251B788
7 changed files with 116 additions and 63 deletions

View File

@ -49,7 +49,8 @@
}, },
"config": { "config": {
"bundledKubectlVersion": "1.18.15", "bundledKubectlVersion": "1.18.15",
"bundledHelmVersion": "3.5.4" "bundledHelmVersion": "3.5.4",
"sentryDsn": ""
}, },
"engines": { "engines": {
"node": ">=12 <13" "node": ">=12 <13"

View File

@ -59,7 +59,7 @@ export class UserStore extends BaseStore<UserStoreModel> /* implements UserStore
@observable seenContexts = observable.set<string>(); @observable seenContexts = observable.set<string>();
@observable newContexts = observable.set<string>(); @observable newContexts = observable.set<string>();
@observable allowTelemetry: boolean; @observable allowTelemetry: boolean;
@observable allowErrorReporting: boolean; @observable allowErrorReporting: dsnIsValid;
@observable allowUntrustedCAs: boolean; @observable allowUntrustedCAs: boolean;
@observable colorTheme: string; @observable colorTheme: string;
@observable localeTimezone: string; @observable localeTimezone: string;

View File

@ -0,0 +1,41 @@
/**
* 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) => {
if (Boolean(url)) {
try {
new URL(url);
return true;
} catch {
// ignore TypeError and return false
}
}
return false;
};

View File

@ -24,6 +24,7 @@ import path from "path";
import { SemVer } from "semver"; import { SemVer } from "semver";
import packageInfo from "../../package.json"; import packageInfo from "../../package.json";
import { defineGlobal } from "./utils/defineGlobal"; import { defineGlobal } from "./utils/defineGlobal";
import { isValidURL } from "./utils/isValidURL";
export const isMac = process.platform === "darwin"; export const isMac = process.platform === "darwin";
export const isWindows = process.platform === "win32"; export const isWindows = process.platform === "win32";
@ -71,7 +72,6 @@ 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;
// Sentry DSNs are safe to keep public because they only allow submission of new events export const dsn = packageInfo.config.sentryDsn;
// and related event data; they do not allow read access to any information. export const dsnIsValid = isValidURL(packageInfo.config?.sentryDsn);
// ref: https://docs.sentry.io/product/sentry-basics/dsn-explainer/#dsn-utilization
export const dsn = "https://673ac8dc2d4649b5bc498639765f995b@o625296.ingest.sentry.io/5753768";

View File

@ -26,7 +26,7 @@ import * as Mobx from "mobx";
import * as LensExtensionsCommonApi from "../extensions/common-api"; import * as LensExtensionsCommonApi from "../extensions/common-api";
import * as LensExtensionsMainApi from "../extensions/main-api"; import * as LensExtensionsMainApi from "../extensions/main-api";
import { app, autoUpdater, dialog, powerMonitor } from "electron"; import { app, autoUpdater, dialog, powerMonitor } from "electron";
import { appName, isMac, productName, dsn, isProduction } from "../common/vars"; import { appName, isMac, productName, dsn, dsnIsValid, isProduction } from "../common/vars";
import path from "path"; import path from "path";
import { LensProxy } from "./proxy/lens-proxy"; import { LensProxy } from "./proxy/lens-proxy";
import { WindowManager } from "./window-manager"; import { WindowManager } from "./window-manager";
@ -62,27 +62,30 @@ import { FilesystemProvisionerStore } from "./extension-filesystem";
import { CaptureConsole, Dedupe, Offline } from "@sentry/integrations"; import { CaptureConsole, Dedupe, Offline } from "@sentry/integrations";
import * as Sentry from "@sentry/electron/dist/main"; import * as Sentry from "@sentry/electron/dist/main";
Sentry.init({ if (dsnIsValid) {
beforeSend(event) { Sentry.init({
const allow = UserStore.getInstance().allowErrorReporting; beforeSend(event) {
const allow = UserStore.getInstance().allowErrorReporting;
if (allow) return event; if (allow) return event;
return null;
},
dsn,
integrations: [
new CaptureConsole({ levels: ["error"] }),
new Dedupe(),
new Offline()
],
initialScope: {
tags: {
"process": "main"
}
},
environment: isProduction ? "production" : "development"
});
}
return null;
},
dsn,
integrations: [
new CaptureConsole({ levels: ["error"] }),
new Dedupe(),
new Offline()
],
initialScope: {
tags: {
"process": "main"
}
},
environment: isProduction ? "production" : "development"
});
const workingDir = path.join(app.getPath("appData"), appName); const workingDir = path.join(app.getPath("appData"), appName);
const cleanup = disposer(); const cleanup = disposer();

View File

@ -41,6 +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 { dsnIsValid } from "../../../common/vars";
enum Pages { enum Pages {
Application = "application", Application = "application",
@ -258,25 +259,29 @@ 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)}
<React.Fragment key='sentry'> {dsnIsValid ? (
<section id='sentry' className="small"> <React.Fragment key='sentry'>
<SubTitle title='Automatic Error Reporting' /> <section id='sentry' className="small">
<Checkbox <SubTitle title='Automatic Error Reporting' />
label="Allow automatic error reporting" <Checkbox
value={UserStore.getInstance().allowErrorReporting} label="Allow automatic error reporting"
onChange={value => { value={UserStore.getInstance().allowErrorReporting}
UserStore.getInstance().allowErrorReporting = value; onChange={value => {
}} UserStore.getInstance().allowErrorReporting = value;
/> }}
<div className="hint"> />
<span> <div className="hint">
<span>
Automatic error reports provide vital information about issues and application crashes. Automatic error reports provide vital information about issues and application crashes.
It is highly recommended to keep this feature enabled to ensure fast turnaround for issues you might encounter. It is highly recommended to keep this feature enabled to ensure fast turnaround for issues you might encounter.
</span> </span>
</div> </div>
</section> </section>
<hr className="small" /> <hr className="small" />
</React.Fragment> </React.Fragment>) :
// we don't need to shows the checkbox at all if dsn is not a valid url
null
}
</section> </section>
)} )}
{this.activeTab == Pages.Extensions && ( {this.activeTab == Pages.Extensions && (

View File

@ -36,32 +36,35 @@ import { registerIpcHandlers } from "./ipc";
import { ipcRenderer } from "electron"; import { ipcRenderer } from "electron";
import { IpcRendererNavigationEvents } from "./navigation/events"; import { IpcRendererNavigationEvents } from "./navigation/events";
import { catalogEntityRegistry } from "./api/catalog-entity-registry"; import { catalogEntityRegistry } from "./api/catalog-entity-registry";
import { dsn, isProduction } from "../common/vars"; import { dsn, dsnIsValid, isProduction } from "../common/vars";
import { CaptureConsole, Dedupe, Offline } from "@sentry/integrations"; import { CaptureConsole, Dedupe, Offline } from "@sentry/integrations";
import * as Sentry from "@sentry/electron/dist/renderer"; import * as Sentry from "@sentry/electron/dist/renderer";
import { UserStore } from "../common/user-store"; import { UserStore } from "../common/user-store";
Sentry.init({ if (dsnIsValid) {
beforeSend(event) { Sentry.init({
const allow = UserStore.getInstance().allowErrorReporting; beforeSend(event) {
const allow = UserStore.getInstance().allowErrorReporting;
if (allow) return event; if (allow) return event;
return null;
},
dsn,
integrations: [
new CaptureConsole({ levels: ["error"] }),
new Dedupe(),
new Offline()
],
initialScope: {
tags: {
"process": "renderer"
}
},
environment: isProduction ? "production" : "development"
});
}
return null;
},
dsn,
integrations: [
new CaptureConsole({ levels: ["error"] }),
new Dedupe(),
new Offline()
],
initialScope: {
tags: {
"process": "renderer"
}
},
environment: isProduction ? "production" : "development"
});
@observer @observer
export class LensApp extends React.Component { export class LensApp extends React.Component {