getsentry / sentry-javascript

Official Sentry SDKs for JavaScript
https://sentry.io
MIT License
7.94k stars 1.56k forks source link

Init with environment config not working #5130

Closed ghost closed 2 years ago

ghost commented 2 years ago

Environment

SaaS (https://sentry.io/)

Version

No response

Steps to Reproduce

My project using NextJS and deploy it with Vercel, I am integrating with Sentry and I want to filter environment to like dev, staging, production. I have followed the instruction to pass the config environment to the Sentry init, but it doesn't work, any idea?

sentry.client.config.js

Sentry.init({
    dsn: ...
    // Adjust this value in production, or use tracesSampler for greater control
    tracesSampleRate: 1.0,
    debug: true,
    environment: 'custom-staging',
    // ...
    // Note: if you want to override the automatic release value, do not set a
    // `release` value here - use the environment variable `SENTRY_RELEASE`, so
    // that it will also get attached to your source maps
})

sentry.server.config.js

Sentry.init({
    dsn: ...
    // Adjust this value in production, or use tracesSampler for greater control
    tracesSampleRate: 1.0,
    debug: true,
    environment: 'custom-staging',
    // ...
    // Note: if you want to override the automatic release value, do not set a
    // `release` value here - use the environment variable `SENTRY_RELEASE`, so
    // that it will also get attached to your source maps
})

Expected Result

Has new filter when release a new version

Actual Result

Screen Shot 2022-05-18 at 14 49 46 Screen Shot 2022-05-18 at 14 50 41

It has only production and development

getsentry-release commented 2 years ago

Routing to @getsentry/team-web-sdk-frontend for triage. ⏲️

lforst commented 2 years ago

Hi, I do not see anything wrong with your configuration. Are you using the Sentry Vercel integration? There you might be able to set the SENTRY_ENVIRONMENT env variable. More info.

Also, are you sending errors to sentry using the custom-staging environment? You need to send at least one event to sentry before the environment appears in the interface. Can you verify this?

pawlarius commented 2 years ago

I have the same issue. I've tried with Sentry Vercel integration and adding SENTRY_ENVIRONMENT as "staging".

And sentry somehow ignores that and set it as "production" 😭

I've tried without Sentry Vercel integration too, manually calling Sentry.init and setting the environment not working, seems like Sentry is reading from another env, probably NODE_ENV.

I read the sentry guide, it only said either set environment manually during Sentry.init or use env SENTRY_ENVIRONMENT. Both not working and I'm not sure how to continue now.

pawlarius commented 2 years ago

Ok, i think i've got it working.

So first I'm connecting my project with Sentry Vercel Integration, which will add some SENTRY environments to my Vercel. For some reason, the env only available for Vercel production env, I change those for Vercel production,preview & development.

Then I follow instructions mentioned here Sentry NextJS guide which will generate some files and some of them are sentry.client.config.js and sentry.server.config.js.

Inside those file there should be Sentry.init function and I place the environment there, but with prefix NEXTPUBLIC* for both sentry.client.config.js and sentry.server.config.js.

Sentry.init({
  dsn: SENTRY_DSN || '',
  tracesSampleRate: 1.0,
  release: process.env.VERCEL_GIT_COMMIT_SHA,
  // Add prefix NEXT_PUBLIC_*
  environment: process.env.NEXT_PUBLIC_SENTRY_ENVIRONMENT,
});

Then it's working. I think the issue is sentry.client.config.js can't read process.env.SENTRY_ENVIRONMENT since it's not available to browser version.

vladanpaunovic commented 2 years ago

@pawlarius, you are right! 🎉

This is by Next.js's design. It is stripping environment variables exposed to the browser for security reasons. You can read more about it here - Next.js: Exposing Environment Variables to the Browser, but essentially it boils down to this:

By default environment variables are only available in the Node.js environment, meaning they won't be exposed to the browser.

@thienna-beincomm, please prefix your env variables with NEXT_PUBLIC_ so Next.js exposes it on the browser and it will work.

@thienna-beincomm, please let us know if this resolves the issue for you.

github-actions[bot] commented 2 years ago

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀