Skip to content

Commit

Permalink
dev: Improve Sentry reporting (#2247)
Browse files Browse the repository at this point in the history
With the new v3.x major version of Sentry, we can easily improve our
reporting via our client setup.

We will now include the Sentry preload script in our persisting
session partition which could help capture request errors.
We also add 2 integrations:
- ElectronMinidump which should offer better crash dumps than the
  default SentryMinidump integration (especially since we've uploaded
  the Electron debug symbols corresponding to the version we're using)
- ExtraErrorData whichi will extract all custom attributes from our
  errors and attach them as `extra` info. For this reason, we will
  stop attaching log messages' attributes via `setExtra` and use a
  scope context instead
  • Loading branch information
taratatach authored Jul 7, 2022
2 parents 8cda028 + 7417b5f commit c313952
Show file tree
Hide file tree
Showing 6 changed files with 380 additions and 148 deletions.
3 changes: 0 additions & 3 deletions core/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const SyncState = require('./syncstate')
const Registration = require('./remote/registration')
const logger = require('./utils/logger')
const { LOG_FILE, LOG_FILENAME } = logger
const sentry = require('./utils/sentry')
const { sendToTrash } = require('./utils/fs')
const notes = require('./utils/notes')
const web = require('./utils/web')
Expand Down Expand Up @@ -356,8 +355,6 @@ class App {
const clientInfo = this.clientInfo()
log.info(clientInfo, 'user config')

sentry.setup(clientInfo)

if (!this.config.isValid()) {
throw new config.InvalidConfigError()
}
Expand Down
44 changes: 39 additions & 5 deletions core/utils/sentry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,34 @@
*
* @module core/utils/sentry
* @flow
*
* Setup our Sentry integration to send errors and crash reports to our Sentry
* server.
*
* Follow these steps to upload Electron debug symbols after each version
* upgrade:
*
* 1. `sentry-wizard --skip-connect -u <server url> --integration electron
* 2. Follow the steps (auth tokens can be generated here: https://<server url>/settings/account/api/auth-tokens/)
* 3. `node sentry-symbols.js`
*/

const { session } = require('electron')
const Sentry = require('@sentry/electron')
const {
ExtraErrorData: ExtraErrorDataIntegration
} = require('@sentry/integrations')
const bunyan = require('bunyan')
const url = require('url')
const _ = require('lodash')

const { SESSION_PARTITION_NAME } = require('../../gui/js/proxy')
const logger = require('./logger')

const log = logger({
component: 'Sentry'
})

const _ = require('lodash')

module.exports = {
setup,
flag,
Expand Down Expand Up @@ -57,7 +72,27 @@ function setup(clientInfos /*: ClientInfo */) {
Sentry.init({
dsn: SENTRY_DSN,
release: appVersion,
environment
environment,
// Inject preload script into all used sessions
getSessions: () => [
session.defaultSession,
session.fromPartition(SESSION_PARTITION_NAME)
],
// Adding the ElectronMinidump integration like this
// ensures that it is the first integrations to be initialized.
integrations: defaultIntegrations => {
return [
// Uploads minidumps via Crashpad/Breakpad built in uploader with
// partial context when reporting native crash.
new Sentry.Integrations.ElectronMinidump(),
// Extract all non-native attributes up to <depth> from Error objects
// and attach them to events as extra data.
// If the error object has a .toJSON() method, it will be run to
// extract the additional data.
new ExtraErrorDataIntegration({ depth: 10 }),
...defaultIntegrations
]
}
})
Sentry.configureScope(scope => {
scope.setUser({ username: instance })
Expand Down Expand Up @@ -115,10 +150,9 @@ const handleBunyanMessage = msg => {

Sentry.withScope(scope => {
scope.setLevel(level)
scope.setExtras(extra)
scope.setContext('msgDetails', extra)

if (msg.err) {
if (msg.err.reason) scope.setExtra('reason', msg.err.reason)
Sentry.captureException(format(msg.err))
} else {
Sentry.captureMessage(msg.msg)
Expand Down
3 changes: 2 additions & 1 deletion gui/js/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const http = require('http')
const https = require('https')
const yargs = require('yargs')

const log = require('../../core/app').logger({
const logger = require('../../core/utils/logger')
const log = logger({
component: 'GUI:proxy'
})

Expand Down
29 changes: 12 additions & 17 deletions gui/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@

// Initialize `remote` module so that renderer processes can use it.
require('@electron/remote/main').initialize()
const { app, Menu, Notification, ipcMain, dialog } = require('electron')
const {
app,
Menu,
Notification,
ipcMain,
dialog,
session
} = require('electron')

const Desktop = require('../core/app.js')
const sentry = require('../core/utils/sentry')
const { openNote } = require('./utils/notes')
const pkg = require('../package.json')

Expand Down Expand Up @@ -57,7 +65,9 @@ if (!mainInstance && !process.env.COZY_DESKTOP_PROPERTY_BASED_TESTING) {
app.exit()
}

let desktop
let desktop = new Desktop.App(process.env.COZY_DESKTOP_DIR)
sentry.setup(desktop.clientInfo())

let diskTimeout = null
let onboardingWindow = null
let helpWindow = null
Expand Down Expand Up @@ -565,26 +575,11 @@ app.on('ready', async () => {
app.dock.hide()
}

const { session } = require('electron')

const hostID = (dumbhash(os.hostname()) % 4096).toString(16)
let userAgent = `Cozy-Desktop-${process.platform}-${pkg.version}-${hostID}`
await proxy.setup(app, proxy.config(), session, userAgent)
log.info('Loading CLI...')
i18n.init(app)
try {
desktop = new Desktop.App(process.env.COZY_DESKTOP_DIR)
} catch (err) {
if (err.message.match(/GLIBCXX/)) {
await dialog.showMessageBox(null, {
type: 'error',
message: translate('Error Bad GLIBCXX version'),
buttons: [translate('AppMenu Close')]
})
await exit(0)
return
} else throw err
}

const { argv } = process

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@
"@babel/runtime": "7.16.7",
"@electron/remote": "2.0.8",
"@parcel/watcher": "https://github.com/taratatach/parcel-watcher.git#v3.0.0",
"@sentry/electron": "2.5.4",
"@sentry/electron": "3.0.7",
"@sentry/integrations": "7.5.1",
"async": "3.2.3",
"auto-bind": "4.0.0",
"auto-launch": "^5.0.3",
Expand Down Expand Up @@ -126,6 +127,7 @@
},
"devDependencies": {
"@babel/core": "7.16.7",
"@sentry/cli": "^2.3.0",
"babel-preset-cozy-app": "^2.0.1",
"chai": "4.3.4",
"chai-like": "1.1.1",
Expand All @@ -139,6 +141,7 @@
"devtron": "^1.4.0",
"electron": "19.0.0",
"electron-builder": "^22.8.1",
"electron-download": "^4.1.1",
"electron-mocha": "11.0.2",
"electron-notarize": "^0.1.1",
"elm": "^0.19.1",
Expand Down
Loading

0 comments on commit c313952

Please sign in to comment.