Skip to content
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

[JavaScript] Nuxt SDK #9095

Open
19 of 35 tasks
stephanie-anderson opened this issue Sep 22, 2023 · 42 comments
Open
19 of 35 tasks

[JavaScript] Nuxt SDK #9095

stephanie-anderson opened this issue Sep 22, 2023 · 42 comments
Assignees
Labels
Package: nuxt Issues related to the Sentry Nuxt SDK

Comments

@stephanie-anderson
Copy link
Contributor

stephanie-anderson commented Sep 22, 2023

Important

This issue is tracking progress on the Nuxt SDK. If you find bugs or problems with the @sentry/nuxt package please open a separate issue. Thank you!

New SDK for https://nuxt.com/

There is an existing discussion: Sentry SDK for Nuxt

Nuxt uses nitro as their server, this means we'll need to have nitro support. It might make sense for us to create an OpenTelemetry instrumentation for nitro. It will also be valuable to add similar instrumentation for the other libraries Nuxt relies on:

Alpha

  1. Package: nuxt
  2. Package: nuxt

Beta

  1. Package: nuxt
    s1gr1d
  2. Package: nuxt
  3. Package: nuxt
    s1gr1d
  4. Package: nuxt
    s1gr1d

Stable Release

  1. Package: nuxt
    s1gr1d

Nice to have/Stretch Goals

  1. Package: nuxt Type: Tests
  2. Package: nuxt
@stephanie-anderson stephanie-anderson added the Package: nuxt Issues related to the Sentry Nuxt SDK label Sep 22, 2023
@AbhiPrasad
Copy link
Member

See #6929

@s1gr1d s1gr1d self-assigned this Jun 17, 2024
@manniL
Copy link

manniL commented Jun 20, 2024

If you need insights from the Nuxt team at any time, please don't hesitate to reach out! 🙏

@cosbgn
Copy link

cosbgn commented Jun 24, 2024

This is such a great news!

It would be ideal that it would work on all envs.
Nuxt works very well on the edge (cloudflare workers, netlify edge, etc), however not all libraries are compatible as there are some restrictions (no eval, no new Function(), etc).

It would be great if sentry/nuxt would work everywhere from day one.

@s1gr1d
Copy link
Member

s1gr1d commented Jun 24, 2024

@cosbgn Thanks for the input! Which libraries do you mean with "not all libraries are compatible"?

@cosbgn
Copy link

cosbgn commented Jun 24, 2024

@cosbgn Thanks for the input! Which libraries do you mean with "not all libraries are compatible"?

Hi @s1gr1d, I don't have an extensive list, but for example AJV doesn't work. Workers don't use node, they use their own runtime (workerd) which is limited to web standards (similarly to deno).

I think if you use only sentry/core it should work as it's used by toucan-js which is an unofficial client for cloudflare workers. I think sentry/node won't work by default, but I might be wrong here.

@cosbgn
Copy link

cosbgn commented Jun 24, 2024

This is of course for the backend part of nuxt, i.e. nitro. On the frontend sentry/vue should work perfectly.

@alexcroox
Copy link

alexcroox commented Jun 24, 2024

I use Sentry on my APIs (fullstack Nuxt 3 deployment to Cloudflare pages) with the sentry/browser package on the API side to get around edge restrictions.

The context Sentry devs, for cosbgn's comment, is that Nuxt 3 was built with the "deploy anywhere" ethos, so all the core packages and (new) community modules are designed to be runtime agnostic. That's where https://unjs.io/ was born from.

I initially followed this guide if it helps at all with the PR in general https://www.lichter.io/articles/nuxt3-sentry-recipe (written by a core Nuxt 3 dev)

@lforst
Copy link
Member

lforst commented Jun 24, 2024

While we may not support worker (WinterCG) runtimes from the start we will look into it eventually. We are planning to release the SDK iteratively and in stages. Prioritization of worker runtime support depends of course a bit on the demand. I added an item to the list to track. Technically we have all the building blocks available.

@cosbgn
Copy link

cosbgn commented Jun 24, 2024

I don't have any specific data, but from my understanding a big percent of nuxt apps are deployed on the edge (vercel-edge, workers, netlify edge, etc) as you get better performance for lower costs, and Nuxt pretty much "guarantees" that it will work on the edge, so it's an obvious solution.

I understand if you decide not to prioritize it, I just wanted to let you know that it would be great to have it from day one. It might be as simple as using sentry/core rather than sentry/node (I don't know, just an example).

Anyways, thanks for the first party package, love sentry and I would love to be able to add it to Nuxt easily!

@gloompiq
Copy link

Great initiative!

Could we also make sure that errors before the hydration are also handled?

@s1gr1d
Copy link
Member

s1gr1d commented Jun 28, 2024

We want to monitor as many as possible. The first release of the SDK will cover monitoring the majority of errors and we will definitely improve it over time

s1gr1d added a commit that referenced this issue Jul 1, 2024
To be able to differentiate between a browser/client execution context,
sentry is initialized in an external config file. An import statement in
`nuxt-root.vue` is added which loads this config file.

Nuxt tracking issue:
#9095

---------

Co-authored-by: Abhijeet Prasad <aprasad@sentry.io>
@s1gr1d
Copy link
Member

s1gr1d commented Aug 26, 2024

Yes, something like this could work. I am just trying different approaches.

@smeubank
Copy link
Member

@s1gr1d some feedback from someone testing out the Nuxt SDK. Some of this feedback i think holds true regarding the file requirements for config of most of our JS SDKs, especially the meta frameworks.

running in dev, i notice that Nuxt has a dev tool bar, slightly (un)related but i wonder if Sentry could interact there? Like you could get more info about the error in browser in dev mode?

Tried it out today. Looking great! Here are just a few things I found that could use improvement IMO:

  1. having the instrument.server.mjs file in the public folder is kind of weird. Feels more natural in the root. Maybe can copy to public folder on build? Changing the commands for the build is also weird. I'm sure Daniel Roe or Alex Lichter could point you towards a better solution.

  2. Would be nice if you could do all the config in the nuxt.config.ts file instead of spread out across 3 different files

  3. I trigger an error in a server API endpoint and didn't see it reported in Sentry (https://nuxt.com/docs/guide/directory-structure/server)

  4. Would be cool if you could detect the app running in dev vs running in prod and automatically set the environment in sentry

  5. Didn't see an error with the fatal option set to true reported to sentry( throw createError({ fatal: true, message: "etc..." }) https://nuxt.com/docs/api/utils/create-error

Hope it helps!

@samulefevre
Copy link

I tested it for server, i use startSpan but nothing sent to "Performance".

@lforst
Copy link
Member

lforst commented Aug 29, 2024

@samulefevre thanks for trying out the SDK. Would you mind opening a new issue with reproduction steps? Thank you!

@AnthonyRuelle
Copy link

Hello,

Thank you for your work!

I'm encountering two significant issues while integrating Sentry into my Nuxt.js project:

1 - Sourcemap Upload Fails on Staging:

Local Build: When building the project locally, the sourcemap is successfully uploaded to Sentry. (
Only when the sentry.client.config.ts file is successfully created)
Staging Environment: On my staging environment (a test environment similar to production), the sourcemap upload fails with the following warning:

caused by: [60] SSL peer certificate or SSH remote key was not OK (SSL certificate problem: unable to get local issuer certificate)
error: API request failed

This may be due to server configuration, I will investigate

2 - Environment Variables Not Accessible:
I'm unable to access environment variables in both sentry.client.config.ts and instrument.server.mjs.
I've tried various approaches, but variables remain undefined.
Is there a known solution for this, or is it currently not feasible to access environment variables in these specific files?

3 - Attempted Solution with Nuxt Plugin:

I attempted to create a custom Nuxt plugin to cleanly manage the Sentry configuration, but in this case, the sourcemap upload doesn’t even trigger as the file seems to be unrecognized.

Thank you in advance for your feedback :)

@lforst
Copy link
Member

lforst commented Aug 30, 2024

@AnthonyRuelle I transferred your comment into a separate issue because otherwise this issue is gonna get messy af

@iBobik
Copy link

iBobik commented Sep 24, 2024

@cosbgn @s1gr1d Not sure what exactly from @sentry/node does not work in workers envs, but exactly for this runtimes there is https://github.com/robertcepa/toucan-js, check what it does differently.

How I use Sentry in Nuxt currently (without frontend logging):

nuxt.config.ts:

import { sentryVitePlugin } from "@sentry/vite-plugin";

const sentryReleaseName = process.env.CF_PAGES_COMMIT_SHA ?? 'unknown commit'

export default defineNuxtConfig({
  ...

  runtimeConfig: {
    public: {
      sentryDsn: 'xxx,
      sentryReleaseName,
    },
  },

  vite: {
    build: {
      sourcemap: true,
    },
    plugins: [
      // Put the Sentry vite plugin after all other plugins
      sentryVitePlugin({
        authToken: process.env.SENTRY_AUTH_TOKEN,
        org: "nuxt.config.ts",
        project: "xxx",
        release: {
          name: sentryReleaseName,
        },
      }),
    ],
  },
})

server/plugins/sentry.ts:

import { Toucan } from 'toucan-js'

export default defineNitroPlugin((nitroApp) => {
	const { public: { sentryDsn, sentryReleaseName } } = useRuntimeConfig()

	nitroApp.hooks.hook('error', (err, context) => {
		const incomingMessage = context.event?.node.req;
		const request = incomingMessage?.url ? new Request(incomingMessage.url, {
			method: incomingMessage.method,
		}) : undefined;

		const sentry = new Toucan({
			dsn: sentryDsn,
			release: sentryReleaseName,
			context: context.event,
			request,
			requestDataOptions: {
				allowedSearchParams: true,
			},
		})

		sentry.setTag('server', true)

		sentry.captureException(err)
	})
})

@ps-20x
Copy link

ps-20x commented Sep 24, 2024

I don't know if this is of any help but nuxt scripts provides some options for better performance loading scripts (also npm scripts). Maybe adding a registry script could provide some features and help with the performance.

@s1gr1d
Copy link
Member

s1gr1d commented Sep 24, 2024

@iBobik I haven't tested @sentry/nuxt with cloudflare workers so far as this feature will be included a little bit later. But feel free to open a separate issue (to better keep track of it) if you encounter any problems :)

@ps-20x Thanks for the input! I'm not quite sure how nuxt scripts can be used here. Perhaps you could explain a little more about how you thought this could be used in the SDK :)

@BlazingTide
Copy link

Hi, site maps are not being generated, or at least not being read correctly when viewing an error.

Does Sentry automatically generate sitemaps via nuxt.config's sentry options (as listed in the docs), or is another command required?

@s1gr1d
Copy link
Member

s1gr1d commented Sep 25, 2024

@BlazingTide If there is an issue with the experimental SDK feel free to open a new issue in our repository as it makes it easier to keep track of things 🙌

I am not sure what you mean with "sitemaps" - we don't generate sitemaps. In case you mean source maps: Yes, they are generated and uploaded automatically if you provide the sourceMapsUploadOptions and you don't need another command :)

@mydea mydea changed the title Nuxt SDK [JavaScript] Nuxt SDK Sep 26, 2024
@ps-20x
Copy link

ps-20x commented Oct 1, 2024

Thanks for the input! I'm not quite sure how nuxt scripts can be used here. Perhaps you could explain a little more about how you thought this could be used in the SDK :)

So the idea is to optimize the page loading performance by using nuxt scripts and prevent possible errors with SSR. For example it provides a proxy object to track events and will fire them when it is fully set up.
The slogan on the page says

Nuxt Scripts lets you load third-party scripts with better performance, privacy, security and DX

and

Nuxt Scripts provides an abstraction layer on top of third-party scripts, providing SSR support and type-safety and while still giving you full low-level control over how a script is loaded.

But as mentioned before: I am not sure if it would be possible to adapt the logic so it could be integrated.

@lforst
Copy link
Member

lforst commented Oct 1, 2024

@ps-20x Re: nuxt scripts. I think we need to look into it a bit further. Superficially I can say that the upside it gives is probably very slim since the Sentry SDK needs to load very early on in the pageload for a bunch of important features (errors, performance, replay). The DX is definitely an argument.

For now, I think we'll try to focus on shipping a robust SDK without using nuxt scripts but there is nothing that prevents us from migrating to that pattern later on. (also note that nuxt scripts are in beta - to some degree atleast)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: nuxt Issues related to the Sentry Nuxt SDK
Projects
Status: No status
Development

No branches or pull requests