From da63340658f0b4850f7b436af2b015e735573678 Mon Sep 17 00:00:00 2001 From: Dmitry Molotkov Date: Fri, 19 Oct 2018 22:47:34 +0300 Subject: [PATCH] feat: use new sentry sdk, close #20, #30 --- README.md | 13 +-- lib/sentry.js | 40 ++++----- lib/templates/sentry-client.js | 22 ++--- package.json | 6 +- test/fixture/nuxt.config.js | 1 - yarn.lock | 154 +++++++++++++++++++++++++++------ 6 files changed, 162 insertions(+), 74 deletions(-) diff --git a/README.md b/README.md index 38c946fe..ceb85d39 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,6 @@ The module enables error logging through [Sentry](http://sentry.io). sentry: { public_key: '', - private_key: '', project_id: '', config: { // Additional config @@ -42,7 +41,7 @@ Enter your DSN in the NuxtJS config file. Additional config settings can be foun ### Usage in Vue component -In a Vue component, `Raven` is available as `this.$raven`, so we can call functions like +In a Vue component, `Sentry` is available as `this.$sentry`, so we can call functions like ``` this.$raven.setUserContext({user}) @@ -59,11 +58,6 @@ Normally setting required DSN information would be enough. - Type: `String` - Default: `process.env.SENTRY_DSN` -### public_dsn -- Type: `String` - - Default: `process.env.SENTRY_PUBLIC_DSN` - -If value omitted it will be generated using `dsn` value, by removing private key part. ### public_key - Type: `String` @@ -71,11 +65,6 @@ If value omitted it will be generated using `dsn` value, by removing private key Will be ignored if `dsn` provided. -### private_key -- Type: `String` - - Default: `process.env.SENTRY_PRIVATE_KEY` - -Will be ignored if `dsn` provided. ### host - Type: `String` diff --git a/lib/sentry.js b/lib/sentry.js index 45cf3ac1..4776a42f 100644 --- a/lib/sentry.js +++ b/lib/sentry.js @@ -1,4 +1,4 @@ -const Raven = require('raven') +const Sentry = require('@sentry/node') const path = require('path') const logger = require('consola').withScope('nuxt:sentry') @@ -9,9 +9,7 @@ module.exports = function sentry (moduleOptions) { disabled: process.env.SENTRY_DISABLED || false, disableClientSide: process.env.SENTRY_DISABLE_CLIENT_SIDE === 'true' || false, dsn: process.env.SENTRY_DSN || null, - public_dsn: process.env.SENTRY_PUBLIC_DSN || null, public_key: process.env.SENTRY_PUBLIC_KEY || null, - private_key: process.env.SENTRY_PRIVATE_KEY || null, host: process.env.SENTRY_HOST || 'sentry.io', protocol: process.env.SENTRY_PROTOCOL || 'https', project_id: process.env.SENTRY_PROJECT_ID || '', @@ -28,28 +26,20 @@ module.exports = function sentry (moduleOptions) { } // Don't proceed if no keys are provided - if (!options.dsn && !options.public_key && !options.private_key) { - logger.info('Disabled because no (private) key was found') + if (!options.dsn && !options.public_key) { + logger.info('Disabled because no key was found') return } // Generate DSN // https://docs.sentry.io/quickstart/#about-the-dsn if (!options.dsn || !options.dsn.length) { - if (!options.private_key || !options.private_key.length) { - options.dsn = `${options.protocol}://${options.public_key}@${options.host}${options.path}${options.project_id}` - } else { - options.dsn = `${options.protocol}://${options.public_key}:${options.private_key}@${options.host}${options.path}${options.project_id}` - } - } - - // Public DSN (without private key) - if (!options.public_dsn || !options.public_dsn.length) { - options.public_dsn = options.dsn.replace(/:\w+@/, '@') + options.dsn = `${options.protocol}://${options.public_key}@${options.host}${options.path}${options.project_id}` } - // Setup raven - Raven.config(options.dsn, options.config).install() + options.config.dsn = options.dsn + // Setup sentry + Sentry.init(options.config) // Register the client plugin if (!options.disableClientSide) { @@ -62,20 +52,26 @@ module.exports = function sentry (moduleOptions) { } if (this.nuxt.hook) { - this.nuxt.hook('render:setupMiddleware', app => app.use(Raven.requestHandler())) - this.nuxt.hook('render:errorMiddleware', app => app.use(Raven.errorHandler())) + this.nuxt.hook('render:setupMiddleware', app => app.use(Sentry.Handlers.requestHandler())) + this.nuxt.hook('render:errorMiddleware', app => app.use(Sentry.Handlers.errorHandler())) this.nuxt.hook('generate:routeFailed', ({ route, errors }) => { - errors.forEach(({ error }) => Raven.captureException(error, { extra: { route } })) + errors.forEach(({ error }) => Sentry.withScope(scope => { + scope.setExtra('route', route) + Sentry.captureException(error) + })) }) } else { this.nuxt.plugin('renderer', renderer => { - renderer.app.use(Raven.requestHandler()) + renderer.app.use(Sentry.Handlers.requestHandler()) // Grab Nuxt's original error middleware and overwrite it with our own const nuxtErrorMiddleware = renderer.errorMiddleware renderer.errorMiddleware = (err, req, res, next) => { // Log the error - res.sentry = Raven.captureException(err, { req }) + Sentry.withScope(scope => { + scope.setExtra('req', req) + Sentry.captureException(err) + }) // Call Nuxt's original error middleware nuxtErrorMiddleware.call(renderer, err, req, res, next) diff --git a/lib/templates/sentry-client.js b/lib/templates/sentry-client.js index 2709029a..1d860ce6 100644 --- a/lib/templates/sentry-client.js +++ b/lib/templates/sentry-client.js @@ -1,14 +1,16 @@ import Vue from 'vue' -import Raven from 'raven-js' -import RavenVue from 'raven-js/plugins/vue' +import * as Sentry from '@sentry/browser' export default function (ctx, inject) { - // Inject Raven to the context as $raven - ctx.$raven = Raven - inject('raven', Raven) - - Raven - .config('<%= options.public_dsn %>', <%= serialize(options.config) %>) - .addPlugin(RavenVue, Vue) - .install() + const opts = Object.assign({}, <%= serialize(options.config) %>, { + integrations: (integrations) => { + integrations.push(new Sentry.Integrations.Vue({ Vue })) + return integrations + } + }) + Sentry.init(opts) + + // Inject Sentry to the context as $sentry + ctx.$sentry = Sentry + inject('sentry', Sentry) } diff --git a/package.json b/package.json index e1663a59..c6016191 100755 --- a/package.json +++ b/package.json @@ -34,9 +34,9 @@ ] }, "dependencies": { - "consola": "^1.4.1", - "raven": "^2.3.0", - "raven-js": "^3.21.0" + "consola": "^1.4.3", + "@sentry/node": "^4.0.6", + "@sentry/browser": "^4.0.6" }, "devDependencies": { "nuxt-module-builder": "latest" diff --git a/test/fixture/nuxt.config.js b/test/fixture/nuxt.config.js index db2ad0c7..5aaf0f7a 100644 --- a/test/fixture/nuxt.config.js +++ b/test/fixture/nuxt.config.js @@ -7,7 +7,6 @@ module.exports = { modules: [ ['@@', { public_key: 'public_key', - private_key: 'private_key', project_id: 'project_id', config: {} }] diff --git a/yarn.lock b/yarn.lock index 06863193..693612df 100644 --- a/yarn.lock +++ b/yarn.lock @@ -10,6 +10,61 @@ mustache "^2.3.0" stack-trace "0.0.10" +"@sentry/browser@^4.0.6": + version "4.0.6" + resolved "https://registry.yarnpkg.com/@sentry/browser/-/browser-4.0.6.tgz#6a9183371c9d6c2c1905f14af17ad68e305ce918" + dependencies: + "@sentry/core" "4.0.6" + "@sentry/types" "4.0.6" + "@sentry/utils" "4.0.6" + md5 "2.2.1" + +"@sentry/core@4.0.6": + version "4.0.6" + resolved "https://registry.yarnpkg.com/@sentry/core/-/core-4.0.6.tgz#8e59445dde27e639ffcac23b4e40529cda4c9df7" + dependencies: + "@sentry/hub" "4.0.6" + "@sentry/minimal" "4.0.6" + "@sentry/types" "4.0.6" + "@sentry/utils" "4.0.6" + +"@sentry/hub@4.0.6": + version "4.0.6" + resolved "https://registry.yarnpkg.com/@sentry/hub/-/hub-4.0.6.tgz#ab86bd50d12234a0699857452e925069311f54d1" + dependencies: + "@sentry/types" "4.0.6" + "@sentry/utils" "4.0.6" + +"@sentry/minimal@4.0.6": + version "4.0.6" + resolved "https://registry.yarnpkg.com/@sentry/minimal/-/minimal-4.0.6.tgz#494d725f2d7c343724c47bbb0cd99302b6124e7d" + dependencies: + "@sentry/hub" "4.0.6" + "@sentry/types" "4.0.6" + +"@sentry/node@^4.0.6": + version "4.0.6" + resolved "https://registry.yarnpkg.com/@sentry/node/-/node-4.0.6.tgz#8a3b9ab837f187a1bd3c490235c88f8ca18e3c7f" + dependencies: + "@sentry/core" "4.0.6" + "@sentry/hub" "4.0.6" + "@sentry/types" "4.0.6" + "@sentry/utils" "4.0.6" + cookie "0.3.1" + lsmod "1.0.0" + md5 "2.2.1" + stack-trace "0.0.10" + +"@sentry/types@4.0.6": + version "4.0.6" + resolved "https://registry.yarnpkg.com/@sentry/types/-/types-4.0.6.tgz#421478df17415802baa4d5e2d3db246e559a0fad" + +"@sentry/utils@4.0.6": + version "4.0.6" + resolved "https://registry.yarnpkg.com/@sentry/utils/-/utils-4.0.6.tgz#77ca73fb7d2c436914d2dd84fa975dd5b618246d" + dependencies: + "@sentry/types" "4.0.6" + "@types/node@^6.0.46": version "6.0.89" resolved "https://registry.yarnpkg.com/@types/node/-/node-6.0.89.tgz#154be0e6a823760cd6083aa8c48f952e2e63e0b0" @@ -150,6 +205,12 @@ ansi-styles@^3.0.0, ansi-styles@^3.1.0: dependencies: color-convert "^1.9.0" +ansi-styles@^3.2.1: + version "3.2.1" + resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-3.2.1.tgz#41fbb20243e50b12be0f04b8dedbf07520ce841d" + dependencies: + color-convert "^1.9.0" + anymatch@^1.3.0: version "1.3.2" resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-1.3.2.tgz#553dcb8f91e3c889845dfdba34c77721b90b9d7a" @@ -1264,6 +1325,18 @@ chalk@^2.0.0, chalk@^2.0.1, chalk@^2.1.0: escape-string-regexp "^1.0.5" supports-color "^4.0.0" +chalk@^2.3.2: + version "2.4.1" + resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.1.tgz#18c49ab16a037b6eb0152cc83e3471338215b66e" + dependencies: + ansi-styles "^3.2.1" + escape-string-regexp "^1.0.5" + supports-color "^5.3.0" + +charenc@~0.0.1: + version "0.0.2" + resolved "https://registry.yarnpkg.com/charenc/-/charenc-0.0.2.tgz#c0a1d2f3a7092e03774bfa83f14c0fc5790a8667" + chokidar@^1.7.0: version "1.7.0" resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-1.7.0.tgz#798e689778151c8076b4b360e5edd28cda2bb468" @@ -1283,6 +1356,10 @@ ci-info@^1.0.0: version "1.1.1" resolved "https://registry.yarnpkg.com/ci-info/-/ci-info-1.1.1.tgz#47b44df118c48d2597b56d342e7e25791060171a" +ci-info@^1.5.0: + version "1.6.0" + resolved "https://registry.yarnpkg.com/ci-info/-/ci-info-1.6.0.tgz#2ca20dbb9ceb32d4524a683303313f0304b1e497" + cipher-base@^1.0.0, cipher-base@^1.0.1, cipher-base@^1.0.3: version "1.0.4" resolved "https://registry.yarnpkg.com/cipher-base/-/cipher-base-1.0.4.tgz#8760e4ecc272f4c363532f926d874aae2c1397de" @@ -1480,6 +1557,15 @@ connect@^3.6.3: parseurl "~1.3.2" utils-merge "1.0.1" +consola@^1.4.3: + version "1.4.3" + resolved "https://registry.yarnpkg.com/consola/-/consola-1.4.3.tgz#945e967e05430ddabd3608b37f5fa37fcfacd9dd" + dependencies: + chalk "^2.3.2" + figures "^2.0.0" + lodash "^4.17.5" + std-env "^1.1.0" + console-browserify@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/console-browserify/-/console-browserify-1.1.0.tgz#f0241c45730a9fc6323b206dbf38edc741d0bb10" @@ -1722,6 +1808,10 @@ cross-spawn@^5.0.1, cross-spawn@^5.1.0: shebang-command "^1.2.0" which "^1.2.9" +crypt@~0.0.1: + version "0.0.2" + resolved "https://registry.yarnpkg.com/crypt/-/crypt-0.0.2.tgz#88d7ff7ec0dfb86f713dc87bbb42d044d3e6c41b" + cryptiles@2.x.x: version "2.0.5" resolved "https://registry.yarnpkg.com/cryptiles/-/cryptiles-2.0.5.tgz#3bdfecdc608147c1c67202fa291e7dca59eaa3b8" @@ -3023,6 +3113,10 @@ has-flag@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-2.0.0.tgz#e8207af1cc7b30d446cc70b734b5e8be18f88d51" +has-flag@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-3.0.0.tgz#b5d454dc2199ae225699f3467e5a07f3b955bafd" + has-unicode@^2.0.0: version "2.0.1" resolved "https://registry.yarnpkg.com/has-unicode/-/has-unicode-2.0.1.tgz#e0e6fe6a28cf51138855e086d1691e771de2a8b9" @@ -3341,6 +3435,10 @@ is-buffer@^1.1.5: version "1.1.5" resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.5.tgz#1f3b26ef613b214b88cbca23cc6c01d87961eecc" +is-buffer@~1.1.1: + version "1.1.6" + resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be" + is-builtin-module@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/is-builtin-module/-/is-builtin-module-1.0.0.tgz#540572d34f7ac3119f8f76c30cbc1b1e037affbe" @@ -3357,6 +3455,12 @@ is-ci@^1.0.10: dependencies: ci-info "^1.0.0" +is-ci@^1.1.0: + version "1.2.1" + resolved "https://registry.yarnpkg.com/is-ci/-/is-ci-1.2.1.tgz#e3779c8ee17fccf428488f6e281187f2e632841c" + dependencies: + ci-info "^1.5.0" + is-date-object@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/is-date-object/-/is-date-object-1.0.1.tgz#9aa20eb6aeebbff77fbd33e74ca01b33581d3a16" @@ -4188,6 +4292,10 @@ lodash@^4.0.0, lodash@^4.13.1, lodash@^4.14.0, lodash@^4.17.3, lodash@^4.17.4, l version "4.17.4" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae" +lodash@^4.17.5: + version "4.17.11" + resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d" + longest@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/longest/-/longest-1.0.1.tgz#30a0b2da38f73770e8294a0d22e6625ed77d0097" @@ -4261,6 +4369,14 @@ md5.js@^1.3.4: hash-base "^3.0.0" inherits "^2.0.1" +md5@2.2.1: + version "2.2.1" + resolved "https://registry.yarnpkg.com/md5/-/md5-2.2.1.tgz#53ab38d5fe3c8891ba465329ea23fac0540126f9" + dependencies: + charenc "~0.0.1" + crypt "~0.0.1" + is-buffer "~1.1.1" + media-typer@0.3.0: version "0.3.0" resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748" @@ -5678,20 +5794,6 @@ range-parser@^1.0.3, range-parser@~1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/range-parser/-/range-parser-1.2.0.tgz#f49be6b487894ddc40dcc94a322f611092e00d5e" -raven-js@^3.18.1: - version "3.18.1" - resolved "https://registry.yarnpkg.com/raven-js/-/raven-js-3.18.1.tgz#746339c08fca75c3f402d7e6b633687c3ffc6ee8" - -raven@^2.2.1: - version "2.2.1" - resolved "https://registry.yarnpkg.com/raven/-/raven-2.2.1.tgz#57c7fbe68a80147ec527def3d7c01575cf948fe3" - dependencies: - cookie "0.3.1" - lsmod "1.0.0" - stack-trace "0.0.9" - timed-out "4.0.1" - uuid "3.0.0" - raw-body@2.3.2: version "2.3.2" resolved "https://registry.yarnpkg.com/raw-body/-/raw-body-2.3.2.tgz#bcd60c77d3eb93cde0050295c3f379389bc88f89" @@ -6384,10 +6486,6 @@ stack-trace@0.0.10: version "0.0.10" resolved "https://registry.yarnpkg.com/stack-trace/-/stack-trace-0.0.10.tgz#547c70b347e8d32b4e108ea1a2a159e5fdde19c0" -stack-trace@0.0.9: - version "0.0.9" - resolved "https://registry.yarnpkg.com/stack-trace/-/stack-trace-0.0.9.tgz#a8f6eaeca90674c333e7c43953f275b451510695" - stackframe@^1.0.3: version "1.0.4" resolved "https://registry.yarnpkg.com/stackframe/-/stackframe-1.0.4.tgz#357b24a992f9427cba6b545d96a14ed2cbca187b" @@ -6408,6 +6506,12 @@ standard-version@^4.2.0: version "1.3.1" resolved "https://registry.yarnpkg.com/statuses/-/statuses-1.3.1.tgz#faf51b9eb74aaef3b3acf4ad5f61abf24cb7b93e" +std-env@^1.1.0: + version "1.3.1" + resolved "https://registry.yarnpkg.com/std-env/-/std-env-1.3.1.tgz#4e1758412439e9ece1d437b1b098551911aa44ee" + dependencies: + is-ci "^1.1.0" + stealthy-require@^1.1.0: version "1.1.1" resolved "https://registry.yarnpkg.com/stealthy-require/-/stealthy-require-1.1.1.tgz#35b09875b4ff49f26a777e509b3090a3226bf24b" @@ -6524,6 +6628,12 @@ supports-color@^4.0.0, supports-color@^4.2.1, supports-color@^4.4.0: dependencies: has-flag "^2.0.0" +supports-color@^5.3.0: + version "5.5.0" + resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.5.0.tgz#e2e69a44ac8772f78a1ec0b35b689df6530efc8f" + dependencies: + has-flag "^3.0.0" + svg-tags@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/svg-tags/-/svg-tags-1.0.0.tgz#58f71cee3bd519b59d4b2a843b6c7de64ac04764" @@ -6630,10 +6740,6 @@ time-stamp@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/time-stamp/-/time-stamp-2.0.0.tgz#95c6a44530e15ba8d6f4a3ecb8c3a3fac46da357" -timed-out@4.0.1: - version "4.0.1" - resolved "https://registry.yarnpkg.com/timed-out/-/timed-out-4.0.1.tgz#f32eacac5a175bea25d7fab565ab3ed8741ef56f" - timers-browserify@^2.0.2: version "2.0.4" resolved "https://registry.yarnpkg.com/timers-browserify/-/timers-browserify-2.0.4.tgz#96ca53f4b794a5e7c0e1bd7cc88a372298fa01e6" @@ -6846,10 +6952,6 @@ utils-merge@1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713" -uuid@3.0.0: - version "3.0.0" - resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.0.0.tgz#6728fc0459c450d796a99c31837569bdf672d728" - uuid@^3.0.0, uuid@^3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.1.0.tgz#3dd3d3e790abc24d7b0d3a034ffababe28ebbc04"