Skip to content

Conversation

@nicohrubec
Copy link
Member

@nicohrubec nicohrubec commented Jan 16, 2026

This PR adds automatic instrumentation for global request and function middleware in TanStack Start applications.

Overview

The sentryTanstackStart Vite plugin now automatically wraps requestMiddleware and functionMiddleware arrays in createStart() with Sentry instrumentation. This is done via a source code transformation during the build that converts:

createStart({
  requestMiddleware: [myMiddleware],
  functionMiddleware: [anotherMiddleware],
});

into:

import { wrapMiddlewaresWithSentry } from '@sentry/tanstackstart-react';

createStart({
  requestMiddleware: wrapMiddlewaresWithSentry({ myMiddleware }),
  functionMiddleware: wrapMiddlewaresWithSentry({ anotherMiddleware }),
});

Usage

Auto-instrumentation is enabled by default. To explicitly disable it:

// vite.config.ts
import { sentryTanstackStart } from '@sentry/tanstackstart-react';

export default defineConfig({
  plugins: [
    sentryTanstackStart({
      org: 'your-org',
      project: 'your-project',
      autoInstrumentMiddleware: false, // Disable auto-instrumentation
    }),
  ],
});

This should give users flexibility in case things go wrong.

Implementation Details

  • Uses a regex-based approach to transform source files containing createStart()
  • Skips transformation if the user has already manually wrapped middlewares with wrapMiddlewaresWithSentry
  • Only handles simple identifier arrays (e.g., [foo, bar]), not complex expressions

Testing

  • Updated E2E tests to use auto-instrumentation for global middleware
  • Updated unit tests for the sentryTanstackStart plugin
  • Added unit tests for autoInstrumentMiddleware

Follow-ups

Future PRs will add support for non-global request/function middleware. This PR focuses on global middleware to make it a bit easier to review. I also want to give users a bit more control by allowing them to disable auto-instrumentation on a file-level, but this doesn't make sense yet since the current implementation only patches a single file anyways.

Closes #18845

@nicohrubec nicohrubec self-assigned this Jan 16, 2026
@nicohrubec nicohrubec marked this pull request as ready for review January 16, 2026 10:00
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

@nicohrubec nicohrubec force-pushed the nh/automatic-middleware-instrumentation branch from 50e7961 to 2706bc0 Compare January 16, 2026 15:33
@github-actions
Copy link
Contributor

github-actions bot commented Jan 19, 2026

node-overhead report 🧳

Note: This is a synthetic benchmark with a minimal express app and does not necessarily reflect the real-world performance impact in an application.
⚠️ Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

Scenario Requests/s % of Baseline Prev. Requests/s Change %
GET Baseline 8,493 - 9,023 -6%
GET With Sentry 1,566 18% 1,634 -4%
GET With Sentry (error only) 5,805 68% 5,919 -2%
POST Baseline 1,159 - 1,137 +2%
POST With Sentry 559 48% 550 +2%
POST With Sentry (error only) 1,017 88% 1,035 -2%
MYSQL Baseline 3,164 - 3,156 +0%
MYSQL With Sentry 324 10% 398 -19%
MYSQL With Sentry (error only) 2,519 80% 2,578 -2%

View base workflow run

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

@github-actions
Copy link
Contributor

size-limit report 📦

⚠️ Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

Path Size % Change Change
@sentry/browser 25.11 kB - -
@sentry/browser - with treeshaking flags 23.61 kB - -
@sentry/browser (incl. Tracing) 41.87 kB - -
@sentry/browser (incl. Tracing, Profiling) 46.45 kB - -
@sentry/browser (incl. Tracing, Replay) 80.47 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 70.16 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 85.17 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 97.37 kB - -
@sentry/browser (incl. Feedback) 41.83 kB - -
@sentry/browser (incl. sendFeedback) 29.79 kB - -
@sentry/browser (incl. FeedbackAsync) 34.79 kB - -
@sentry/browser (incl. Metrics) 26.21 kB - -
@sentry/browser (incl. Logs) 26.37 kB - -
@sentry/browser (incl. Metrics & Logs) 27.02 kB - -
@sentry/react 26.84 kB - -
@sentry/react (incl. Tracing) 44.09 kB - -
@sentry/vue 29.56 kB - -
@sentry/vue (incl. Tracing) 43.67 kB - -
@sentry/svelte 25.12 kB - -
CDN Bundle 27.67 kB - -
CDN Bundle (incl. Tracing) 42.67 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 43.5 kB - -
CDN Bundle (incl. Tracing, Replay) 79.37 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 84.81 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 85.72 kB - -
CDN Bundle - uncompressed 81.08 kB - -
CDN Bundle (incl. Tracing) - uncompressed 126.5 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 129.33 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 243.03 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 255.83 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 258.64 kB - -
@sentry/nextjs (client) 46.44 kB - -
@sentry/sveltekit (client) 42.25 kB - -
@sentry/node-core 51.93 kB +0.06% +27 B 🔺
@sentry/node 162.19 kB +0.02% +30 B 🔺
@sentry/node - without tracing 93.35 kB +0.03% +21 B 🔺
@sentry/aws-serverless 108.85 kB +0.02% +21 B 🔺

View base workflow run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Auto-instrument global request/function middleware

2 participants