sentry-module
sentry-module copied to clipboard
Plan for Nuxt 3 version
The module's code was refactored in #456 and now largely follows a structure of a Nuxt 3 module (using defineModule and all) but note that it's not using @nuxt/kit but instead a "homemade" shim that has similar (same?) methods. This should make it easy to transition to the real @nuxt/kit for Nuxt 3.
The plan is for the Nuxt 3 version to live on its own branch and have higher major version (we can skip 2 versions or so to give some space for the Nuxt 2 version to introduce breaking changes). Attempts at making a version compatible with both Nuxt 2 and Nuxt 3 were futile so this is how it has to be. Alternatively, a monorepo with both versions sounds like a viable alternative but those would have to not share dependencies so not sure if that would work (I'm not too familiar with the current monorepo solutions).
When implementing the new version, those are some challenges that I foresee:
- [ ] Supporting Nitro and making Sentry available on the server side (in non-webpack/non-vite context). Current version exposes sentry on
process.sentrywhich is a bit of a hack to make Sentry available globally and to avoid reinitializing it on every server side page load. This is to also to allow use of Sentry even outside of specific server routes. If there are better ways to do it in Nuxt 3 then we can try that, even if it would be a breaking change. Important: ensure that Sentry is initialized early in a global server context. It would likely not be enough to do from a middleware and require some request to run first. - [ ] Supporting publishing releases in Vite environment. Currently we are using the official webpack plugin to handle that. We would also need to support Vite. Looks like there is official support ready for Vite now in https://github.com/getsentry/sentry-javascript-bundler-plugins
- [ ] Supporting tracing functionality. Part of the functionality uses connect/express middleware to instrument tracing of routes. Make sure that still works.
- [ ] Adding the Vue composition APIs. Currently lacking.
- [ ] Sentry express/connect middleware handlers (https://docs.sentry.io/platforms/node/guides/express/) have a bunch of logic for detecting transactions, session tracking and more. Not sure if h3 is compatible with those. If not then maybe those have to be adapted for Nuxt 3 version.
I'm open for contributors to tackle this. Since I'm not using Nuxt 3 at the moment, I don't have that much incentive or time to work on it myself.
any updates here guys
The below is what I currently have. It uses @sentry/vite-plugin, which should tick the second box. The below is most definitely not on par wit the current Nuxt 2 module, and the code is absolutely not battle tested. But maybe it can be used as a starting point.
~/nuxt.config.ts:
import { sentryVitePlugin } from '@sentry/vite-plugin'
import { defineNuxtConfig } from 'nuxt/config'
export default defineNuxtConfig({
ssr: false, // I have SSR false
runtimeConfig: {
public: {
ENV: process.env.NODE_ENV ?? 'production',
SENTRY_ENABLED: process.env.NUXT_PUBLIC_SENTRY_ENABLED,
SENTRY_DSN: process.env.NUXT_PUBLIC_SENTRY_DSN,
SENTRY_ENVIRONMENT: process.env.NUXT_PUBLIC_.SENTRY_ENVIRONMENT,
SENTRY_RELEASE: process.env.NUXT_PUBLIC_SENTRY_RELEASE,
SENTRY_TRACE_PROPAGATION_TARGET: process.env.NUXT_PUBLIC_SENTRY_TRACE_PROPAGATION_TARGET,
},
},
sourcemap: {
server: true,
client: true,
},
vite: {
build: {
sourcemap: true,
},
plugins: [
sentryVitePlugin({
authToken: process.env.NUXT_PUBLIC_SENTRY_AUTH_TOKEN,
debug: true,
org: process.env.NUXT_PUBLIC_SENTRY_ORG,
project: process.env.NUXT_PUBLIC_SENTRY_PROJECT,
release: {
name: process.env.NUXT_PUBLIC_SENTRY_RELEASE,
},
sourcemaps: {
assets: ['./.nuxt/dist/client/**'],
},
telemetry: false,
}),
],
},
})
~/plugins/sentry.client.ts:
import {
HttpClient as HttpClientIntegration,
ReportingObserver as ReportingObserverIntegration,
} from '@sentry/integrations'
import type { Breadcrumb, CaptureContext, Primitive, User } from '@sentry/types'
import * as Sentry from '@sentry/vue'
import { withScope } from '@sentry/vue'
import type { Router } from 'vue-router'
import { defineNuxtPlugin } from '#app'
export default defineNuxtPlugin({
parallel: true,
setup: (nuxtApp) => {
if (
typeof window === 'undefined' ||
!['true', true].includes(nuxtApp.$config.public.SENTRY_ENABLED)
) {
return {
provide: {
sentrySetContext: (
_name: string,
_context: {
[key: string]: any
} | null,
) => {},
sentrySetUser: (_user: User | null) => {},
sentrySetTag: (_key: string, _value: Primitive) => {},
sentryAddBreadcrumb: (_breadcrumb: Breadcrumb) => {},
sentryCaptureException: (_exception: any, _captureContext?: CaptureContext) => {},
},
}
}
Sentry.init({
app: nuxtApp.vueApp,
autoSessionTracking: true,
dsn: nuxtApp.$config.public.SENTRY_DSN,
release: nuxtApp.$config.public.SENTRY_RELEASE,
environment: nuxtApp.$config.public.SENTRY_ENVIRONMENT,
integrations: [
new Sentry.BrowserTracing({
routingInstrumentation: Sentry.vueRouterInstrumentation(nuxtApp.$router as Router, {
routeLabel: 'path',
}),
}),
new Sentry.Replay({
networkDetailAllowUrls: [`https//${nuxtApp.$config.public.HOST_NAME}`],
}),
new HttpClientIntegration(),
new ReportingObserverIntegration(),
],
tracePropagationTargets: [nuxtApp.$config.public.SENTRY_TRACE_PROPAGATION_TARGET],
trackComponents: true,
hooks: ['activate', 'create', 'destroy', 'mount', 'update'],
// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production
tracesSampleRate: 0.2,
// Capture Replay for 10% of all sessions,
// plus for 100% of sessions with an error
replaysSessionSampleRate: 0.1,
replaysOnErrorSampleRate: 1,
})
nuxtApp.vueApp.config.errorHandler = (err, context) => {
withScope((scope) => {
scope.setExtra('context', context)
Sentry.captureException(err)
})
}
nuxtApp.hook('app:error', (err) => {
Sentry.captureException(err)
})
return {
provide: {
sentrySetContext: Sentry.setContext,
sentrySetUser: Sentry.setUser,
sentrySetTag: Sentry.setTag,
sentryAddBreadcrumb: Sentry.addBreadcrumb,
sentryCaptureException: Sentry.captureException,
},
}
},
})
any news?
sourcemap are not working with the abobe configuration
Hi, No progress on the subject? we're going to production soon, it's boring :(
Any progress?
I don't understand well
Is the @darthf1 code workings or it's your current not working code ?
@anthony-bernardo I my case @darthf1 code with some project specific changes is working well.
Unfortunately sourcemaps do not work, but thats maybe my buggy code or some other issue I can't solve.
Bugsnag has a module for nuxt. That could be used as a starting point for sentry perhaps? https://github.com/JulianMar/nuxt-bugsnag/blob/main/src/module.ts
Maybe a maintainer could establish a branch specifically for v3 support and a feature branch where we start working on first features for nuxt3 support. Even when not all features of the current module are covered, one could do a pre-release, so we all can start using it. After that, we can improve the solution.
At the moment, everyone would have to copy over a workaround and maybe some of them improve the current workaround but do not come back and contribute an improvement.
Created nuxt3 from main.
@anthony-bernardo I my case @darthf1 code with some project specific changes is working well.
Unfortunately sourcemaps do not work, but thats maybe my buggy code or some other issue I can't solve.
Add
sentryVitePlugin({
...
sourcemaps: {
assets: ['./.nuxt/dist/client/**'],
},
})
Snippet updated.
@anthony-bernardo I my case @darthf1 code with some project specific changes is working well. Unfortunately sourcemaps do not work, but thats maybe my buggy code or some other issue I can't solve.
Add
sentryVitePlugin({ ... sourcemaps: { assets: ['./.nuxt/dist/client/**'], }, })Snippet updated.
and clean the sourcemaps after upload with
sentryVitePlugin({
...
sourcemaps: {
assets: ['./.nuxt/dist/client/**'],
filesToDeleteAfterUpload: ['.nuxt/dist/**/*.js.map']
},
})
Have not tried it, but I found this repo. Looks promising, maybe it's a good start for a v3 version of this module?
Just a note that none of those solutions seem to handle server-side error tracking (on the h3 side).
@rchl I was looking for a solution for server side. Your note is just in time!
@rchl @kogratte I was looking for the same. As @rchl mentioned it is working only on the client side, any idea of to implement it on the server side?
The below is what I currently have. It uses
@sentry/vite-plugin, which should tick the second box. The below is most definitely not on par wit the current Nuxt 2 module, and the code is absolutely not battle tested. But maybe it can be used as a starting point.
~/nuxt.config.ts:import { sentryVitePlugin } from '@sentry/vite-plugin' import { defineNuxtConfig } from 'nuxt/config' export default defineNuxtConfig({ ssr: false, // I have SSR false runtimeConfig: { public: { ENV: process.env.NODE_ENV ?? 'production', SENTRY_ENABLED: process.env.NUXT_PUBLIC_SENTRY_ENABLED, SENTRY_DSN: process.env.NUXT_PUBLIC_SENTRY_DSN, SENTRY_ENVIRONMENT: process.env.NUXT_PUBLIC_.SENTRY_ENVIRONMENT, SENTRY_RELEASE: process.env.NUXT_PUBLIC_SENTRY_RELEASE, SENTRY_TRACE_PROPAGATION_TARGET: process.env.NUXT_PUBLIC_SENTRY_TRACE_PROPAGATION_TARGET, }, }, sourcemap: { server: true, client: true, }, vite: { build: { sourcemap: true, }, plugins: [ sentryVitePlugin({ authToken: process.env.NUXT_PUBLIC_SENTRY_AUTH_TOKEN, debug: true, org: process.env.NUXT_PUBLIC_SENTRY_ORG, project: process.env.NUXT_PUBLIC_SENTRY_PROJECT, release: { name: process.env.NUXT_PUBLIC_SENTRY_RELEASE, }, sourcemaps: { assets: ['./.nuxt/dist/client/**'], }, telemetry: false, }), ], }, })
~/plugins/sentry.client.ts:import { HttpClient as HttpClientIntegration, ReportingObserver as ReportingObserverIntegration, } from '@sentry/integrations' import type { Breadcrumb, CaptureContext, Primitive, User } from '@sentry/types' import * as Sentry from '@sentry/vue' import { withScope } from '@sentry/vue' import type { Router } from 'vue-router' import { defineNuxtPlugin } from '#app' export default defineNuxtPlugin({ parallel: true, setup: (nuxtApp) => { if ( typeof window === 'undefined' || !['true', true].includes(nuxtApp.$config.public.SENTRY_ENABLED) ) { return { provide: { sentrySetContext: ( _name: string, _context: { [key: string]: any } | null, ) => {}, sentrySetUser: (_user: User | null) => {}, sentrySetTag: (_key: string, _value: Primitive) => {}, sentryAddBreadcrumb: (_breadcrumb: Breadcrumb) => {}, sentryCaptureException: (_exception: any, _captureContext?: CaptureContext) => {}, }, } } Sentry.init({ app: nuxtApp.vueApp, autoSessionTracking: true, dsn: nuxtApp.$config.public.SENTRY_DSN, release: nuxtApp.$config.public.SENTRY_RELEASE, environment: nuxtApp.$config.public.SENTRY_ENVIRONMENT, integrations: [ new Sentry.BrowserTracing({ routingInstrumentation: Sentry.vueRouterInstrumentation(nuxtApp.$router as Router, { routeLabel: 'path', }), }), new Sentry.Replay({ networkDetailAllowUrls: [`https//${nuxtApp.$config.public.HOST_NAME}`], }), new HttpClientIntegration(), new ReportingObserverIntegration(), ], tracePropagationTargets: [nuxtApp.$config.public.SENTRY_TRACE_PROPAGATION_TARGET], trackComponents: true, hooks: ['activate', 'create', 'destroy', 'mount', 'update'], // Set tracesSampleRate to 1.0 to capture 100% // of transactions for performance monitoring. // We recommend adjusting this value in production tracesSampleRate: 0.2, // Capture Replay for 10% of all sessions, // plus for 100% of sessions with an error replaysSessionSampleRate: 0.1, replaysOnErrorSampleRate: 1, }) nuxtApp.vueApp.config.errorHandler = (err, context) => { withScope((scope) => { scope.setExtra('context', context) Sentry.captureException(err) }) } nuxtApp.hook('app:error', (err) => { Sentry.captureException(err) }) return { provide: { sentrySetContext: Sentry.setContext, sentrySetUser: Sentry.setUser, sentrySetTag: Sentry.setTag, sentryAddBreadcrumb: Sentry.addBreadcrumb, sentryCaptureException: Sentry.captureException, }, } }, })
@darthf1 Have you tried anything for the server side?
No. Like I mentioned, I'm using ssr: false so no server side for me.
@rchl @pallimalilsuhail @kogratte
I've been using the approaches offered upthread for getting Sentry integrated into Nuxt on the vite & vue side. (thanks for that!)
My first pass at making Sentry available to Nuxt on the server is with a Nitro plugin:
~/src/server/plugins/sentry.js:
import * as Sentry from "@sentry/node";
export default defineNitroPlugin((nitroApp) => {
const config = useRuntimeConfig();
Sentry.init({
dsn: config.public.SENTRY_DSN,
environment: config.public.SENTRY_ENVIRONMENT,
debug: true,
// ... Any other Sentry SDK options here
});
nitroApp.$sentry = Sentry;
nitroApp.hooks.hook('error', async (error, { event }) => {
Sentry.captureException(error);
});
nitroApp.hooks.hook('request', async (event) => {
event.context.$sentry = Sentry;
});
});
This uses Nitro's error hook to forward unhandled errors (or errors created with Nitro's createError()) to Sentry. This also captures unhandled errors in Vue component code running on the server.
Sentry is attached to the event context as $sentry at the beginning of a request in case a server page or middleware need access.
I guess some of the other Nitro hooks could be used to create Sentry breadcrumbs.
It doesn't know about the sourcemaps from Vite and I'm not sure how to tell it about them. It seems like Nuxt/Nitro uses its own build process and would need to coordinate with Vite about uploading them. Currently in a production Nuxt build, Vite has uploaded the source maps before Nitro starts compiling.
Hopefully this gives folks some ideas for improvements.
A bit simplistic since we should use Sentry's Request and Error handlers instead but maybe it will help someone with starting an implementation.
(Also when using tracing there should be Sentry's Tracing handler set up)
Mannil had a similar implementation on his blog: https://www.lichter.io/articles/nuxt3-sentry-recipe/#own-your-implementation Although, I think the type declaration should be this instead:
import type * as Sentry from '@sentry/node';
declare module 'h3' {
interface H3EventContext {
$sentry?: typeof Sentry;
}
}
I have been experimenting with server-side Nuxt & Sentry integration, and the blog at https://www.lichter.io/articles/nuxt3-sentry-recipe/ has been very useful.
However, there is one thing missing: Distributed HTTP tracing.
Adding new Integrations.Http({ tracing: true }) to integrations when calling init is not enough. One needs to start and finish a transaction for each request. The central problem is: Sentry uses global state, while Nitro serves requests concurrently. So if you start the transaction during the request hook, and finish it during the afterResponse hook, it will only work if the response is sent before the next request comes in. Otherwise the new request will create a new transaction, which discards the previous transaction. On a high traffic site, this will effectively prevent any transaction from ever reaching Sentry.
Here is my example repo: https://github.com/rudolfbyker/sentry-nuxt3-server-side-example/tree/e2558941a1f211344578adc21404978fd21ca25c
I'm talking to Sentry support about this, but if any of you have an interim solution for the concurrency problem, please share it!
EDIT: Of course I has to ask an AI, and it said to use a Hub: https://www.phind.com/search?cache=c9d7p9dp9osuiyzh7ggrd2qj I would love to hear your feedback on whether this will actually solve our problem...
EDIT: Reverse engineering and/or re-using the following seems to be the way to go:
- https://docs.sentry.io/platforms/node/guides/express/
- https://github.com/getsentry/sentry-javascript/blob/develop/packages/node/src/handlers.ts
I suppose express also handles requests concurrently so it's nothing inherently different in how h3 does it. It's just that the implementation of the Sentry request handler needs to more closely mimic what Sentry is doing in its bundled express middleware.
This version https://github.com/rudolfbyker/sentry-nuxt3-server-side-example/tree/f9da82eeaa51cdb94e41f6bc471bef0159ee0322 is my first attempt at re-using Sentry's express middleware in a Nitro server plugin. When looking at the console, it seems to work, but I don't see any data showing up in Sentry yet. I'm still debugging that...
EDIT: I can see some of the http.client spans in Sentry, but not all of them. I'm not sure if it's just hiding the short ones.
EDIT: It seems that I just had to be patient. It totally works. The "Slow HTTP Ops" on the "backend" tab of the "Performance" page is also working now.
Hey quick question I see the nuxt3 branch, is it possible to install it already as a nuxt package? Something like npm i @nuxtjs/sentry@nuxt3?
Dear regards, snakysnake
No, the branch is just a fork and contains no Nuxt3-specific code..
Anyone know an alternative to Sentry? open-source, of course. I looked for some by my own, but none of them are compatible with Nuxt 3. any advice? Thanks.
Anyone know an alternative to Sentry? open-source, of course. I looked for some by my own, but none of them are compatible with Nuxt 3. any advice? Thanks.
https://www.lichter.io/articles/nuxt3-sentry-recipe/
We've made our setup using this guide and it works absolutely fine.
Anyone know an alternative to Sentry? open-source, of course. I looked for some by my own, but none of them are compatible with Nuxt 3. any advice? Thanks.
https://www.lichter.io/articles/nuxt3-sentry-recipe/
We've made our setup using this guide and it works absolutely fine.
yeah... but sorry. Could you please show me an example of how to implement this on client side? Thanks.