Closed rchl closed 1 week ago
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:
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.
@YoimarMoreno This is also included in the blog post π
@manniL Have you already started working on sentry module for nuxt3? Is there a repo we could take a look and maybe contribute?
@YoimarMoreno This is also included in the blog post π
Thanks. It works correctly.
@manniL Thanks for your article which i followed. It works but i do get plenty of the below console errors:
The 'compilerOptions' config option is only respected when using a build of Vue.js that includes the runtime compiler (aka "full build"). Since you are using the runtime-only build, 'compilerOptions' must be passed to '@vue/compiler-dom' in the build setup instead.
Any idea how to fix this so these errors go away?
@ssglopes If you can live without the sentry replays, on my side removing new Sentry.Replay(),
resolves the warnings.
I followed this guide and everything worked: https://gearboxgo.com/articles/web-application-development/setting-up-sentry-with-nuxt-3
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.
GlitchTip is a Sentry-compatible open source alternative! So it should work with the tutorials that people are posting here https://glitchtip.com/
If anyone has the time, you could contribute GlitchTip nuxt documentation by adding a markdown file here.
@manniL Thanks for your article which i followed. It works but i do get plenty of the below console errors:
The 'compilerOptions' config option is only respected when using a build of Vue.js that includes the runtime compiler (aka "full build"). Since you are using the runtime-only build, 'compilerOptions' must be passed to '@vue/compiler-dom' in the build setup instead.
Any idea how to fix this so these errors go away?
I moved the lazy loading code of Sentry Replay into a component to solve this problem.
<!-- src/components/Sentry.vue -->
<script setup lang="ts">
onMounted(async () => {
try {
// lazy load sentry replay
const { addIntegration, Replay } = await import("@sentry/vue");
addIntegration(new Replay({}));
} catch {}
});
</script>
<template></template>
<!-- app.vue -->
<template>
<ClientOnly>
<Sentry />
</ClientOnly>
<NuxtLayout>
<NuxtPage />
</NuxtLayout>
</template>
And here is my version to integrate Sentry by encapsulating it into a module. It works fine.
How to use
import { captureMessage, setExtra, setTag } from '@sentry/vue';
in project. I have 500 err, it can't be imported.
@manniL Thanks for your article which i followed. It works but i do get plenty of the below console errors:
The 'compilerOptions' config option is only respected when using a build of Vue.js that includes the runtime compiler (aka "full build"). Since you are using the runtime-only build, 'compilerOptions' must be passed to '@vue/compiler-dom' in the build setup instead.
Any idea how to fix this so these errors go away?I moved the lazy loading code of Sentry Replay into a component to solve this problem.
<!-- src/components/Sentry.vue --> <script setup lang="ts"> onMounted(async () => { try { // lazy load sentry replay const { addIntegration, Replay } = await import("@sentry/vue"); addIntegration(new Replay({})); } catch {} }); </script>
Nice idea! You can also use hook inside your plugin in order to keep components structure clean
nuxtApp.hook('app:mounted', async () => {
try {
const { addIntegration, Replay } = await import('@sentry/vue')
const replay = new Replay({
beforeErrorSampling: event => {
if (event.tags?.['no-sentry-replay']) {
return false
}
return true
},
})
addIntegration(replay)
} catch {
console.warn('Sentry replay not loaded')
}
})
https://github.com/getsentry/sentry-javascript/discussions/6929#discussioncomment-9818779
Sentry is actually taking a look at building a 1st class Nuxt SDK, @sentry/nuxt
right now! π
@manniL Thanks for your article which i followed. It works but i do get plenty of the below console errors:
The 'compilerOptions' config option is only respected when using a build of Vue.js that includes the runtime compiler (aka "full build"). Since you are using the runtime-only build, 'compilerOptions' must be passed to '@vue/compiler-dom' in the build setup instead.
Any idea how to fix this so these errors go away?
@ssglopes to get rid of those messages, you can wrap the content of your plugin with an if (config.public.NODE_ENV !== development)
getsentry/sentry-javascript#6929 (comment)
Sentry is actually taking a look at building a 1st class Nuxt SDK,
@sentry/nuxt
right now! π
Package has been released: https://www.npmjs.com/package/@sentry/nuxt π
Package has been released: npmjs.com/package/@sentry/nuxt π
It's worth noting that it is described by Sentry staff as being in the late alpha stage.
Closing as the Nuxt 3 Sentry module exists now.
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:
process.sentry
which 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.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.