-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(sveltekit): Add wrapServerRouteWithSentry wrapper
#13247
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 3 commits
Commits
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
7 changes: 7 additions & 0 deletions
7
...ackages/e2e-tests/test-applications/sveltekit-2/src/routes/wrap-server-route/+page.svelte
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,7 @@ | ||
| <script lang="ts"> | ||
| export let data; | ||
| </script> | ||
|
|
||
| <p> | ||
| Message from API: {data.myMessage} | ||
| </p> |
7 changes: 7 additions & 0 deletions
7
dev-packages/e2e-tests/test-applications/sveltekit-2/src/routes/wrap-server-route/+page.ts
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,7 @@ | ||
| export const load = async ({ fetch }) => { | ||
| const res = await fetch('/wrap-server-route/api'); | ||
| const myMessage = await res.json(); | ||
| return { | ||
| myMessage: myMessage.myMessage, | ||
| }; | ||
| }; |
6 changes: 6 additions & 0 deletions
6
...kages/e2e-tests/test-applications/sveltekit-2/src/routes/wrap-server-route/api/+server.ts
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,6 @@ | ||
| import { wrapServerRouteWithSentry } from '@sentry/sveltekit'; | ||
| import { error } from '@sveltejs/kit'; | ||
|
|
||
| export const GET = wrapServerRouteWithSentry(async () => { | ||
| error(500, 'error() error'); | ||
| }); |
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 |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, startSpan } from '@sentry/node'; | ||
| import { addNonEnumerableProperty } from '@sentry/utils'; | ||
| import type { RequestEvent } from '@sveltejs/kit'; | ||
| import { flushIfServerless, sendErrorToSentry } from './utils'; | ||
|
|
||
| type PatchedServerRouteEvent = RequestEvent & { __sentry_wrapped__?: boolean }; | ||
|
|
||
| /** | ||
| * Wraps a server route handler for API or server routes registered in `+server.(js|js)` files. | ||
| * | ||
| * This function will automatically capture any errors that occur during the execution of the route handler | ||
| * and it will start a span for the duration of your route handler. | ||
| * | ||
| * @example | ||
| * ```js | ||
| * import { wrapServerRouteWithSentry } from '@sentry/sveltekit'; | ||
| * | ||
| * const get = async event => { | ||
| * return new Response(JSON.stringify({ message: 'hello world' })); | ||
| * } | ||
| * | ||
| * export const GET = wrapServerRouteWithSentry(get); | ||
| * ``` | ||
| * | ||
| * @param originalRouteHandler your server route handler | ||
| * @param httpMethod the HTTP method of your route handler | ||
| * | ||
| * @returns a wrapped version of your server route handler | ||
| */ | ||
| export function wrapServerRouteWithSentry( | ||
| originalRouteHandler: (request: RequestEvent) => Promise<Response>, | ||
| ): (requestEvent: RequestEvent) => Promise<Response> { | ||
| return new Proxy(originalRouteHandler, { | ||
| apply: async (wrappingTarget, thisArg, args) => { | ||
| const event = args[0] as PatchedServerRouteEvent; | ||
| const routeId = event.route && event.route.id; | ||
| const httpMethod = event.request.method; | ||
|
|
||
| if (event.__sentry_wrapped__) { | ||
| return wrappingTarget.apply(thisArg, args); | ||
| } | ||
|
|
||
| addNonEnumerableProperty(event as unknown as Record<string, unknown>, '__sentry_wrapped__', true); | ||
|
|
||
| try { | ||
| return await startSpan( | ||
| { | ||
| name: `${httpMethod} ${routeId || 'Server Route'}`, | ||
| op: `function.sveltekit.server.${httpMethod.toLowerCase()}`, | ||
| attributes: { | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.sveltekit', | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route', | ||
| }, | ||
| onlyIfParent: true, | ||
| }, | ||
| () => wrappingTarget.apply(thisArg, args), | ||
| ); | ||
| } catch (e) { | ||
| sendErrorToSentry(e, 'serverRoute'); | ||
| throw e; | ||
| } finally { | ||
| await flushIfServerless(); | ||
| } | ||
| }, | ||
| }); | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.