sentry-javascript icon indicating copy to clipboard operation
sentry-javascript copied to clipboard

TanStack Start on Nitro: first request per server instance has no http.server span without --import

Open ryanhiizy opened this issue 1 month ago • 2 comments

Is there an existing issue for this?

  • [x] I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues
  • [x] I have reviewed the documentation https://docs.sentry.io/
  • [x] I am using the latest SDK release https://github.com/getsentry/sentry-javascript/releases

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/tanstackstart-react

SDK Version

11.10.0

Framework Version

@tanstack/react-start 1.168.60, nitro 3.0.260903-beta

Reproduction Example/SDK Setup

On hosts where node --import isn't available, such as Vercel, #19458 recommends importing the instrumentation first in server.ts:

// src/server.ts
import './instrument.server';
import { wrapFetchWithSentry } from '@sentry/tanstackstart-react';
import handler, { createServerEntry } from '@tanstack/react-start/server-entry';

export default createServerEntry(
  wrapFetchWithSentry({
    fetch(request: Request) {
      return handler.fetch(request);
    },
  }),
);
// src/instrument.server.ts
import * as Sentry from '@sentry/tanstackstart-react';

Sentry.init({ dsn: '...', tracesSampleRate: 1, debug: true });
console.log('[sentry] init');

The app is your tanstackstart-react e2e test app, cut down to __root and index, using the nitro() and sentryTanstackStart() Vite plugins.

Steps to Reproduce

  1. Run vite build.
  2. Run node .output/server/index.mjs, without --import.
  3. Request / twice.

Expected Result

Both requests produce an http.server root span.

Actual Result

[sentry] init isn't printed at startup. It's printed when the first request arrives, so the HTTP instrumentation misses that request:

Listening on: http://localhost:3107/
# first request
[sentry] init
[Tracing] Starting sampled root span
  op: middleware
  name: globalRequestMiddleware
Sending span envelope for trace e4ef... with 1 spans
# second request
Http.Server Handling incoming request
[Tracing] Starting sampled root span
  op: http.server
  name: GET
[Tracing] Starting sampled span
  op: middleware
  name: globalRequestMiddleware
Sending span envelope for trace aa6e... with 2 spans

Additional Context

Nitro loads the server entry lazily, so server.ts first runs inside the first request:

// .output/server/index.mjs
var _lazy = defineLazyEventHandler(() => import("./_chunks/ssr-renderer.mjs"));

On serverless hosts this repeats for every new instance, so the requests with no trace are the cold starts. We see about 2,000 a day on a Vercel staging deployment (measured on SDK 11.9.0).

We tried calling Sentry.init from a Nitro plugin, which runs at startup. The plugin bundled a second copy of the SDK that never received the route names, so route naming broke and we reverted it. A supported way to initialise from a Nitro plugin, or to have the Vite plugin do it, would fix this.

This was written by Opus 5.5.

ryanhiizy avatar Oct 31 '26 06:10 ryanhiizy

JS-4051

linear-code[bot] avatar Oct 31 '26 06:10 linear-code[bot]

Thanks for reaching out, we'll look into it!

chargome avatar Nov 05 '26 08:11 chargome