perf: lazyload sentry (#22249)
This commit is contained in:
parent
9edb8850b8
commit
7c229947f7
|
|
@ -1,5 +1,4 @@
|
|||
import Honeybadger from '@honeybadger-io/js';
|
||||
import * as Sentry from '@sentry/node';
|
||||
import type { ErrorHandler, NotFoundHandler } from 'hono';
|
||||
import { routePath } from 'hono/route';
|
||||
|
||||
|
|
@ -11,6 +10,8 @@ import Error from '@/views/error';
|
|||
|
||||
import NotFoundError from './types/not-found';
|
||||
|
||||
const Sentry = config.sentry.dsn ? await import('@sentry/node') : undefined;
|
||||
|
||||
export const errorHandler: ErrorHandler = (error, ctx) => {
|
||||
const requestPath = ctx.req.path;
|
||||
const matchedRoute = routePath(ctx);
|
||||
|
|
@ -43,7 +44,7 @@ export const errorHandler: ErrorHandler = (error, ctx) => {
|
|||
});
|
||||
}
|
||||
|
||||
if (config.sentry.dsn) {
|
||||
if (Sentry) {
|
||||
Sentry.withScope((scope) => {
|
||||
scope.setTag('name', requestPath.split('/', 2)[1]);
|
||||
Sentry.captureException(error);
|
||||
|
|
|
|||
|
|
@ -47,6 +47,25 @@ describe('sentry middleware', () => {
|
|||
return { middleware, sentry, logger, scope, getRouteNameFromPath };
|
||||
};
|
||||
|
||||
it('does not load sentry when dsn is not configured', async () => {
|
||||
const sentryFactory = vi.fn(() => ({ init: vi.fn() }));
|
||||
vi.doMock('@sentry/node', sentryFactory);
|
||||
vi.doMock('@/config', () => ({
|
||||
config: {
|
||||
sentry: {
|
||||
dsn: '',
|
||||
},
|
||||
errorTrackingRouteTimeout: 50,
|
||||
nodeName: 'node-a',
|
||||
},
|
||||
}));
|
||||
|
||||
const { default: middleware } = await import('@/middleware/sentry');
|
||||
await middleware({ req: { path: '/test/slow' } } as any, async () => {});
|
||||
|
||||
expect(sentryFactory).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('initializes sentry and captures slow routes', async () => {
|
||||
const { middleware, sentry, logger, scope, getRouteNameFromPath } = await loadMiddleware();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,11 +1,14 @@
|
|||
import * as Sentry from '@sentry/node';
|
||||
import type * as SentryType from '@sentry/node';
|
||||
import type { MiddlewareHandler } from 'hono';
|
||||
|
||||
import { config } from '@/config';
|
||||
import { getRouteNameFromPath } from '@/utils/helpers';
|
||||
import logger from '@/utils/logger';
|
||||
|
||||
let Sentry: typeof SentryType | undefined;
|
||||
|
||||
if (config.sentry.dsn) {
|
||||
Sentry = await import('@sentry/node');
|
||||
Sentry.init({
|
||||
dsn: config.sentry.dsn,
|
||||
});
|
||||
|
|
@ -17,10 +20,10 @@ if (config.sentry.dsn) {
|
|||
const middleware: MiddlewareHandler = async (ctx, next) => {
|
||||
const time = Date.now();
|
||||
await next();
|
||||
if (config.sentry.dsn && Date.now() - time >= config.errorTrackingRouteTimeout) {
|
||||
if (Sentry && Date.now() - time >= config.errorTrackingRouteTimeout) {
|
||||
Sentry.withScope((scope) => {
|
||||
scope.setTag('name', getRouteNameFromPath(ctx.req.path));
|
||||
Sentry.captureException(new Error('Route Timeout'));
|
||||
Sentry!.captureException(new Error('Route Timeout'));
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue