UNPKG

@compas/server

Version:

Koa server and common middleware

50 lines (44 loc) 1.38 kB
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 (ctx, next) => { const method = ctx.method.toLowerCase(); if (method === "options" || method === "head") { return next(); } if (!_compasSentryExport) { return next(); } const _sentry = _compasSentryExport; return _sentry.withIsolationScope(async () => { return await _sentry.startSpanManual( { op: "function", // We rename this when ending the request to the route name. name: `${ctx.method} ${ctx.url}`, }, async () => { return await next(); }, ); }); }; }