-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): add Sentry support to the Koa instance returned by `get…
…App`
- Loading branch information
Showing
5 changed files
with
98 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { _compasSentryExport } from "@compas/stdlib"; | ||
|
||
/** | ||
* Sentry support; | ||
* - Starts a new root span for each incoming request. | ||
* - Tries to name it based on the finalized name of `ctx.event`. | ||
* This is most likely in the format `router.foo.bar` for matched routes by the | ||
* generated router. | ||
* - Uses the sentry-trace header when provided. | ||
* Note that if a custom list of `allowHeaders` is provided in the CORS options, | ||
* 'sentry-trace' and 'baggage' should be allowed as well. | ||
* - If the error handler retrieves an unknown or AppError.serverError, it is reported as | ||
* an uncaught exception. | ||
* | ||
* @returns {import("koa").Middleware} | ||
*/ | ||
export function sentry() { | ||
if (!_compasSentryExport) { | ||
return (ctx, next) => { | ||
return next(); | ||
}; | ||
} | ||
|
||
return async (ctx, next) => { | ||
let traceParentData; | ||
if (ctx.request.get("sentry-trace")) { | ||
// @ts-expect-error | ||
traceParentData = _compasSentryExport.extractTraceparentData( | ||
ctx.request.get("sentry-trace"), | ||
); | ||
} | ||
|
||
// @ts-expect-error | ||
return await _compasSentryExport.startSpanManual( | ||
{ | ||
op: "http", | ||
name: "http", | ||
...traceParentData, | ||
}, | ||
async () => { | ||
return await next(); | ||
}, | ||
); | ||
}; | ||
} |