Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nextjs): Add
excludeServerRoutes
config option (#6207)
Currently, in the nextjs SDK, we inject the user's `Sentry.init()` code (by way of their `sentry.server.config.js` file) into all serverside routes. This adds a new option to the `sentry` object in `next.config.js` which allows users to prevent specific routes from being instrumented in this way. In this option, excluded routes can be specified using either strings (which need to exactly match the route) or regexes. Note: Heavily inspired by #6125. h/t to @lforst for his work there. Compared to that PR, this one allows non-API routes to be excluded and allows excluded pages to be specified as routes rather than filepaths. (Using routes a) obviates the need for users to add `pages/` to the beginning of every entry, b) abstracts away the differences between windows and POSIX paths, and c) futureproofs users' config values against underlying changes to project file organization.) Docs for this feature are being added in getsentry/sentry-docs#5789. Fixes #6119. Fixes #5964.
- Loading branch information
1 parent
66bcbd7
commit fd6ae0f
Showing
10 changed files
with
163 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
packages/nextjs/test/integration/pages/api/excludedEndpoints/excludedWithRegExp.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// This file will test the `excludeServerRoutes` option when a route is provided as a RegExp. | ||
const handler = async (): Promise<void> => { | ||
throw new Error('API Error'); | ||
}; | ||
|
||
export default handler; |
6 changes: 6 additions & 0 deletions
6
packages/nextjs/test/integration/pages/api/excludedEndpoints/excludedWithString.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// This file will test the `excludeServerRoutes` option when a route is provided as a string. | ||
const handler = async (): Promise<void> => { | ||
throw new Error('API Error'); | ||
}; | ||
|
||
export default handler; |
67 changes: 67 additions & 0 deletions
67
packages/nextjs/test/integration/test/server/excludedApiEndpoints.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const assert = require('assert'); | ||
|
||
const { sleep } = require('../utils/common'); | ||
const { getAsync, interceptEventRequest, interceptTracingRequest } = require('../utils/server'); | ||
|
||
module.exports = async ({ url: urlBase, argv }) => { | ||
const regExpUrl = `${urlBase}/api/excludedEndpoints/excludedWithRegExp`; | ||
const stringUrl = `${urlBase}/api/excludedEndpoints/excludedWithString`; | ||
|
||
const capturedRegExpErrorRequest = interceptEventRequest( | ||
{ | ||
exception: { | ||
values: [ | ||
{ | ||
type: 'Error', | ||
value: 'API Error', | ||
}, | ||
], | ||
}, | ||
tags: { | ||
runtime: 'node', | ||
}, | ||
request: { | ||
url: regExpUrl, | ||
method: 'GET', | ||
}, | ||
transaction: 'GET /api/excludedEndpoints/excludedWithRegExp', | ||
}, | ||
argv, | ||
'excluded API endpoint via RegExp', | ||
); | ||
|
||
const capturedStringErrorRequest = interceptEventRequest( | ||
{ | ||
exception: { | ||
values: [ | ||
{ | ||
type: 'Error', | ||
value: 'API Error', | ||
}, | ||
], | ||
}, | ||
tags: { | ||
runtime: 'node', | ||
}, | ||
request: { | ||
url: regExpUrl, | ||
method: 'GET', | ||
}, | ||
transaction: 'GET /api/excludedEndpoints/excludedWithString', | ||
}, | ||
argv, | ||
'excluded API endpoint via String', | ||
); | ||
|
||
await Promise.all([getAsync(regExpUrl), getAsync(stringUrl)]); | ||
await sleep(250); | ||
|
||
assert.ok( | ||
!capturedRegExpErrorRequest.isDone(), | ||
'Did intercept error request even though route should be excluded (RegExp)', | ||
); | ||
assert.ok( | ||
!capturedStringErrorRequest.isDone(), | ||
'Did intercept error request even though route should be excluded (String)', | ||
); | ||
}; |