-
Notifications
You must be signed in to change notification settings - Fork 380
/
Copy pathSentry.ts
82 lines (74 loc) · 2.73 KB
/
Sentry.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { anonymizedPhone } from '@celo/utils/src/phoneNumbers'
import DeviceInfo from 'react-native-device-info'
import * as RNFS from 'react-native-fs'
import { Sentry } from 'react-native-sentry'
import { e164NumberSelector } from 'src/account/reducer'
import { SENTRY_URL } from 'src/config'
import { DispatchType, GetStateType } from 'src/redux/reducers'
import Logger from 'src/utils/Logger'
import { currentAccountSelector } from 'src/web3/selectors'
const TAG = 'sentry/Sentry'
// This should be called as early in the lifecycle of the app as possible.
export async function installSentry() {
if (!SENTRY_URL) {
Logger.info(TAG, 'installSentry', 'Sentry URL not found, skiping instalation')
return
}
await Sentry.config(SENTRY_URL).install()
Sentry.setTagsContext({
environment: DeviceInfo.getBundleId(),
react: true,
})
await uploadNdkCrashesIfAny()
Logger.info(TAG, 'installSentry', 'Sentry installation complete')
}
// This should not be called at cold start since it can slow down the cold start.
export const initializeSentryUserContext = () => async (
dispatch: DispatchType,
getState: GetStateType
) => {
const state = getState()
const account = currentAccountSelector(state)
if (!account) {
return
}
const phoneNumber =
e164NumberSelector(state) || (await DeviceInfo.getPhoneNumber()) || 'unknownPhoneNumber'
Logger.debug(
TAG,
'initializeSentryUserContext',
`Setting Sentry user context to "${phoneNumber}" and "${account}"`
)
Sentry.setUserContext({
username: anonymizedPhone(phoneNumber.slice(0, -4)),
extra: {
Address: account,
},
})
}
const uploadNdkCrashesIfAny = async () => {
// This file path should be same here and in MainApplication.java
const ndkCrashLogsFilePath = RNFS.CachesDirectoryPath + '/ndk_crash_logs.txt'
const ndkCrashLogcatLogsFilePath = RNFS.CachesDirectoryPath + '/ndk_crash_logcat_logs.txt'
if (!(await RNFS.exists(ndkCrashLogsFilePath))) {
Logger.debug(
'Sentry@uploadNdkCrashesIfAny',
`crash log file ${ndkCrashLogsFilePath} not found, no native crashes recorded`
)
return
}
const fileSize = parseInt((await RNFS.stat(ndkCrashLogsFilePath)).size, 10)
Logger.info(
'Sentry@uploadNdkCrashesIfAny',
`crash log file ${ndkCrashLogsFilePath} found (${fileSize} bytes), capturing it via Sentry`
)
const msg1 = (await RNFS.exists(ndkCrashLogcatLogsFilePath))
? await RNFS.readFile(ndkCrashLogcatLogsFilePath)
: 'Logcat logs not available'
const msg2 = await RNFS.readFile(ndkCrashLogsFilePath)
Sentry.captureMessage(`NDK crash\n${msg1}\n${msg2}`)
await RNFS.unlink(ndkCrashLogsFilePath)
if (!(await RNFS.exists(ndkCrashLogcatLogsFilePath))) {
await RNFS.unlink(ndkCrashLogcatLogsFilePath)
}
}