Skip to content

Commit

Permalink
Merge pull request #2 from cbsi-cmg/jmkoenig/CNETDEV-37718
Browse files Browse the repository at this point in the history
CNETDEV-37718 migrate Sentry JavaScript SDK from 7.x to 8.x
  • Loading branch information
jmkoenig authored Jul 8, 2024
2 parents cd2452e + be2780c commit 25fd6fa
Show file tree
Hide file tree
Showing 27 changed files with 62,538 additions and 379 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ node_modules
**/.yarn/*state*

# Generated dirs
dist
# dist

# Nuxt
.nuxt
Expand Down
5 changes: 5 additions & 0 deletions dist/module.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = function(...args) {
return import('./module.mjs').then(m => m.default.call(this, ...args))
}
const _meta = module.exports.meta = require('./module.json')
module.exports.getMeta = () => Promise.resolve(_meta)
2 changes: 2 additions & 0 deletions dist/module.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "/Users/jkoenig/sentry-module/src/module";
export { default } from "/Users/jkoenig/sentry-module/src/module";
5 changes: 5 additions & 0 deletions dist/module.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@nuxtjs/sentry",
"configKey": "sentry",
"version": "8.0.7"
}
12 changes: 12 additions & 0 deletions dist/module.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import jiti from "file:///Users/jkoenig/sentry-module/node_modules/jiti/lib/index.js";

/** @type {import("/Users/jkoenig/sentry-module/src/module")} */
const _module = jiti(null, {
"esmResolve": true,
"interopDefault": true,
"alias": {
"@nuxtjs/sentry": "/Users/jkoenig/sentry-module"
}
})("/Users/jkoenig/sentry-module/src/module.ts");

export default _module;
1 change: 1 addition & 0 deletions dist/runtime
1 change: 1 addition & 0 deletions dist/templates
25 changes: 25 additions & 0 deletions dist/types.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

import type { ModuleOptions, ModuleHooks, RuntimeModuleHooks, ModuleRuntimeHooks, ModuleRuntimeConfig, ModulePublicRuntimeConfig } from './module.js'

declare module '#app' {
interface RuntimeNuxtHooks extends RuntimeModuleHooks, ModuleRuntimeHooks {}
}

declare module '@nuxt/schema' {
interface NuxtConfig { ['sentry']?: Partial<ModuleOptions> }
interface NuxtOptions { ['sentry']?: ModuleOptions }
interface NuxtHooks extends ModuleHooks {}
interface RuntimeConfig extends ModuleRuntimeConfig {}
interface PublicRuntimeConfig extends ModulePublicRuntimeConfig {}
}

declare module 'nuxt/schema' {
interface NuxtConfig { ['sentry']?: Partial<ModuleOptions> }
interface NuxtOptions { ['sentry']?: ModuleOptions }
interface NuxtHooks extends ModuleHooks {}
interface RuntimeConfig extends ModuleRuntimeConfig {}
interface PublicRuntimeConfig extends ModulePublicRuntimeConfig {}
}


export type { default } from './module.js'
25 changes: 25 additions & 0 deletions dist/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

import type { ModuleOptions, ModuleHooks, RuntimeModuleHooks, ModuleRuntimeHooks, ModuleRuntimeConfig, ModulePublicRuntimeConfig } from './module'

declare module '#app' {
interface RuntimeNuxtHooks extends RuntimeModuleHooks, ModuleRuntimeHooks {}
}

declare module '@nuxt/schema' {
interface NuxtConfig { ['sentry']?: Partial<ModuleOptions> }
interface NuxtOptions { ['sentry']?: ModuleOptions }
interface NuxtHooks extends ModuleHooks {}
interface RuntimeConfig extends ModuleRuntimeConfig {}
interface PublicRuntimeConfig extends ModulePublicRuntimeConfig {}
}

declare module 'nuxt/schema' {
interface NuxtConfig { ['sentry']?: Partial<ModuleOptions> }
interface NuxtOptions { ['sentry']?: ModuleOptions }
interface NuxtHooks extends ModuleHooks {}
interface RuntimeConfig extends ModuleRuntimeConfig {}
interface PublicRuntimeConfig extends ModulePublicRuntimeConfig {}
}


export type { default } from './module'
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,10 @@
}
},
"dependencies": {
"@sentry/core": "^7.100.0",
"@sentry/integrations": "^7.100.0",
"@sentry/node": "^7.100.0",
"@sentry/utils": "^7.100.0",
"@sentry/vue": "^7.100.0",
"@sentry/core": "^8.13.0",
"@sentry/node": "^8.13.0",
"@sentry/utils": "^8.13.0",
"@sentry/vue": "^8.13.0",
"consola": "^3.2.3",
"defu": "^6.1.4",
"hash-sum": "^2.0.0",
Expand All @@ -79,8 +78,8 @@
"@nuxtjs/eslint-config-typescript": "12.1.0",
"@nuxtjs/module-test-utils": "1.6.3",
"@release-it/conventional-changelog": "8.0.1",
"@sentry/profiling-node": "7.100.0",
"@sentry/webpack-plugin": "2.14.0",
"@sentry/profiling-node": "^8.13.0",
"@sentry/webpack-plugin": "^2.20.1",
"@size-limit/file": "11.0.2",
"@types/hash-sum": "1.0.2",
"@types/lodash.mergewith": "4.6.9",
Expand Down
12 changes: 4 additions & 8 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import type { Nuxt } from './kit-shim'
import type { ModuleConfiguration } from './types/configuration'
import { clientSentryEnabled, serverSentryEnabled, canInitialize } from './utils'
import { resolveRelease, ResolvedClientOptions, resolveClientOptions, ResolvedServerOptions, resolveServerOptions } from './options'
import type { SentryHandlerProxy } from './options'

const RESOLVED_RELEASE_FILENAME = 'sentry.release.config.mjs'

Expand Down Expand Up @@ -83,7 +82,7 @@ export async function webpackConfigHook (nuxt: Nuxt, webpackConfigs: WebpackConf
if (!publishRelease.release) {
publishRelease.release = {}
}
publishRelease.release.name = publishRelease.release.name || options.config.release || await resolveRelease(options)
publishRelease.release.name = publishRelease.release.name || options.config.release || (await resolveRelease(options))
if (!publishRelease.release.name) {
// We've already tried to determine "release" manually using Sentry CLI so to avoid webpack plugin crashing, we'll just bail here.
logger.warn('Sentry release will not be published because "config.release" or "publishRelease.release.name" was not set nor it ' +
Expand All @@ -98,7 +97,7 @@ export async function webpackConfigHook (nuxt: Nuxt, webpackConfigs: WebpackConf
}
}

export async function initializeServerSentry (nuxt: Nuxt, moduleOptions: ModuleConfiguration, sentryHandlerProxy: SentryHandlerProxy, logger: ConsolaInstance): Promise<void> {
export async function initializeServerSentry (nuxt: Nuxt, moduleOptions: ModuleConfiguration, logger: ConsolaInstance): Promise<void> {
if (process.sentry) {
return
}
Expand All @@ -118,11 +117,8 @@ export async function initializeServerSentry (nuxt: Nuxt, moduleOptions: ModuleC

if (canInitialize(moduleOptions)) {
Sentry.init(config)
sentryHandlerProxy.errorHandler = Sentry.Handlers.errorHandler()
sentryHandlerProxy.requestHandler = Sentry.Handlers.requestHandler(moduleOptions.requestHandlerConfig)
if (serverOptions.tracing) {
sentryHandlerProxy.tracingHandler = Sentry.Handlers.tracingHandler()
}
// https://docs.sentry.io/platforms/javascript/guides/express/migration/v7-to-v8/#updated-sdk-initialization
nuxt.hook('render:setupMiddleware', (app: any) => app.use(() => Sentry.setupExpressErrorHandler(app)))
}
}

Expand Down
23 changes: 1 addition & 22 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type { Configuration as WebpackConfig } from 'webpack'
import { defineNuxtModule, isNuxt2, useLogger } from './kit-shim'
import { envToBool, boolToText, callOnce, canInitialize, clientSentryEnabled, serverSentryEnabled } from './utils'
import { buildHook, initializeServerSentry, shutdownServerSentry, webpackConfigHook } from './hooks'
import type { SentryHandlerProxy } from './options'
import type { ModuleConfiguration, ModuleOptions, ModulePublicRuntimeConfig } from './types'

export type { ModuleOptions, ModulePublicRuntimeConfig }
Expand Down Expand Up @@ -51,7 +50,6 @@ export default defineNuxtModule<ModuleConfiguration>({
},
serverConfig: {},
clientConfig: {},
requestHandlerConfig: {},
}),
async setup (options, nuxt) {
const defaultsPublishRelease: SentryWebpackPluginOptions = {
Expand Down Expand Up @@ -88,7 +86,6 @@ export default defineNuxtModule<ModuleConfiguration>({
'lodash.mergewith',
'@sentry/browser',
'@sentry/core',
'@sentry/integrations',
'@sentry/utils',
'@sentry/vue',
]
Expand All @@ -97,24 +94,6 @@ export default defineNuxtModule<ModuleConfiguration>({
}

if (serverSentryEnabled(options)) {
/**
* Proxy that provides a dummy request handler before Sentry is initialized and gets replaced with Sentry's own
* handler after initialization. Otherwise server-side request tracing would not work as it depends on Sentry being
* initialized already during handler creation.
*/
const sentryHandlerProxy: SentryHandlerProxy = {
errorHandler: (error, _, __, next) => { next(error) },
requestHandler: (_, __, next) => { next() },
tracingHandler: (_, __, next) => { next() },
}
// @ts-expect-error Nuxt 2 only hook
nuxt.hook('render:setupMiddleware', app => app.use((req, res, next) => { sentryHandlerProxy.requestHandler(req, res, next) }))
if (options.tracing) {
// @ts-expect-error Nuxt 2 only hook
nuxt.hook('render:setupMiddleware', app => app.use((req, res, next) => { sentryHandlerProxy.tracingHandler(req, res, next) }))
}
// @ts-expect-error Nuxt 2 only hook
nuxt.hook('render:errorMiddleware', app => app.use((error, req, res, next) => { sentryHandlerProxy.errorHandler(error, req, res, next) }))
// @ts-expect-error Nuxt 2 only hook
nuxt.hook('generate:routeFailed', ({ route, errors }) => {
type routeGeneretorError = {
Expand All @@ -139,7 +118,7 @@ export default defineNuxtModule<ModuleConfiguration>({
if (isNuxt2()) {
const isBuilding = nuxt.options._build && !nuxt.options.dev
const initHook = isBuilding ? 'build:compile' : 'ready'
nuxt.hook(initHook, () => initializeServerSentry(nuxt, options, sentryHandlerProxy, logger))
nuxt.hook(initHook, () => initializeServerSentry(nuxt, options, logger))
const shutdownHook = isBuilding ? 'build:done' : 'close'
const shutdownServerSentryOnce = callOnce(() => shutdownServerSentry())
nuxt.hook(shutdownHook, shutdownServerSentryOnce)
Expand Down
Loading

0 comments on commit 25fd6fa

Please sign in to comment.