UNPKG

@texagon/nestjs-health

Version:

A comprehensive health monitoring package for NestJS applications with metrics and Kubernetes-ready health checks

78 lines 3.48 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var HealthService_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.HealthService = void 0; const common_1 = require("@nestjs/common"); const terminus_1 = require("@nestjs/terminus"); const os = require("os"); let HealthService = HealthService_1 = class HealthService extends terminus_1.HealthIndicator { constructor() { super(); this.logger = new common_1.Logger(HealthService_1.name); this.isShuttingDown = false; this.memoryThreshold = 0.9; this.cpuThreshold = 0.95; this.uptimeMinThreshold = 10; process.on("SIGTERM", () => { this.logger.log("SIGTERM received, marking service as unhealthy for graceful shutdown"); this.isShuttingDown = true; }); } async isHealthy() { const health = await this.checkSystemHealth(); const isHealthy = health.serviceHealthy && (health.memoryHealthy || health.cpuHealthy); const result = this.getStatus("service", isHealthy, { version: process.env.npm_package_version || "0.0.1", timestamp: new Date().toISOString(), memoryHealthy: health.memoryHealthy, cpuHealthy: health.cpuHealthy, diskHealthy: health.diskHealthy, }); if (isHealthy) { return result; } throw new terminus_1.HealthCheckError("Health check failed", result); } async checkSystemHealth() { const serviceHealthy = !this.isShuttingDown; const memoryUsage = process.memoryUsage(); const totalMemory = os.totalmem(); const memoryHealthy = memoryUsage.rss / totalMemory < this.memoryThreshold; const cpuLoad = os.loadavg()[0] / os.cpus().length; const cpuHealthy = cpuLoad < this.cpuThreshold; const diskHealthy = true; return { memoryHealthy, cpuHealthy, diskHealthy, serviceHealthy, }; } async checkDependencies() { const isHealthy = true; return this.getStatus("dependencies", isHealthy); } async getMemoryStats() { const memoryUsage = process.memoryUsage(); return this.getStatus("memory", true, { heapUsed: Math.round(memoryUsage.heapUsed / 1024 / 1024) + "MB", heapTotal: Math.round(memoryUsage.heapTotal / 1024 / 1024) + "MB", rss: Math.round(memoryUsage.rss / 1024 / 1024) + "MB", }); } }; exports.HealthService = HealthService; exports.HealthService = HealthService = HealthService_1 = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", []) ], HealthService); //# sourceMappingURL=health.service.js.map