getsentry / sentry-javascript

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

can not disable session tracking and client reports for Next.js #10962

Closed volker-attempto closed 7 months ago

volker-attempto commented 7 months ago

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/nextjs

SDK Version

7.105.0

Framework Version

7.105.0

Link to Sentry event

No response

SDK Setup

next.config.js

/** @type {import('next').NextConfig} */

const { withSentryConfig } = require('@sentry/nextjs');

const nextConfig = {
  output: 'standalone',
  reactStrictMode: true,
  i18n: {
    locales: ['de'],
    defaultLocale: 'de',
  },
  async redirects() {
    return [
      {
        source: '/',
        destination: '/home',
        permanent: false,
      },
    ];
  },
  sentry: {
    disableServerWebpackPlugin: true, // Disable the Sentry Webpack plugin for server bundles
    disableClientWebpackPlugin: true, // Disable the Sentry Webpack plugin for client bundles
    disableLogger: true,
  },
};

const sentryWebpackPluginOptions = {
  org: '---removed---',
  project: '---removed---',
  environment: process.env.NEXT_PUBLIC_SENTRY_ENVIRONMENT,

  silent: true, // Suppresses all logs
};

module.exports = withSentryConfig(nextConfig, sentryWebpackPluginOptions);
module.exports.env = {
  NEXT_PUBLIC_SENTRY_DSN: process.env.NEXT_PUBLIC_SENTRY_DSN,
};

sentry.client.config.ts

Sentry.init({
  dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
  environment: process.env.NEXT_PUBLIC_SENTRY_ENVIRONMENT,
  enableTracing: false,
  debug: true,
  // Adjust this value in production, or use tracesSampler for greater control
  tracesSampleRate: 0.0,
  autoSessionTracking: false,
  // Setting this option to true will print useful information to the console while you're setting up Sentry.
  // debug: process.env.NEXT_PUBLIC_SENTRY_DEBUG === 'true',
  sendClientReports: false,
});

Steps to Reproduce

  1. start application in prod mode pnpm run start
  2. load page in Chrome browser
  3. see the session request with payload:
    {"sent_at":"2024-03-07T14:32:52.493Z","sdk":{"name":"sentry.javascript.nextjs","version":"7.105.0"}}
    {"type":"session"}
    {"sid":"93a52e4fe04a4e70a24d2f8d8775ce3f","init":true,"started":"2024-03-07T14:32:52.493Z","timestamp":"2024-03-07T14:32:52.493Z","status":"ok","errors":0,"attrs":{"release":"9sBuGU8rJAON6_VQvPLgv","environment":"local","user_agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"}}
  4. show initiator for call: _sendEnvelope <- sendSession <- captureSession <- .... <- onEntrypoint
  5. client reports are also sent, when tracesSampleRate is set to 0.0

  1. start application in dev mode pnpm run dev
  2. load page in Chrome browser
  3. see client report request with payload:
    {}
    {"type":"client_report"}
    {"timestamp":1709822964.918,"discarded_events":[{"reason":"sample_rate","category":"transaction","quantity":4}]}

Expected Result

Respect the configuration files and make requests to sentry.io only in error case.

Actual Result

dev mode seems to obey the "autoSessionTracking: false" instruction, but still sends client reports, whereas prod mode doesn't care about the configuration at all

When debugging, I can see my provided configuration is somewhere in the code:

Screenshot 2024-03-07 at 12 09 46

But the configuration seems to be unavailable / set to defaults at some other point:

Screenshot 2024-03-07 at 12 08 01

lforst commented 7 months ago

I have to ask this: Did you trigger a rebuild after you changed the settings and before you ran pnpm run start?

volker-attempto commented 7 months ago

I have to ask this: Did you trigger a rebuild after you changed the settings and before you ran pnpm run start?

Yes, my IDE-button, which runs pnpm run start does always a pnpm run build before.

lforst commented 7 months ago

Hi, setting autoSessionTracking: false in sentry.client.config.ts works as expected in my test app. No session events are sent. Would you mind providing a minimal reproduction example? Thank you!

(Before that, I recommend deleting your .next folder once to ensure that no caching or similar swallowed your changes.)

volker-attempto commented 7 months ago

We found the culprit. There was another file included in our _app.tsx from previous sentry implementation attempts. It called Sentry.init with an incomplete configuration object.

Thank you very much for your help!

I hereby close this ticket.