Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

limit sentry events #930

Merged
merged 20 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions main/errors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { init as initSentry, captureException, IPCMode } from '@sentry/electron'
import log from 'electron-log'
import type { Event } from '@sentry/types'

import showUnhandledExceptionDialog from '../windows/dialog/unhandledException'
goosewobbler marked this conversation as resolved.
Show resolved Hide resolved
import store from '../store'

const EVENT_RATE_LIMIT = 5

function getCrashReportFields () {
const fields = ['networks', 'networksMeta', 'tokens']

return fields.reduce((extra, field) => ({ ...extra, [field]: JSON.stringify(store('main', field) || {}) }), {})
}

function sanitizeStackFrame ({ module = '' }) {
const matches = /(.+)[\\|\/]frame[\\|\/]resources[\\|\/]app.asar[\\|\/](.+)/.exec(module)
if (matches && matches[2]) {
return `{asar}/${matches[2].replaceAll('\\', '/')}`
}
return module
}

function getSentryExceptions (event: Event) {
const exceptions = event?.exception?.values || []
const safeExceptions = exceptions.map((exception) => {
const frames = exception?.stacktrace?.frames || []
const safeFrames = frames.map((frame) => ({ ...frame, module: sanitizeStackFrame(frame) }))
return { stacktrace: { frames: safeFrames } }
})

return safeExceptions
}

// prevent showing the exit dialog more than once
let closing = false

export function uncaughtExceptionHandler (e) {
log.error('uncaughtException', e)

captureException(e)

if (e.code === 'EPIPE') {
log.error('uncaught EPIPE error', e)
return
}

if (!closing) {
closing = true

showUnhandledExceptionDialog(e.message, e.code)
}
}

export function init () {
let allowedEvents = EVENT_RATE_LIMIT

setInterval(() => {
if (allowedEvents < EVENT_RATE_LIMIT) {
allowedEvents++
}
}, 60_000)

initSentry({
// only use IPC from renderer process, not HTTP
ipcMode: IPCMode.Classic,
dsn: 'https://7b09a85b26924609bef5882387e2c4dc@o1204372.ingest.sentry.io/6331069',
beforeSend: (event: Event) => {
if (allowedEvents === 0) {
return null
}

allowedEvents--

return {
...event,
exception: { values: getSentryExceptions(event) },
user: { ...event.user, ip_address: undefined }, // remove IP address
tags: { ...event.tags, 'frame.instance_id': store('main.instanceId') },
extra: getCrashReportFields()
}
}
})
}
25 changes: 3 additions & 22 deletions main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,39 +54,20 @@ const accounts = require('./accounts').default

const launch = require('./launch')
const updater = require('./updater')
const sentry = require('./sentry')
const errors = require('./errors')

require('./rpc')
sentry.init()
errors.init()

// const clients = require('./clients')
const signers = require('./signers').default
const persist = require('./store/persist')
const { default: showUnhandledExceptionDialog } = require('./windows/dialog/unhandledException')

log.info('Chrome: v' + process.versions.chrome)
log.info('Electron: v' + process.versions.electron)
log.info('Node: v' + process.versions.node)

// prevent showing the exit dialog more than once
let closing = false

process.on('uncaughtException', e => {
log.error('uncaughtException', e)

Sentry.captureException(e)

if (e.code === 'EPIPE') {
log.error('uncaught EPIPE error', e)
return
}

if (!closing) {
closing = true

showUnhandledExceptionDialog(e.message, e.code)
}
})
process.on('uncaughtException', errors.uncaughtExceptionHandler)

const externalWhitelist = [
'https://frame.sh',
Expand Down
43 changes: 0 additions & 43 deletions main/sentry/index.ts

This file was deleted.

63 changes: 62 additions & 1 deletion test/main/sentry/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,33 @@ import { init as initSentry } from '../../../main/sentry'
jest.mock('@sentry/electron', () => ({ init: jest.fn(), IPCMode: { Classic: 'test-ipcmode' } }))
jest.mock('../../../main/store')

beforeAll(() => {
jest.useFakeTimers()
})

afterAll(() => {
jest.useRealTimers()
})

describe('sentry', () => {
const mockEvent = (event) => Sentry.init.mock.calls[0][0].beforeSend(event)
const mockEvents = (events) => events.map(mockEvent)
const validEvent = {
exception: {
values: [],
},
extra: {
networks: '{}',
networksMeta: '{}',
tokens: '{}',
},
tags: {
"frame.instance_id": undefined,
},
user: {
ip_address: undefined,
}
}

it('should initialize sentry with the expected object', () => {
initSentry()
Expand All @@ -17,7 +42,7 @@ describe('sentry', () => {
})

it('should strip asar paths from stackframe modules', () => {
initSentry();
initSentry()
const sentryEvent = mockEvent({
exception: {
values: [{
Expand All @@ -42,4 +67,40 @@ describe('sentry', () => {
"{asar}/compiled/main/signers/lattice/Lattice/index"
])
})

it('should drop events once the rate limit has been reached', () => {
initSentry()
const sentEvents = mockEvents(Array(10).fill({}))

expect(sentEvents.slice(0, 5)).toStrictEqual(
Array(5).fill(validEvent)
)
expect(sentEvents.slice(5)).toStrictEqual([null, null, null, null, null])
goosewobbler marked this conversation as resolved.
Show resolved Hide resolved
})

it('should send events after the rate limit recovery period has elapsed', () => {
const events = Array(5).fill({})
initSentry()
mockEvents(events)
jest.advanceTimersByTime(60_000)
expect(mockEvents(events)).toStrictEqual(
[validEvent, null, null, null, null]
)
jest.advanceTimersByTime(2 * 60_000)
expect(mockEvents(events)).toStrictEqual(
goosewobbler marked this conversation as resolved.
Show resolved Hide resolved
[validEvent, validEvent, null, null, null]
)
jest.advanceTimersByTime(3 * 60_000)
expect(mockEvents(events)).toStrictEqual(
[validEvent, validEvent, validEvent, null, null]
)
jest.advanceTimersByTime(4 * 60_000)
expect(mockEvents(events)).toStrictEqual(
[validEvent, validEvent, validEvent, validEvent, null]
)
jest.advanceTimersByTime(100 * 60_000)
expect(mockEvents(events)).toStrictEqual(
[validEvent, validEvent, validEvent, validEvent, validEvent]
)
})
})