Skip to content

Commit

Permalink
feat: support traces on Sentry (#6349)
Browse files Browse the repository at this point in the history
  • Loading branch information
huhuanming authored Dec 16, 2024
1 parent 1cb778c commit f580771
Show file tree
Hide file tree
Showing 15 changed files with 271 additions and 391 deletions.
2 changes: 1 addition & 1 deletion apps/desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"dependencies": {
"@onekeyhq/components": "*",
"@onekeyhq/kit": "*",
"@sentry/electron": "^5.7.0",
"@sentry/electron": "5.8.0",
"adm-zip": "^0.5.10",
"electron-context-menu": "^3.5.0",
"electron-is-dev": "^2.0.0",
Expand Down
4 changes: 4 additions & 0 deletions apps/desktop/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ build({
__dirname,
'../../desktop/src-electron/libs/react-native-mock',
),
'@sentry/react-native': path.join(
__dirname,
'../../desktop/src-electron/libs/sentry-react-native-mock',
),
'react-native-uuid': path.join(
__dirname,
'../../../node_modules/react-native-uuid/dist',
Expand Down
2 changes: 2 additions & 0 deletions apps/desktop/src-electron/libs/sentry-react-native-mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const reactNativeTracingIntegration = () => {};
export const reactNavigationIntegration = () => {};
7 changes: 1 addition & 6 deletions apps/mobile/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
import '@onekeyhq/shared/src/polyfills';

import { KitProvider } from '@onekeyhq/kit';
import {
initSentry,
withSentryHOC,
} from '@onekeyhq/shared/src/modules3rdParty/sentry';

initSentry();
import { withSentryHOC } from '@onekeyhq/shared/src/modules3rdParty/sentry';

export default withSentryHOC(KitProvider);
4 changes: 3 additions & 1 deletion apps/mobile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ const {
markJsBundleLoadedTime,
} = require('@onekeyhq/shared/src/modules3rdParty/metrics');

import { I18nManager } from 'react-native';
const { initSentry } = require('@onekeyhq/shared/src/modules3rdParty/sentry');

markJsBundleLoadedTime();
initSentry();

import { I18nManager } from 'react-native';
import { registerRootComponent } from 'expo';

import App from './App';
Expand Down
2 changes: 1 addition & 1 deletion apps/mobile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@react-native-community/netinfo": "^11.4.1",
"@react-native-community/slider": "4.4.3",
"@react-native-google-signin/google-signin": "^9.1.0",
"@sentry/react-native": "^6.2.0",
"@sentry/react-native": "6.4.0",
"@shopify/flash-list": "^1.6.3",
"@types/react-native-canvas": "^0.1.13",
"@walletconnect/modal-react-native": "1.1.0",
Expand Down
28 changes: 21 additions & 7 deletions jest-setup.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// require('react-native-reanimated').setUpTests();

// FIX: ReferenceError: self is not defined
global.self = global.self || global;
globalThis.self = globalThis.self || globalThis;

class LocalStorageMock {
constructor() {
Expand All @@ -25,14 +25,14 @@ class LocalStorageMock {
}
}

global.localStorage = new LocalStorageMock();
global.$$onekeyAppStorage = new LocalStorageMock();
global.addEventListener = jest.fn;
global.fetch = require('node-fetch');
global.WebSocket = require('isomorphic-ws');
globalThis.localStorage = new LocalStorageMock();
globalThis.$$onekeyAppStorage = new LocalStorageMock();
globalThis.addEventListener = jest.fn;
globalThis.fetch = require('node-fetch');
globalThis.WebSocket = require('isomorphic-ws');

if (typeof structuredClone === 'undefined') {
global.structuredClone = require('@ungap/structured-clone').default;
globalThis.structuredClone = require('@ungap/structured-clone').default;
}

jest.mock('react-native-zip-archive', () => ({
Expand Down Expand Up @@ -63,6 +63,20 @@ jest.mock('react-native-device-info', () => ({
getUsedMemorySync: jest.fn(),
}));

jest.mock('@sentry/electron/renderer', () => ({
init: () => jest.fn(),
}));

jest.mock('@sentry/react', () => ({
init: () => jest.fn(),
}));

jest.mock('@sentry/react-native', () => ({
init: () => jest.fn(),
reactNavigationIntegration: () => jest.fn(),
reactNativeTracingIntegration: () => jest.fn(),
}));

jest.mock('expo-localization', () => ({
locale: '',
}));
Expand Down
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@
"@polkadot/extension-inject": "0.46.6",
"@polkadot/types": "11.3.1",
"@polkadot/util-crypto": "12.6.2",
"@sentry/react": "^8.38.0",
"@sentry/browser": "8.42.0",
"@sentry/core": "8.42.0",
"@sentry/react": "8.42.0",
"@substrate/txwrapper-polkadot": "7.5.1",
"@tamagui/animations-moti": "1.108.0",
"@tamagui/config": "1.108.0",
Expand Down Expand Up @@ -241,6 +243,11 @@
"@walletconnect/universal-provider": "2.11.2",
"@walletconnect/core": "2.11.2",
"fs": "npm:@favware/[email protected]",
"react-native-webview": "npm:@onekeyfe/[email protected]"
"react-native-webview": "npm:@onekeyfe/[email protected]",
"@sentry/browser": "npm:8.42.0",
"@sentry/core": "npm:8.42.0",
"@sentry/react": "npm:8.42.0",
"@sentry/types": "npm:8.42.0",
"@sentry/utils": "npm:8.42.0"
}
}
1 change: 1 addition & 0 deletions packages/kit/src/components/LastActivityTracker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const LastActivityTracker = () => {
}),
});
setSentryUser({
id: instanceId,
instanceId,
platform: platformEnv.appPlatform || '',
appChannel: platformEnv.appChannel || '',
Expand Down
15 changes: 13 additions & 2 deletions packages/shared/src/modules3rdParty/sentry/basicOptions.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import {
reactNativeTracingIntegration,
reactNavigationIntegration,
} from '@sentry/react-native';
import wordLists from 'bip39/src/wordlists/english.json';

import { memoizee } from '@onekeyhq/shared/src/utils/cacheUtils';

import type { BrowserOptions } from '@sentry/browser';

// dirty check for common private key formats
const checkPrivateKey = (errorText: string) =>
typeof errorText === 'string' && errorText.length > 26;

const lazyLoadWordSet = memoizee(() => new Set(wordLists));

export const navigationIntegration = reactNavigationIntegration({
enableTimeToInitialDisplay: true,
});

// Check if text contains mnemonic phrases
const checkAndRedactMnemonicWords = (words: string[]) => {
if (!Array.isArray(words)) {
Expand Down Expand Up @@ -43,8 +50,10 @@ const checkAndRedactMnemonicWords = (words: string[]) => {
};

export const basicOptions: BrowserOptions = {
enabled: process.env.NODE_ENV === 'production',
enabled: true,
maxBreadcrumbs: 100,
tracesSampleRate: 1.0,
profilesSampleRate: 1.0,
beforeSend: (event) => {
if (Array.isArray(event.exception?.values)) {
for (let index = 0; index < event.exception.values.length; index += 1) {
Expand Down Expand Up @@ -81,6 +90,8 @@ export const buildOptions = (Sentry: typeof import('@sentry/react')) => ({
});

export const buildIntegrations = (Sentry: typeof import('@sentry/react')) => [
navigationIntegration,
reactNativeTracingIntegration(),
Sentry.browserProfilingIntegration(),
Sentry.browserTracingIntegration(),
Sentry.breadcrumbsIntegration({
Expand Down
8 changes: 4 additions & 4 deletions packages/shared/src/modules3rdParty/sentry/index.desktop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ import { buildIntegrations } from './basicOptions';

export * from '@sentry/electron/renderer';

export * from './basicOptions';

export const initSentry = () => {
if (process.env.NODE_ENV !== 'production') {
return;
}
Sentry.init({
tracesSampleRate: 1.0,
profilesSampleRate: 1.0,
integrations: buildIntegrations(Sentry as any),
});
};
Expand All @@ -26,7 +30,3 @@ export const withSentryHOC = (
console.error('error', error, info);
},
});

export const navigationIntegration = {
registerNavigationContainer: (ref: any) => {},
};
6 changes: 2 additions & 4 deletions packages/shared/src/modules3rdParty/sentry/index.ext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export const initSentry = () => {

export * from '@sentry/react';

export * from './basicOptions';

export const nativeCrash = () => {};

export const withSentryHOC = (
Expand All @@ -28,7 +30,3 @@ export const withSentryHOC = (
console.error('error', error, info);
},
});

export const navigationIntegration = {
registerNavigationContainer: (ref: any) => {},
};
18 changes: 12 additions & 6 deletions packages/shared/src/modules3rdParty/sentry/index.native.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import type { ComponentType } from 'react';

import {
hermesProfilingIntegration,
init,
reactNavigationIntegration,
reactNativeTracingIntegration,
nativeCrash as sentryNativeCrash,
withErrorBoundary,
withProfiler,
wrap,
} from '@sentry/react-native';

import { basicOptions } from './basicOptions';
import { basicOptions, navigationIntegration } from './basicOptions';

export * from '@sentry/react-native';

export const navigationIntegration = reactNavigationIntegration({
enableTimeToInitialDisplay: true,
});
export * from './basicOptions';

export const initSentry = () => {
if (process.env.NODE_ENV !== 'production') {
Expand All @@ -27,7 +26,14 @@ export const initSentry = () => {
maxCacheItems: 60,
enableAppHangTracking: true,
appHangTimeoutInterval: 5,
integrations: [navigationIntegration],
integrations: [
navigationIntegration,
reactNativeTracingIntegration(),
hermesProfilingIntegration({
platformProfilers: true,
}),
],
enableAutoPerformanceTracing: true,
});
};

Expand Down
6 changes: 2 additions & 4 deletions packages/shared/src/modules3rdParty/sentry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { basicOptions, buildIntegrations, buildOptions } from './basicOptions';

export * from '@sentry/react';

export * from './basicOptions';

export const initSentry = () => {
if (process.env.NODE_ENV !== 'production') {
return;
Expand All @@ -28,7 +30,3 @@ export const withSentryHOC = (
console.error('error', error, info);
},
});

export const navigationIntegration = {
registerNavigationContainer: (ref: any) => {},
};
Loading

0 comments on commit f580771

Please sign in to comment.