perf: sentry和posthog增加try...catch保护

pull/64/head
moonrailgun 3 years ago
parent 67960ac877
commit 1d924f1692

@ -1,51 +1,55 @@
import posthog from 'posthog-js'; import posthog from 'posthog-js';
import { sharedEvent } from '@capital/common'; import { sharedEvent } from '@capital/common';
posthog.init('phc_xRCv3qbbOBMQkz31kbYMngXxn7Ey5JMu0BZIFktO6km', { try {
api_host: 'https://app.posthog.com', posthog.init('phc_xRCv3qbbOBMQkz31kbYMngXxn7Ey5JMu0BZIFktO6km', {
autocapture: false, // 关闭autocapture以节约事件用量 api_host: 'https://app.posthog.com',
disable_session_recording: true, // 关闭自动录屏(不需要且一直报错) autocapture: false, // 关闭autocapture以节约事件用量
}); disable_session_recording: true, // 关闭自动录屏(不需要且一直报错)
});
const PLUGIN_NAME = 'posthog';
const PLUGIN_NAME = 'posthog'; console.log(`Plugin ${PLUGIN_NAME} is loaded`);
console.log(`Plugin ${PLUGIN_NAME} is loaded`); setTimeout(() => {
console.log('Report plugin install status');
setTimeout(() => { try {
console.log('Report plugin install status'); const d = window.localStorage['$TailchatInstalledPlugins'];
if (!d) {
posthog.capture('Report Plugin', {
plugins: [],
pluginNum: 0,
pluginRaw: '',
});
return;
}
const storage = JSON.parse(d);
const list = storage.rawData;
if (!list || !Array.isArray(list)) {
// 格式不匹配
return;
}
try {
const d = window.localStorage['$TailchatInstalledPlugins'];
if (!d) {
posthog.capture('Report Plugin', { posthog.capture('Report Plugin', {
plugins: [], plugins: list.map((item) => item.name), // 主要收集名称列表
pluginNum: 0, pluginNum: list.length,
pluginRaw: '', pluginRaw: JSON.stringify(list), // 原始信息
}); });
return; } catch (err) {
} // Ignore error
const storage = JSON.parse(d);
const list = storage.rawData;
if (!list || !Array.isArray(list)) {
// 格式不匹配
return;
} }
}, 2000);
posthog.capture('Report Plugin', { sharedEvent.on('loginSuccess', (userInfo) => {
plugins: list.map((item) => item.name), // 主要收集名称列表 posthog.identify(userInfo._id, {
pluginNum: list.length, email: userInfo.email,
pluginRaw: JSON.stringify(list), // 原始信息 username: `${userInfo.nickname}#${userInfo.discriminator}`,
avatar: userInfo.avatar,
temporary: userInfo.temporary,
}); });
} catch (err) {
// Ignore error
}
}, 2000);
sharedEvent.on('loginSuccess', (userInfo) => {
posthog.identify(userInfo._id, {
email: userInfo.email,
username: `${userInfo.nickname}#${userInfo.discriminator}`,
avatar: userInfo.avatar,
temporary: userInfo.temporary,
}); });
}); } catch (err) {
console.error(err);
}

@ -2,24 +2,28 @@ import * as Sentry from '@sentry/react';
import { BrowserTracing } from '@sentry/tracing'; import { BrowserTracing } from '@sentry/tracing';
import { sharedEvent } from '@capital/common'; import { sharedEvent } from '@capital/common';
Sentry.init({ try {
dsn: 'https://177fd98a1e9e4deba84146a769633c32@o4504196236836864.ingest.sentry.io/4504196241293312', Sentry.init({
replaysSessionSampleRate: 0, dsn: 'https://177fd98a1e9e4deba84146a769633c32@o4504196236836864.ingest.sentry.io/4504196241293312',
replaysOnErrorSampleRate: 1.0, replaysSessionSampleRate: 0,
integrations: [new BrowserTracing(), new Sentry.Replay()], replaysOnErrorSampleRate: 1.0,
integrations: [new BrowserTracing(), new Sentry.Replay()],
// Set tracesSampleRate to 1.0 to capture 100% // Set tracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring. // of transactions for performance monitoring.
// We recommend adjusting this value in production // We recommend adjusting this value in production
tracesSampleRate: 0.1, // reduce sentry quota usage tracesSampleRate: 0.1, // reduce sentry quota usage
}); });
sharedEvent.on('loginSuccess', (userInfo) => { sharedEvent.on('loginSuccess', (userInfo) => {
Sentry.setUser({ Sentry.setUser({
id: userInfo._id, id: userInfo._id,
email: userInfo.email, email: userInfo.email,
username: `${userInfo.nickname}#${userInfo.discriminator}`, username: `${userInfo.nickname}#${userInfo.discriminator}`,
avatar: userInfo.avatar, avatar: userInfo.avatar,
temporary: userInfo.temporary, temporary: userInfo.temporary,
});
}); });
}); } catch (err) {
console.error(err);
}

Loading…
Cancel
Save