-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(astro): Automatically add Sentry middleware in Astro integration #9532
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -14,7 +14,7 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => { | |
name: PKG_NAME, | ||
hooks: { | ||
// eslint-disable-next-line complexity | ||
'astro:config:setup': async ({ updateConfig, injectScript, config }) => { | ||
'astro:config:setup': async ({ updateConfig, injectScript, addMiddleware, config }) => { | ||
// The third param here enables loading of all env vars, regardless of prefix | ||
// see: https://main.vitejs.dev/config/#using-environment-variables-in-config | ||
|
||
|
@@ -73,6 +73,20 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => { | |
options.debug && console.log('[sentry-astro] Using default server init.'); | ||
injectScript('page-ssr', buildServerSnippet(options || {})); | ||
} | ||
|
||
const isSSR = config && (config.output === 'server' || config.output === 'hybrid'); | ||
const shouldAddMiddleware = options.autoInstrumentation?.requestHandler !== false; | ||
|
||
// Guarding calling the addMiddleware function because it was only introduced in [email protected] | ||
// Users on older versions of astro will need to add the middleware manually. | ||
const supportsAddMiddleware = typeof addMiddleware === 'function'; | ||
|
||
if (supportsAddMiddleware && isSSR && shouldAddMiddleware) { | ||
addMiddleware({ | ||
order: 'pre', | ||
entrypoint: '@sentry/astro/middleware', | ||
}); | ||
} | ||
}, | ||
}, | ||
}; | ||
|
This file contains hidden or 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,16 @@ | ||
import type { MiddlewareResponseHandler } from 'astro'; | ||
|
||
import { handleRequest } from '../../server/middleware'; | ||
|
||
/** | ||
* This export is used by our integration to automatically add the middleware | ||
* to astro ^3.5.0 projects. | ||
* | ||
* It's not possible to pass options at this moment, so we'll call our middleware | ||
* factory function with the default options. Users can deactiveate the automatic | ||
* middleware registration in our integration and manually add it in their own | ||
* `/src/middleware.js` file. | ||
*/ | ||
export const onRequest: MiddlewareResponseHandler = (ctx, next) => { | ||
return handleRequest()(ctx, next); | ||
}; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,33 @@ | ||
import { vi } from 'vitest'; | ||
|
||
import { onRequest } from '../../../src/integration/middleware'; | ||
|
||
vi.mock('../../../src/server/meta', () => ({ | ||
getTracingMetaTags: () => ({ | ||
sentryTrace: '<meta name="sentry-trace" content="123">', | ||
baggage: '<meta name="baggage" content="abc">', | ||
}), | ||
})); | ||
|
||
describe('Integration middleware', () => { | ||
it('exports an onRequest middleware request handler', async () => { | ||
expect(typeof onRequest).toBe('function'); | ||
|
||
const next = vi.fn().mockReturnValue(Promise.resolve(new Response(null, { status: 200, headers: new Headers() }))); | ||
const ctx = { | ||
request: { | ||
method: 'GET', | ||
url: '/users/123/details', | ||
headers: new Headers(), | ||
}, | ||
url: new URL('https://myDomain.io/users/123/details'), | ||
params: { | ||
id: '123', | ||
}, | ||
}; | ||
// @ts-expect-error - a partial ctx object is fine here | ||
const res = await onRequest(ctx, next); | ||
|
||
expect(res).toBeDefined(); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Just thinking, will this break if people update the SDK & astro to the latest version and already have the middleware added manually - e.g. will it then add the middleware twice? or do middlewares (or our middleware) handle this scenario?
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.
Good point!
Technically, we're still in alpha so I'd be fine with this. Furthermore, middleware wasn't yet documented anywhere except for the readme.
However, for general robustness, we can probably detect and handle double wrapping by writing a flag to the
locals
object that's passed into the middleware handlers. I'll check take a look.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.
if this is not documented yet, IMHO let's just break it! no need to introduce additional complexity then 👍