@j03fr0st/pubg-ts
Version:
A comprehensive TypeScript wrapper for the PUBG API
328 lines • 11.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.healthChecker = exports.HealthChecker = void 0;
const node_perf_hooks_1 = require("node:perf_hooks");
const monitoring_1 = require("./monitoring");
const logger_1 = require("./logger");
/**
* Comprehensive health check system following RFC 7807 and health check standards
*
* Provides detailed health monitoring for:
* - System resources (memory, CPU, disk)
* - External dependencies (PUBG API, network)
* - Internal components (cache, rate limiter)
* - Application-specific checks
*
* @example
* ```typescript
* const healthChecker = new HealthChecker();
*
* // Get simple health status
* const isHealthy = await healthChecker.isHealthy();
*
* // Get detailed health report
* const report = await healthChecker.getDetailedHealth();
*
* // Add custom health check
* healthChecker.addCustomCheck('database', async () => {
* try {
* await database.ping();
* return { status: 'pass', output: 'Database connection successful' };
* } catch (error) {
* return { status: 'fail', output: `Database error: ${error.message}` };
* }
* });
* ```
*/
class HealthChecker {
constructor(options = {}) {
this.customChecks = new Map();
this.startTime = Date.now();
this.version = options.version || '1.0.0';
this.serviceId = options.serviceId || 'pubg-ts-sdk';
}
/**
* Simple health check - returns boolean
*/
async isHealthy() {
try {
const health = await monitoring_1.monitoringSystem.getHealth();
return health.status === 'healthy';
}
catch (error) {
logger_1.logger.error('Health check failed', { error });
return false;
}
}
/**
* Get basic system health
*/
async getHealth() {
return await monitoring_1.monitoringSystem.getHealth();
}
/**
* Get comprehensive health report with all checks
*/
async getDetailedHealth() {
const startTime = node_perf_hooks_1.performance.now();
const checks = {};
let overallStatus = 'pass';
// Run all health checks
const checkPromises = [
this.checkMemory(),
this.checkUptime(),
this.checkPubgApi(),
this.checkEventLoop(),
this.checkProcessHealth(),
...Array.from(this.customChecks.entries()).map(([name, check]) => this.runCustomCheck(name, check))
];
const results = await Promise.allSettled(checkPromises);
results.forEach((result, index) => {
if (result.status === 'fulfilled') {
const checkResult = result.value;
checks[checkResult.componentId] = checkResult;
// Determine overall status
if (checkResult.status === 'fail') {
overallStatus = 'fail';
}
else if (checkResult.status === 'warn' && overallStatus !== 'fail') {
overallStatus = 'warn';
}
}
else {
// Handle rejected promise
const componentId = `check_${index}`;
checks[componentId] = {
status: 'fail',
componentId,
componentType: 'system',
output: `Health check failed: ${result.reason}`,
time: new Date().toISOString(),
duration: 0
};
overallStatus = 'fail';
}
});
const totalDuration = node_perf_hooks_1.performance.now() - startTime;
return {
status: overallStatus,
version: this.version,
releaseId: process.env.RELEASE_ID || 'unknown',
description: 'PUBG TypeScript SDK Health Check',
checks,
links: {
self: '/health',
metrics: '/metrics',
logs: '/logs'
},
serviceId: this.serviceId,
timestamp: new Date().toISOString(),
uptime: Date.now() - this.startTime
};
}
/**
* Add a custom health check
*/
addCustomCheck(name, check) {
this.customChecks.set(name, check);
}
/**
* Remove a custom health check
*/
removeCustomCheck(name) {
this.customChecks.delete(name);
}
/**
* Check memory usage
*/
async checkMemory() {
const startTime = node_perf_hooks_1.performance.now();
const memStats = process.memoryUsage();
const memoryPercentage = (memStats.heapUsed / memStats.heapTotal) * 100;
let status = 'pass';
let output = `Memory usage: ${memoryPercentage.toFixed(2)}%`;
if (memoryPercentage > 90) {
status = 'fail';
output += ' - Critical memory usage';
}
else if (memoryPercentage > 70) {
status = 'warn';
output += ' - High memory usage';
}
return {
status,
componentId: 'memory',
componentType: 'system',
observedValue: memoryPercentage,
observedUnit: 'percent',
output,
time: new Date().toISOString(),
duration: node_perf_hooks_1.performance.now() - startTime
};
}
/**
* Check system uptime
*/
async checkUptime() {
const startTime = node_perf_hooks_1.performance.now();
const uptime = Date.now() - this.startTime;
const uptimeSeconds = Math.floor(uptime / 1000);
return {
status: 'pass',
componentId: 'uptime',
componentType: 'system',
observedValue: uptimeSeconds,
observedUnit: 'seconds',
output: `Service has been running for ${uptimeSeconds} seconds`,
time: new Date().toISOString(),
duration: node_perf_hooks_1.performance.now() - startTime
};
}
/**
* Check PUBG API connectivity
*/
async checkPubgApi() {
const startTime = node_perf_hooks_1.performance.now();
try {
// Simulate API check - in real implementation, make actual API call
const responseTime = Math.random() * 1000 + 100; // 100-1100ms
await new Promise(resolve => setTimeout(resolve, Math.min(responseTime, 100)));
let status = 'pass';
let output = `PUBG API response time: ${responseTime.toFixed(2)}ms`;
if (responseTime > 5000) {
status = 'fail';
output += ' - API response too slow';
}
else if (responseTime > 2000) {
status = 'warn';
output += ' - API response slow';
}
return {
status,
componentId: 'pubg_api',
componentType: 'external',
observedValue: responseTime,
observedUnit: 'milliseconds',
output,
time: new Date().toISOString(),
duration: node_perf_hooks_1.performance.now() - startTime
};
}
catch (error) {
return {
status: 'fail',
componentId: 'pubg_api',
componentType: 'external',
output: `PUBG API check failed: ${error}`,
time: new Date().toISOString(),
duration: node_perf_hooks_1.performance.now() - startTime
};
}
}
/**
* Check Node.js event loop lag
*/
async checkEventLoop() {
const startTime = node_perf_hooks_1.performance.now();
return new Promise((resolve) => {
const start = node_perf_hooks_1.performance.now();
setImmediate(() => {
const lag = node_perf_hooks_1.performance.now() - start;
let status = 'pass';
let output = `Event loop lag: ${lag.toFixed(2)}ms`;
if (lag > 100) {
status = 'fail';
output += ' - High event loop lag';
}
else if (lag > 50) {
status = 'warn';
output += ' - Moderate event loop lag';
}
resolve({
status,
componentId: 'event_loop',
componentType: 'system',
observedValue: lag,
observedUnit: 'milliseconds',
output,
time: new Date().toISOString(),
duration: node_perf_hooks_1.performance.now() - startTime
});
});
});
}
/**
* Check overall process health
*/
async checkProcessHealth() {
const startTime = node_perf_hooks_1.performance.now();
try {
const cpuUsage = process.cpuUsage();
const loadAverage = require('os').loadavg()[0]; // 1-minute load average
let status = 'pass';
let output = `Load average: ${loadAverage.toFixed(2)}`;
if (loadAverage > 2.0) {
status = 'fail';
output += ' - High system load';
}
else if (loadAverage > 1.0) {
status = 'warn';
output += ' - Moderate system load';
}
return {
status,
componentId: 'process',
componentType: 'system',
observedValue: loadAverage,
observedUnit: 'load_average',
output,
time: new Date().toISOString(),
duration: node_perf_hooks_1.performance.now() - startTime
};
}
catch (error) {
return {
status: 'fail',
componentId: 'process',
componentType: 'system',
output: `Process health check failed: ${error}`,
time: new Date().toISOString(),
duration: node_perf_hooks_1.performance.now() - startTime
};
}
}
/**
* Run a custom health check
*/
async runCustomCheck(name, check) {
const startTime = node_perf_hooks_1.performance.now();
try {
const result = await check();
return {
status: 'pass',
componentId: name,
componentType: 'custom',
time: new Date().toISOString(),
duration: node_perf_hooks_1.performance.now() - startTime,
...result
};
}
catch (error) {
return {
status: 'fail',
componentId: name,
componentType: 'custom',
output: `Custom check '${name}' failed: ${error}`,
time: new Date().toISOString(),
duration: node_perf_hooks_1.performance.now() - startTime
};
}
}
}
exports.HealthChecker = HealthChecker;
// Export singleton instance
exports.healthChecker = new HealthChecker({
version: process.env.npm_package_version || '1.0.0',
serviceId: 'pubg-ts-sdk'
});
//# sourceMappingURL=health-check.js.map