Sentry
Sentry - bu dasturchilarga muammolarni real vaqt rejimida aniqlash va tuzatishga yordam beradigan error tracking va performance monitoring platformasi. Ushbu recipe Sentry'ning Nes
Sentry - bu dasturchilarga muammolarni real vaqt rejimida aniqlash va tuzatishga yordam beradigan error tracking va performance monitoring platformasi. Ushbu recipe Sentry'ning NestJS SDK sini NestJS ilovangiz bilan qanday integratsiya qilishni ko'rsatadi.
O'rnatish
Avval kerakli dependency'larni o'rnating:
1$ npm install --save @sentry/nestjs @sentry/profiling-node@sentry/profiling-node ixtiyoriy, ammo performance profiling uchun tavsiya etiladi.
Asosiy sozlash
Sentry bilan ishlashni boshlash uchun instrument.ts nomli fayl yarating. U ilovangizdagi boshqa modullardan oldin import qilinishi kerak:
1const Sentry = require("@sentry/nestjs");
2const { nodeProfilingIntegration } = require("@sentry/profiling-node");
3
4// Ensure to call this before requiring any other modules!
5Sentry.init({
6 dsn: SENTRY_DSN,
7 integrations: [
8 // Add our Profiling integration
9 nodeProfilingIntegration(),
10 ],
11
12 // Add Tracing by setting tracesSampleRate
13 // We recommend adjusting this value in production
14 tracesSampleRate: 1.0,
15
16 // Set sampling rate for profiling
17 // This is relative to tracesSampleRate
18 profilesSampleRate: 1.0,
19});main.ts faylini instrument.ts boshqa import'lardan oldin yuklanadigan qilib yangilang:
1// Import this first!
2import "./instrument";
3
4// Now import other modules
5import { NestFactory } from "@nestjs/core";
6import { AppModule } from "./app.module";
7
8async function bootstrap() {
9 const app = await NestFactory.create(AppModule);
10 await app.listen(3000);
11}
12
13bootstrap();Shundan so'ng SentryModule ni asosiy modulingizga root module sifatida qo'shing:
1import { Module } from "@nestjs/common";
2import { SentryModule } from "@sentry/nestjs/setup";
3import { AppController } from "./app.controller";
4import { AppService } from "./app.service";
5
6@Module({
7 imports: [
8 SentryModule.forRoot(),
9 // ...other modules
10 ],
11 controllers: [AppController],
12 providers: [AppService],
13})
14export class AppModule {}Exception handling
Agar siz global catch-all exception filter ishlatayotgan bo'lsangiz (app.useGlobalFilters() orqali ro'yxatdan o'tgan filter yoki app module providers ichida argumentlarsiz @Catch() bilan belgilangan filter), filter'ning catch() metodiga @SentryExceptionCaptured() dekoratorini qo'shing. Bu dekorator global error filter'ingiz ushlagan barcha kutilmagan xatolarni Sentry'ga yuboradi:
1import { Catch, ExceptionFilter } from '@nestjs/common';
2import { SentryExceptionCaptured } from '@sentry/nestjs';
3
4@Catch()
5export class YourCatchAllExceptionFilter implements ExceptionFilter {
6 @SentryExceptionCaptured()
7 catch(exception, host): void {
8 // your implementation here
9 }
10}Standart holatda faqat error filter tomonidan ushlanmagan unhandled exception'lar Sentry'ga yuboriladi. HttpExceptions (jumladan undan meros oluvchilar) ham standart bo'yicha yuborilmaydi, chunki ular ko'pincha control flow mexanizmi sifatida ishlaydi.
Agar sizda global catch-all exception filter bo'lmasa, SentryGlobalFilter ni asosiy modulingizning providers qismiga qo'shing. Bu filter boshqa error filter'lar ushlamagan barcha unhandled xatolarni Sentry'ga yuboradi.
SentryGlobalFilter boshqa exception filter'lardan oldin ro'yxatdan o'tkazilishi kerak.
1import { Module } from "@nestjs/common";
2import { APP_FILTER } from "@nestjs/core";
3import { SentryGlobalFilter } from "@sentry/nestjs/setup";
4
5@Module({
6 providers: [
7 {
8 provide: APP_FILTER,
9 useClass: SentryGlobalFilter,
10 },
11 // ..other providers
12 ],
13})
14export class AppModule {}O'qilishi qulay stack trace'lar
Loyihangiz qanday sozlanganiga qarab, Sentry'dagi stack trace'lar haqiqiy kodingizga mos kelmasligi mumkin.
Buni tuzatish uchun source map'larni Sentry'ga yuklang. Eng oson yo'l - Sentry Wizard'dan foydalanish:
1npx @sentry/wizard@latest -i sourcemapsIntegratsiyani tekshirish
Sentry integratsiyasi ishlayotganini tekshirish uchun xato tashlaydigan test endpoint qo'shishingiz mumkin:
1@Get("debug-sentry")
2getError() {
3 throw new Error("My first Sentry error!");
4}Ilovangizda /debug-sentry ga o'ting va xato Sentry dashboard'da paydo bo'lishi kerak.
Xulosa
Sentry'ning NestJS SDK'i bo'yicha to'liq hujjatlar, jumladan ilg'or konfiguratsiya opsiyalari va imkoniyatlar uchun rasmiy Sentry hujjatlari ni ko'ring.
Dasturiy xatolarni topish Sentry'ning ishi bo'lsa ham, ularni baribir biz yozamiz. Agar SDK'ni o'rnatish vaqtida muammoga duch kelsangiz, GitHub Issue oching yoki Discord orqali murojaat qiling.