Health Checks
Muammodan boshlaylik
Serveringiz ishlayapti — lekin ma'lumotlar bazasiga ulanolmayapti. Yoki Redis to'lib ketgan. Foydalanuvchilar xato ko'ryapti, lekin siz bilmayapsiz.
Docker yoki load balancer serveringiz "sog'lom" ekanini bilishi kerak. Agar sog'lom bo'lmasa — trafikni boshqa serverga yo'naltiradi yoki konteyner qayta ishga tushiriladi.
Buning uchun GET /health endpoint kerak.
Health Check nima?
Health Check — "server sog'lommi?" degan savolga javob beradigan endpoint.
Hayotiy o'xshatma
Tibbiy ko'rik. Shifokor tekshiradi: qon bosimi normal, yurak urishi yaxshi, o'pka tozami? Hammasi yaxshi — "sog'lom". Biror ko'rsatkich noto'g'ri — muammo bor.
Server uchun ham xuddi shunday: ma'lumotlar bazasi, Redis, disk bo'sh joy — barchasi tekshiriladi.
O'rnatish
npm install @nestjs/terminus
npm install @nestjs/axios axios
Fayl tuzilmasi
src/
health/
health.controller.ts ← /health endpoint
health.module.ts
Asosiy sozlash
// health/health.module.ts
import { Module } from '@nestjs/common';
import { TerminusModule } from '@nestjs/terminus';
import { HttpModule } from '@nestjs/axios';
import { HealthController } from './health.controller';
@Module({
imports: [TerminusModule, HttpModule],
controllers: [HealthController],
})
export class HealthModule {}
// health/health.controller.ts
import { Controller, Get } from '@nestjs/common';
import {
HealthCheck,
HealthCheckService,
TypeOrmHealthIndicator,
HttpHealthIndicator,
} from '@nestjs/terminus';
@Controller('health')
export class HealthController {
constructor(
private health: HealthCheckService,
private db: TypeOrmHealthIndicator,
private http: HttpHealthIndicator,
) {}
@Get()
@HealthCheck()
check() {
return this.health.check([
() => this.db.pingCheck('database'), // ← DB ulangan mi?
() => this.http.pingCheck('google', 'https://google.com'), // ← internet bormi?
]);
}
}
Natija
Hammasi yaxshi bo'lsa (HTTP 200):
{
"status": "ok",
"info": {
"database": { "status": "up" },
"google": { "status": "up" }
}
}
Biror narsa ishlamasa (HTTP 503):
{
"status": "error",
"error": {
"database": { "status": "down", "message": "Connection refused" }
}
}
Disk va RAM tekshirish
import { DiskHealthIndicator, MemoryHealthIndicator } from '@nestjs/terminus';
@Controller('health')
export class HealthController {
constructor(
private health: HealthCheckService,
private db: TypeOrmHealthIndicator,
private disk: DiskHealthIndicator,
private memory: MemoryHealthIndicator,
) {}
@Get()
@HealthCheck()
check() {
return this.health.check([
() => this.db.pingCheck('database'),
// Disk 90% dan ko'p to'lsa — xato
() => this.disk.checkStorage('storage', { path: '/', thresholdPercent: 0.9 }),
// RAM 300MB dan ko'p ishlatilsa — xato
() => this.memory.checkHeap('memory', 300 * 1024 * 1024),
]);
}
}
Nima uchun 503?
HTTP 503 "Service Unavailable" — server hozir ishlay olmaydi. Docker, load balancer va monitoring tizimlari aynan shu status kodni tekshiradi.
200 OK → hammasi yaxshi, so'rovlarni yuboravering
503 → muammo bor, trafikni boshqa serverga yo'naltiring
Tez-tez uchraydigan xatolar
1. TypeORM indicator ishlamayapti
TypeOrmHealthIndicator provider not found
Sababi: TypeORM ishlatilmayapti yoki TypeOrmModule AppModule ga qo'shilmagan.
Yechim: prisma.pingCheck() yoki mongoose.pingCheck() ishlatilsa — shunga mos indicator qo'ying.
2. HealthModule AppModule ga qo'shilmagan
Yechim: app.module.ts imports ga HealthModule qo'shing.
Xulosa
/healthendpoint — server sog'lomligini ko'rsatadi200 OK— hammasi yaxshi,503— muammo bor- Database, disk, RAM, tashqi API — barchasini tekshirish mumkin
- Docker/monitoring tizimi shu endpointni tekshirib turadi
Keyingi dars: 47-dars: i18n — Internationalization