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

Sentry headers from browser fetch request change after being proxied through NextJS Server using middleware and rewrite. #15435

Closed
3 tasks done
zeeshan-at-extra-year opened this issue Feb 18, 2025 · 6 comments
Labels
getsentry/sentry-javascript Package: nextjs Issues related to the Sentry Nextjs SDK Stale

Comments

@zeeshan-at-extra-year
Copy link

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

9.1.0

Framework Version

Next 14.2.4

Link to Sentry event

https://extrayear.sentry.io/traces/trace/e496329273177d9dc22c4caba543e428/?node=txn-892cf59a8ba7458fa0128d556bcf6853&pageEnd&pageStart&project=4508092950183936&query=trace%3Ae496329273177d9dc22c4caba543e428&source=traces&statsPeriod=2h&timestamp=1739840031.143

Reproduction Example/SDK Setup

sentry.client.config.ts

// This file configures the initialization of Sentry on the client.
// The config you add here will be used whenever a users loads a page in their browser.
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
import { Environment } from '@newdays/platform-layer/common/environment';
import { MonitoringConfig } from '@newdays/platform-layer/common/monitoring-config';
import * as Sentry from '@sentry/nextjs';

Sentry.init({
  dsn: 'https://aedf18cc6cd72059ecd18e07156d03eb@o4507609249611776.ingest.us.sentry.io/4508092950183936',

  // Add optional integrations for additional features
  integrations: [Sentry.replayIntegration(), Sentry.browserTracingIntegration()],

  // Define how likely traces are sampled. Adjust this value in production, or use tracesSampler for greater control.
  tracesSampleRate: 1,

  // Set `tracePropagationTargets` to control for which URLs trace propagation should be enabled
  tracePropagationTargets: ['localhost', /^https:\/\/app.*/],

  // Define how likely Replay events are sampled.
  // This sets the sample rate to be 10%. You may want this to be 100% while
  // in development and sample at a lower rate in production
  replaysSessionSampleRate: 0.1,

  // Define how likely Replay events are sampled when an error occurs.
  replaysOnErrorSampleRate: 1.0,

  // Setting this option to true will print useful information to the console while you're setting up Sentry.
  debug: false,

  // The environment for emitting metrics
  environment: Environment.get(),

  // Whether to enable monitoring
  enabled: MonitoringConfig.isEnabled(),
});

sentry.edge.config.ts

// This file configures the initialization of Sentry for edge features (middleware, edge routes, and so on).
// The config you add here will be used whenever one of the edge features is loaded.
// Note that this config is unrelated to the Vercel Edge Runtime and is also required when running locally.
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
import { Environment } from '@newdays/platform-layer/common/environment';
import { MonitoringConfig } from '@newdays/platform-layer/common/monitoring-config';
import * as Sentry from '@sentry/nextjs';

Sentry.init({
  dsn: 'https://aedf18cc6cd72059ecd18e07156d03eb@o4507609249611776.ingest.us.sentry.io/4508092950183936',

  // Define how likely traces are sampled. Adjust this value in production, or use tracesSampler for greater control.
  tracesSampleRate: 1,

  // Setting this option to true will print useful information to the console while you're setting up Sentry.
  debug: false,

  // The environment for emitting metrics
  environment: Environment.get(),

  // Whether to enable monitoring
  enabled: MonitoringConfig.isEnabled(),
});

sentry.server.config.ts

// This file configures the initialization of Sentry on the server.
// The config you add here will be used whenever the server handles a request.
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
import { Environment } from '@newdays/platform-layer/common/environment';
import { MonitoringConfig } from '@newdays/platform-layer/common/monitoring-config';
import * as Sentry from '@sentry/nextjs';

Sentry.init({
  dsn: 'https://aedf18cc6cd72059ecd18e07156d03eb@o4507609249611776.ingest.us.sentry.io/4508092950183936',

  // Define how likely traces are sampled. Adjust this value in production, or use tracesSampler for greater control.
  tracesSampleRate: 1,

  // Setting this option to true will print useful information to the console while you're setting up Sentry.
  debug: false,

  // The environment for emitting metrics
  environment: Environment.get(),

  // Whether to enable monitoring
  enabled: MonitoringConfig.isEnabled(),
});

Steps to Reproduce

  1. Setup backend with NestJS and enable sentry on nestjs.
  2. Setup frontend with nextjs and enable sentry on nextjs. The frontend should be using nextjs rewrite to proxy the path to an external URL.
  3. Make a browser fetch request to the path.
  4. Examine fetch request headers in browser and ensure that the sentry headers match the meta tags embedded in the browser.
  5. Examine headers on backend.

Expected Result

Sentry trace propagation expectations

When a fetch request is made with proper sentry headers, the trace should continue on the nextjs server and on the backend. While the trace does continue on the nextjs server, the sentry headers get overwritten with some other trace before being sent to the external URL.

Actual Result

Fetch request in browser

Image

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Feb 18, 2025
@github-actions github-actions bot added the Package: nextjs Issues related to the Sentry Nextjs SDK label Feb 18, 2025
@zeeshan-at-extra-year
Copy link
Author

Few things I have already verified:

  • The sentry headers get attached by the browser from the meta tags.
  • The sentry headers are present when the request is intercepted by middleware in the nextjs server.
  • The headers change when they arrive on the external URL after rewrite.
  • The sentry headers are preserved when the request is made directly from nextjs server (i.e no browser fetch is done.)

@Lms24
Copy link
Member

Lms24 commented Feb 18, 2025

Hi @zeeshan-at-extra-year thanks for writing in and for the detailed report! I've forwarded this in the team to people better versed in NextJS than me. We'll get back to you.

@chargome
Copy link
Member

Hey @zeeshan-at-extra-year !
Just to better understand this: the headers from your browser fetch request never make it to the NestJS server (you could check out the received headers of a controller using @Headers)?

@zeeshan-at-extra-year
Copy link
Author

zeeshan-at-extra-year commented Feb 18, 2025

the headers from your browser fetch request never make it to the NestJS server

Yes that is correct. The headers from the browser fetch request never make it to the NestJS server.

(you could check out the received headers of a controller using @headers)

I did try this out. I added a global middleware to log all request headers. The sentry-trace header is not the same as the browser fetch request header. The baggage seems to have the same public key as the baggage on the browser.

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Feb 18, 2025
@chargome
Copy link
Member

And your request flow is from Browser -> NextJS Backend -> NestJS Backend? Could you maybe provide a minimal repro for this?

@getsantry
Copy link

getsantry bot commented Mar 27, 2025

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 remove the label Waiting for: Community, I will leave it alone ... forever!


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

@getsantry getsantry bot added the Stale label Mar 27, 2025
@getsantry getsantry bot closed this as not planned Won't fix, can't repro, duplicate, stale Apr 4, 2025
@linear linear bot closed this as completed Apr 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
getsentry/sentry-javascript Package: nextjs Issues related to the Sentry Nextjs SDK Stale
Projects
Archived in project
Development

No branches or pull requests

4 participants