-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sveltekit): Add
wrapServerRouteWithSentry
wrapper (#13247)
Add a wrapper for SvelteKit server routes. The reason is that some errors (e.g. sveltekit `error()` calls) are not caught within server (API) routes, as reported in #13224 because in contrast to `load` function we don't directly try/catch the function invokation. For now, users will have to add this wrapper manually. At a later time we can think about auto instrumentation, similarly to `load` functions but for now this will remain manual.
- Loading branch information
Showing
11 changed files
with
307 additions
and
79 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
7 changes: 7 additions & 0 deletions
7
...ackages/e2e-tests/test-applications/sveltekit-2/src/routes/wrap-server-route/+page.svelte
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,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 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 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
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; | ||
|
||
if (event.__sentry_wrapped__) { | ||
return wrappingTarget.apply(thisArg, args); | ||
} | ||
|
||
const routeId = event.route && event.route.id; | ||
const httpMethod = event.request.method; | ||
|
||
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 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.