Skip to content

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

@zeeshan-at-extra-year

Description

@zeeshan-at-extra-year

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    Waiting for: Community

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions