-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(tanstackstart-react): Add file exclude to configure middleware auto-instrumentation #19007
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
Changes from all commits
b877f51
40a7ba4
3660c72
9a8026b
e985b3e
f7f4c22
092858f
ce8dbc7
330d7f8
b262eaa
14873c3
a910011
c122987
25e86bc
1b2c92f
b94322a
e7f8e23
df42f71
40ba4ec
9912468
fc0632a
478e1a2
07f4f30
d41a450
57da296
e806bea
d9421fc
529a1e6
bfc47ee
555c71e
1778822
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,10 @@ | ||
| import { stringMatchesSomePattern } from '@sentry/core'; | ||
| import * as path from 'path'; | ||
| import type { Plugin } from 'vite'; | ||
|
|
||
| type AutoInstrumentMiddlewareOptions = { | ||
| enabled?: boolean; | ||
| debug?: boolean; | ||
| exclude?: Array<string | RegExp>; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it would make sense to add some defaults here. E.g. excluding the test files if this is a very common usecase
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll think about this some more and maybe do a follow up, for the test files for instance I think the better approach for us would be to add this to the skipped file extensions early return instead of using this option |
||
| }; | ||
|
|
||
| type WrapResult = { | ||
|
|
@@ -87,25 +89,44 @@ function applyWrap( | |
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if a file should be skipped from auto-instrumentation based on exclude patterns. | ||
| */ | ||
| export function shouldSkipFile(id: string, exclude: Array<string | RegExp> | undefined, debug: boolean): boolean { | ||
| // file doesn't match exclude patterns, don't skip | ||
| if (!exclude || exclude.length === 0 || !stringMatchesSomePattern(id, exclude)) { | ||
| return false; | ||
| } | ||
|
|
||
| // file matches exclude patterns, skip | ||
| if (debug) { | ||
| // eslint-disable-next-line no-console | ||
| console.log(`[Sentry] Skipping auto-instrumentation for excluded file: ${id}`); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. l: I think this should be rather
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this needs to be console.log since it happens during build not runtime |
||
| } | ||
| return true; | ||
| } | ||
nicohrubec marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * A Vite plugin that automatically instruments TanStack Start middlewares: | ||
| * - `requestMiddleware` and `functionMiddleware` arrays in `createStart()` | ||
| * - `middleware` arrays in `createFileRoute()` route definitions | ||
| */ | ||
| export function makeAutoInstrumentMiddlewarePlugin(options: AutoInstrumentMiddlewareOptions = {}): Plugin { | ||
| const { enabled = true, debug = false } = options; | ||
| const { debug = false, exclude } = options; | ||
|
|
||
| return { | ||
| name: 'sentry-tanstack-middleware-auto-instrument', | ||
| enforce: 'pre', | ||
|
|
||
| transform(code, id) { | ||
| if (!enabled) { | ||
| // Skip if not a TS/TSX file | ||
| const fileExtension = path.extname(id); | ||
| if (!['.ts', '.tsx'].includes(fileExtension)) { | ||
nicohrubec marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return null; | ||
| } | ||
|
|
||
| // Skip if not a TS/JS file | ||
| if (!/\.(ts|tsx|js|jsx|mjs|mts)$/.test(id)) { | ||
| // Skip if file matches exclude patterns | ||
| if (shouldSkipFile(id, exclude, debug)) { | ||
| return null; | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was an unnecessary option since we just don't add this plugin if it's not enabled, so I removed it