@merncloud/nodejs-monitoring
Version:
A comprehensive monitoring service for Node.js applications with built-in health probes and metrics collection
107 lines • 3.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BuiltInProbes = void 0;
const SystemMetrics_1 = require("../collectors/SystemMetrics");
class BuiltInProbes {
static createMemoryProbe(threshold = 80) {
return {
name: "memory_usage",
check: async () => {
const metrics = SystemMetrics_1.SystemMetrics.getInstance().getMemoryMetrics();
const usage = metrics.percentage;
if (usage > threshold) {
return {
status: "critical",
message: `Memory usage is ${usage}% (threshold: ${threshold}%)`,
value: usage,
};
}
else if (usage > threshold * 0.8) {
return {
status: "warning",
message: `Memory usage is ${usage}% (approaching threshold)`,
value: usage,
};
}
return {
status: "healthy",
message: `Memory usage is ${usage}%`,
value: usage,
};
},
};
}
static createCpuProbe(threshold = 80) {
return {
name: "cpu_usage",
check: async () => {
const metrics = SystemMetrics_1.SystemMetrics.getInstance().getCpuMetrics();
const usage = metrics.usage;
if (usage > threshold) {
return {
status: "critical",
message: `CPU usage is ${usage}% (threshold: ${threshold}%)`,
value: usage,
};
}
else if (usage > threshold * 0.8) {
return {
status: "warning",
message: `CPU usage is ${usage}% (approaching threshold)`,
value: usage,
};
}
return {
status: "healthy",
message: `CPU usage is ${usage}%`,
value: usage,
};
},
};
}
static createUptimeProbe() {
return {
name: "uptime",
check: async () => {
const uptime = process.uptime();
const hours = Math.floor(uptime / 3600);
const minutes = Math.floor((uptime % 3600) / 60);
return {
status: "healthy",
message: `Application has been running for ${hours}h ${minutes}m`,
value: uptime,
};
},
};
}
static createDiskSpaceProbe() {
return {
name: "disk_space",
check: async () => {
try {
const fs = require("fs");
const stats = fs.statSync(process.cwd());
return {
status: "healthy",
message: "Disk space check completed",
metadata: {
path: process.cwd(),
accessible: true,
},
};
}
catch (error) {
return {
status: "critical",
message: "Cannot access disk space",
metadata: {
error: error instanceof Error ? error.message : "Unknown error",
},
};
}
},
};
}
}
exports.BuiltInProbes = BuiltInProbes;
//# sourceMappingURL=BuiltInProbes.js.map