@mh.js/health-monitor 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/monitor.d.ts +36 -0
- package/dist/monitor.js +36 -0
- package/dist/types.d.ts +53 -0
- package/package.json +10 -2
package/dist/monitor.d.ts
CHANGED
|
@@ -1,14 +1,50 @@
|
|
|
1
1
|
import type { HealthIndicator, HealthStatus, MonitorConfig } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* HealthMonitor class for monitoring system health and event loop lag.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* const monitor = new HealthMonitor({ interval: 3000 });
|
|
8
|
+
* monitor.register({
|
|
9
|
+
* name: 'db',
|
|
10
|
+
* check: async () => await db.ping()
|
|
11
|
+
* }).start();
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
2
14
|
export declare class HealthMonitor {
|
|
3
15
|
private indicators;
|
|
4
16
|
private intervalId;
|
|
5
17
|
private isCheckInProgress;
|
|
6
18
|
private config;
|
|
7
19
|
private latestStatus;
|
|
20
|
+
/**
|
|
21
|
+
* Creates a new HealthMonitor instance.
|
|
22
|
+
* @param config - Configuration options for the monitor.
|
|
23
|
+
*/
|
|
8
24
|
constructor(config?: MonitorConfig);
|
|
25
|
+
/**
|
|
26
|
+
* Registers a new health indicator to be monitored.
|
|
27
|
+
* @param indicator - The health indicator configuration.
|
|
28
|
+
* @returns The HealthMonitor instance for method chaining.
|
|
29
|
+
*/
|
|
9
30
|
register(indicator: HealthIndicator): this;
|
|
31
|
+
/**
|
|
32
|
+
* Starts the periodic health check loop.
|
|
33
|
+
* If the monitor is already running, this method does nothing.
|
|
34
|
+
*/
|
|
10
35
|
start(): void;
|
|
36
|
+
/**
|
|
37
|
+
* Stops the health check loop.
|
|
38
|
+
*/
|
|
11
39
|
stop(): void;
|
|
40
|
+
/**
|
|
41
|
+
* Retrieves the current health status of the system.
|
|
42
|
+
*
|
|
43
|
+
* If the health check loop is stalled (not updated for > 3x interval),
|
|
44
|
+
* it returns a 'down' status with a system error.
|
|
45
|
+
*
|
|
46
|
+
* @returns The current HealthStatus object.
|
|
47
|
+
*/
|
|
12
48
|
getStatus(): HealthStatus;
|
|
13
49
|
private checkEventLoopLag;
|
|
14
50
|
private check;
|
package/dist/monitor.js
CHANGED
|
@@ -2,7 +2,23 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.HealthMonitor = void 0;
|
|
4
4
|
const perf_hooks_1 = require("perf_hooks");
|
|
5
|
+
/**
|
|
6
|
+
* HealthMonitor class for monitoring system health and event loop lag.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* const monitor = new HealthMonitor({ interval: 3000 });
|
|
11
|
+
* monitor.register({
|
|
12
|
+
* name: 'db',
|
|
13
|
+
* check: async () => await db.ping()
|
|
14
|
+
* }).start();
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
5
17
|
class HealthMonitor {
|
|
18
|
+
/**
|
|
19
|
+
* Creates a new HealthMonitor instance.
|
|
20
|
+
* @param config - Configuration options for the monitor.
|
|
21
|
+
*/
|
|
6
22
|
constructor(config) {
|
|
7
23
|
var _a, _b;
|
|
8
24
|
this.indicators = [];
|
|
@@ -19,10 +35,19 @@ class HealthMonitor {
|
|
|
19
35
|
timeout: (_b = config === null || config === void 0 ? void 0 : config.timeout) !== null && _b !== void 0 ? _b : 1000,
|
|
20
36
|
};
|
|
21
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* Registers a new health indicator to be monitored.
|
|
40
|
+
* @param indicator - The health indicator configuration.
|
|
41
|
+
* @returns The HealthMonitor instance for method chaining.
|
|
42
|
+
*/
|
|
22
43
|
register(indicator) {
|
|
23
44
|
this.indicators.push(indicator);
|
|
24
45
|
return this;
|
|
25
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Starts the periodic health check loop.
|
|
49
|
+
* If the monitor is already running, this method does nothing.
|
|
50
|
+
*/
|
|
26
51
|
start() {
|
|
27
52
|
if (this.intervalId)
|
|
28
53
|
return;
|
|
@@ -30,12 +55,23 @@ class HealthMonitor {
|
|
|
30
55
|
this.intervalId = setInterval(() => this.check(), this.config.interval);
|
|
31
56
|
this.intervalId.unref();
|
|
32
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* Stops the health check loop.
|
|
60
|
+
*/
|
|
33
61
|
stop() {
|
|
34
62
|
if (this.intervalId) {
|
|
35
63
|
clearInterval(this.intervalId);
|
|
36
64
|
this.intervalId = null;
|
|
37
65
|
}
|
|
38
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* Retrieves the current health status of the system.
|
|
69
|
+
*
|
|
70
|
+
* If the health check loop is stalled (not updated for > 3x interval),
|
|
71
|
+
* it returns a 'down' status with a system error.
|
|
72
|
+
*
|
|
73
|
+
* @returns The current HealthStatus object.
|
|
74
|
+
*/
|
|
39
75
|
getStatus() {
|
|
40
76
|
const now = Date.now();
|
|
41
77
|
const isStale = (now - this.latestStatus.timestamp) > (this.config.interval * 3);
|
package/dist/types.d.ts
CHANGED
|
@@ -1,20 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interface representing a health check indicator.
|
|
3
|
+
*/
|
|
1
4
|
export interface HealthIndicator {
|
|
5
|
+
/**
|
|
6
|
+
* The name of the component being monitored (e.g., 'database', 'redis').
|
|
7
|
+
*/
|
|
2
8
|
name: string;
|
|
9
|
+
/**
|
|
10
|
+
* A function that performs the health check.
|
|
11
|
+
* Should return a Promise that resolves if healthy, and rejects if unhealthy.
|
|
12
|
+
*/
|
|
3
13
|
check: () => Promise<any>;
|
|
14
|
+
/**
|
|
15
|
+
* Optional timeout for this specific indicator in milliseconds.
|
|
16
|
+
* If not provided, the global monitor timeout will be used.
|
|
17
|
+
*/
|
|
4
18
|
timeout?: number;
|
|
5
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* Interface representing the status details of a specific component.
|
|
22
|
+
*/
|
|
6
23
|
export interface ComponentDetail {
|
|
24
|
+
/**
|
|
25
|
+
* The status of the component ('up' or 'down').
|
|
26
|
+
*/
|
|
7
27
|
status: 'up' | 'down';
|
|
28
|
+
/**
|
|
29
|
+
* The latency of the health check in milliseconds.
|
|
30
|
+
*/
|
|
8
31
|
latency?: number;
|
|
32
|
+
/**
|
|
33
|
+
* The error message if the status is 'down'.
|
|
34
|
+
*/
|
|
9
35
|
error?: string;
|
|
10
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Interface representing the overall health status of the system.
|
|
39
|
+
*/
|
|
11
40
|
export interface HealthStatus {
|
|
41
|
+
/**
|
|
42
|
+
* The overall status of the system ('ok' if all indicators are up, 'down' otherwise).
|
|
43
|
+
*/
|
|
12
44
|
status: 'ok' | 'down';
|
|
45
|
+
/**
|
|
46
|
+
* The event loop lag in milliseconds.
|
|
47
|
+
* High lag indicates the system is under heavy load.
|
|
48
|
+
*/
|
|
13
49
|
lag: number;
|
|
50
|
+
/**
|
|
51
|
+
* Detailed status of each registered component.
|
|
52
|
+
*/
|
|
14
53
|
details: Record<string, ComponentDetail>;
|
|
54
|
+
/**
|
|
55
|
+
* The timestamp when this status was generated.
|
|
56
|
+
*/
|
|
15
57
|
timestamp: number;
|
|
16
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Configuration options for the HealthMonitor.
|
|
61
|
+
*/
|
|
17
62
|
export interface MonitorConfig {
|
|
63
|
+
/**
|
|
64
|
+
* The interval between health checks in milliseconds.
|
|
65
|
+
* @default 3000
|
|
66
|
+
*/
|
|
18
67
|
interval?: number;
|
|
68
|
+
/**
|
|
69
|
+
* The default timeout for health checks in milliseconds.
|
|
70
|
+
* @default 1000
|
|
71
|
+
*/
|
|
19
72
|
timeout?: number;
|
|
20
73
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mh.js/health-monitor",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Lightweight health check and event loop monitoring detection library for Node.js. Supports custom indicators and TypeScript.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -33,5 +33,13 @@
|
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@types/node": "^25.0.9",
|
|
35
35
|
"typescript": "^5.9.3"
|
|
36
|
-
}
|
|
36
|
+
},
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "git+https://github.com/iu7726/health-monitor.git"
|
|
40
|
+
},
|
|
41
|
+
"bugs": {
|
|
42
|
+
"url": "https://github.com/iu7726/health-monitor/issues"
|
|
43
|
+
},
|
|
44
|
+
"homepage": "https://github.com/iu7726/health-monitor#readme"
|
|
37
45
|
}
|