-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Plan for Nuxt 3 version #530
Comments
any updates here guys |
The below is what I currently have. It uses
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,
}),
],
},
})
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,
},
}
},
}) |
This comment was marked as off-topic.
This comment was marked as off-topic.
sourcemap are not working with the abobe configuration |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
I don't understand well Is the @darthf1 code workings or it's your current not working code ? |
@anthony-bernardo 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? |
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 |
Add sentryVitePlugin({
...
sourcemaps: {
assets: ['./.nuxt/dist/client/**'],
},
}) Snippet updated. |
and clean the sourcemaps after upload with
|
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! |
@darthf1 Have you tried anything for the server side? |
No. Like I mentioned, I'm using |
@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:
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 Sentry is attached to the event context as 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 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 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 |
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. |
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? |
Thanks. It works correctly. |
@manniL Thanks for your article which i followed. It works but i do get plenty of the below console errors: |
@ssglopes If you can live without the sentry replays, on my side removing |
I followed this guide and everything worked: https://gearboxgo.com/articles/web-application-development/setting-up-sentry-with-nuxt-3 |
GlitchTip is a Sentry-compatible open source alternative! |
If anyone has the time, you could contribute GlitchTip nuxt documentation by adding a markdown file here. |
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 |
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')
}
}) |
getsentry/sentry-javascript#6929 (comment) Sentry is actually taking a look at building a 1st class Nuxt SDK, |
@ssglopes to get rid of those messages, you can wrap the content of your plugin with an |
Package has been released: https://www.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.
The text was updated successfully, but these errors were encountered: