@omnitron-dev/titan-health 0.1.1
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/LICENSE +21 -0
- package/README.md +19 -0
- package/dist/health.indicator.d.ts +29 -0
- package/dist/health.indicator.d.ts.map +1 -0
- package/dist/health.indicator.js +136 -0
- package/dist/health.indicator.js.map +1 -0
- package/dist/health.module.d.ts +9 -0
- package/dist/health.module.d.ts.map +1 -0
- package/dist/health.module.js +302 -0
- package/dist/health.module.js.map +1 -0
- package/dist/health.rpc-service.d.ts +56 -0
- package/dist/health.rpc-service.d.ts.map +1 -0
- package/dist/health.rpc-service.js +138 -0
- package/dist/health.rpc-service.js.map +1 -0
- package/dist/health.service.d.ts +28 -0
- package/dist/health.service.d.ts.map +1 -0
- package/dist/health.service.js +182 -0
- package/dist/health.service.js.map +1 -0
- package/dist/health.tokens.d.ts +12 -0
- package/dist/health.tokens.d.ts.map +1 -0
- package/dist/health.tokens.js +10 -0
- package/dist/health.tokens.js.map +1 -0
- package/dist/health.types.d.ts +103 -0
- package/dist/health.types.d.ts.map +1 -0
- package/dist/health.types.js +2 -0
- package/dist/health.types.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/indicators/database.indicator.d.ts +25 -0
- package/dist/indicators/database.indicator.d.ts.map +1 -0
- package/dist/indicators/database.indicator.js +66 -0
- package/dist/indicators/database.indicator.js.map +1 -0
- package/dist/indicators/disk.indicator.d.ts +16 -0
- package/dist/indicators/disk.indicator.d.ts.map +1 -0
- package/dist/indicators/disk.indicator.js +159 -0
- package/dist/indicators/disk.indicator.js.map +1 -0
- package/dist/indicators/event-loop.indicator.d.ts +27 -0
- package/dist/indicators/event-loop.indicator.d.ts.map +1 -0
- package/dist/indicators/event-loop.indicator.js +150 -0
- package/dist/indicators/event-loop.indicator.js.map +1 -0
- package/dist/indicators/index.d.ts +6 -0
- package/dist/indicators/index.d.ts.map +1 -0
- package/dist/indicators/index.js +6 -0
- package/dist/indicators/index.js.map +1 -0
- package/dist/indicators/memory.indicator.d.ts +13 -0
- package/dist/indicators/memory.indicator.d.ts.map +1 -0
- package/dist/indicators/memory.indicator.js +103 -0
- package/dist/indicators/memory.indicator.js.map +1 -0
- package/dist/indicators/redis.indicator.d.ts +26 -0
- package/dist/indicators/redis.indicator.d.ts.map +1 -0
- package/dist/indicators/redis.indicator.js +101 -0
- package/dist/indicators/redis.indicator.js.map +1 -0
- package/package.json +44 -0
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
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;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
import { Service, Public, Injectable } from '@omnitron-dev/titan/decorators';
|
|
11
|
+
let HealthRpcService = class HealthRpcService {
|
|
12
|
+
setHealthService(healthService) {
|
|
13
|
+
this.healthService = healthService;
|
|
14
|
+
}
|
|
15
|
+
setVersion(version) {
|
|
16
|
+
this.version = version;
|
|
17
|
+
}
|
|
18
|
+
async check() {
|
|
19
|
+
const result = await this.healthService.check();
|
|
20
|
+
return {
|
|
21
|
+
status: result.status,
|
|
22
|
+
timestamp: result.timestamp.toISOString(),
|
|
23
|
+
uptime: result.uptime ?? this.healthService.getUptime(),
|
|
24
|
+
version: this.version,
|
|
25
|
+
indicators: result.indicators,
|
|
26
|
+
totalLatency: result.totalLatency,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
async live() {
|
|
30
|
+
const isAlive = await this.healthService.isAlive();
|
|
31
|
+
return {
|
|
32
|
+
status: isAlive ? 'up' : 'down',
|
|
33
|
+
timestamp: new Date().toISOString(),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
async ready() {
|
|
37
|
+
const result = await this.healthService.check();
|
|
38
|
+
const checks = {};
|
|
39
|
+
for (const [name, indicator] of Object.entries(result.indicators)) {
|
|
40
|
+
if (indicator.status !== 'healthy') {
|
|
41
|
+
checks[name] = {
|
|
42
|
+
status: indicator.status,
|
|
43
|
+
message: indicator.message,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
status: result.status,
|
|
49
|
+
timestamp: new Date().toISOString(),
|
|
50
|
+
checks: Object.keys(checks).length > 0 ? checks : undefined,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
async checkIndicator(name) {
|
|
54
|
+
const result = await this.healthService.checkOne(name);
|
|
55
|
+
return {
|
|
56
|
+
name,
|
|
57
|
+
result,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
async listIndicators() {
|
|
61
|
+
const indicators = this.healthService.getIndicators();
|
|
62
|
+
return {
|
|
63
|
+
indicators,
|
|
64
|
+
count: indicators.length,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
async uptime() {
|
|
68
|
+
const uptimeMs = this.healthService.getUptime();
|
|
69
|
+
const uptimeSeconds = Math.floor(uptimeMs / 1000);
|
|
70
|
+
const days = Math.floor(uptimeSeconds / 86400);
|
|
71
|
+
const hours = Math.floor((uptimeSeconds % 86400) / 3600);
|
|
72
|
+
const minutes = Math.floor((uptimeSeconds % 3600) / 60);
|
|
73
|
+
const seconds = uptimeSeconds % 60;
|
|
74
|
+
return {
|
|
75
|
+
uptime: {
|
|
76
|
+
ms: uptimeMs,
|
|
77
|
+
formatted: `${days}d ${hours}h ${minutes}m ${seconds}s`,
|
|
78
|
+
days,
|
|
79
|
+
hours,
|
|
80
|
+
minutes,
|
|
81
|
+
seconds,
|
|
82
|
+
},
|
|
83
|
+
timestamp: new Date().toISOString(),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
async isHealthy() {
|
|
87
|
+
const healthy = await this.healthService.isHealthy();
|
|
88
|
+
return { healthy };
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
__decorate([
|
|
92
|
+
Public({ auth: { allowAnonymous: true } }),
|
|
93
|
+
__metadata("design:type", Function),
|
|
94
|
+
__metadata("design:paramtypes", []),
|
|
95
|
+
__metadata("design:returntype", Promise)
|
|
96
|
+
], HealthRpcService.prototype, "check", null);
|
|
97
|
+
__decorate([
|
|
98
|
+
Public({ auth: { allowAnonymous: true } }),
|
|
99
|
+
__metadata("design:type", Function),
|
|
100
|
+
__metadata("design:paramtypes", []),
|
|
101
|
+
__metadata("design:returntype", Promise)
|
|
102
|
+
], HealthRpcService.prototype, "live", null);
|
|
103
|
+
__decorate([
|
|
104
|
+
Public({ auth: { allowAnonymous: true } }),
|
|
105
|
+
__metadata("design:type", Function),
|
|
106
|
+
__metadata("design:paramtypes", []),
|
|
107
|
+
__metadata("design:returntype", Promise)
|
|
108
|
+
], HealthRpcService.prototype, "ready", null);
|
|
109
|
+
__decorate([
|
|
110
|
+
Public({ auth: { allowAnonymous: true } }),
|
|
111
|
+
__metadata("design:type", Function),
|
|
112
|
+
__metadata("design:paramtypes", [String]),
|
|
113
|
+
__metadata("design:returntype", Promise)
|
|
114
|
+
], HealthRpcService.prototype, "checkIndicator", null);
|
|
115
|
+
__decorate([
|
|
116
|
+
Public({ auth: { allowAnonymous: true } }),
|
|
117
|
+
__metadata("design:type", Function),
|
|
118
|
+
__metadata("design:paramtypes", []),
|
|
119
|
+
__metadata("design:returntype", Promise)
|
|
120
|
+
], HealthRpcService.prototype, "listIndicators", null);
|
|
121
|
+
__decorate([
|
|
122
|
+
Public({ auth: { allowAnonymous: true } }),
|
|
123
|
+
__metadata("design:type", Function),
|
|
124
|
+
__metadata("design:paramtypes", []),
|
|
125
|
+
__metadata("design:returntype", Promise)
|
|
126
|
+
], HealthRpcService.prototype, "uptime", null);
|
|
127
|
+
__decorate([
|
|
128
|
+
Public({ auth: { allowAnonymous: true } }),
|
|
129
|
+
__metadata("design:type", Function),
|
|
130
|
+
__metadata("design:paramtypes", []),
|
|
131
|
+
__metadata("design:returntype", Promise)
|
|
132
|
+
], HealthRpcService.prototype, "isHealthy", null);
|
|
133
|
+
HealthRpcService = __decorate([
|
|
134
|
+
Service({ name: 'Health@1.0.0' }),
|
|
135
|
+
Injectable()
|
|
136
|
+
], HealthRpcService);
|
|
137
|
+
export { HealthRpcService };
|
|
138
|
+
//# sourceMappingURL=health.rpc-service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"health.rpc-service.js","sourceRoot":"","sources":["../src/health.rpc-service.ts"],"names":[],"mappings":";;;;;;;;;AASA,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,gCAAgC,CAAC;AAoFtE,IAAM,gBAAgB,GAAtB,MAAM,gBAAgB;IAQ3B,gBAAgB,CAAC,aAA4B;QAC3C,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;IACrC,CAAC;IAKD,UAAU,CAAC,OAAe;QACxB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IASK,AAAN,KAAK,CAAC,KAAK;QACT,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,aAAa,CAAC,KAAK,EAAE,CAAC;QAEhD,OAAO;YACL,MAAM,EAAE,MAAM,CAAC,MAAM;YACrB,SAAS,EAAE,MAAM,CAAC,SAAS,CAAC,WAAW,EAAE;YACzC,MAAM,EAAE,MAAM,CAAC,MAAM,IAAI,IAAI,CAAC,a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E,EAAE,cAAc,EAAE,IAAI,EAAE,EAAE,CAAC;;;;6CAY1C;AAWK;IADL,MAAM,CAAC,EAAE,IAAI,EAAE,EAAE,cAAc,EAAE,IAAI,EAAE,EAAE,CAAC;;;;4CAQ1C;AAYK;IADL,MAAM,CAAC,EAAE,IAAI,EAAE,EAAE,cAAc,EAAE,IAAI,EAAE,EAAE,CAAC;;;;6CAoB1C;AAQK;IADL,MAAM,CAAC,EAAE,IAAI,EAAE,EAAE,cAAc,EAAE,IAAI,EAAE,EAAE,CAAC;;;;sDAQ1C;AAMK;IADL,MAAM,CAAC,EAAE,IAAI,EAAE,EAAE,cAAc,EAAE,IAAI,EAAE,EAAE,CAAC;;;;sDAQ1C;AAMK;IADL,MAAM,CAAC,EAAE,IAAI,EAAE,EAAE,cAAc,EAAE,IAAI,EAAE,EAAE,CAAC;;;;8CAoB1C;AAMK;IADL,MAAM,CAAC,EAAE,IAAI,EAAE,EAAE,cAAc,EAAE,IAAI,EAAE,EAAE,CAAC;;;;iDAI1C;AApJU,gBAAgB;IAF5B,OAAO,CAAC,EAAE,IAAI,EAAE,cAAc,EAAE,CAAC;IACjC,UAAU,EAAE;GACA,gBAAgB,CAqJ5B"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { IHealthService, IHealthIndicator, HealthIndicatorResult, HealthCheckResult, HealthModuleOptions } from './health.types.js';
|
|
2
|
+
import type { ILogger } from '@omnitron-dev/titan/module/logger';
|
|
3
|
+
export declare class HealthService implements IHealthService {
|
|
4
|
+
private indicators;
|
|
5
|
+
private options;
|
|
6
|
+
private cache;
|
|
7
|
+
private startTime;
|
|
8
|
+
private logger?;
|
|
9
|
+
constructor(options?: HealthModuleOptions);
|
|
10
|
+
setLogger(logger: ILogger): void;
|
|
11
|
+
registerIndicator(indicator: IHealthIndicator): void;
|
|
12
|
+
unregisterIndicator(name: string): boolean;
|
|
13
|
+
check(): Promise<HealthCheckResult>;
|
|
14
|
+
checkOne(name: string): Promise<HealthIndicatorResult>;
|
|
15
|
+
isHealthy(): Promise<boolean>;
|
|
16
|
+
isReady(): Promise<boolean>;
|
|
17
|
+
isAlive(): Promise<boolean>;
|
|
18
|
+
getIndicators(): string[];
|
|
19
|
+
getIndicator(name: string): IHealthIndicator | undefined;
|
|
20
|
+
hasIndicator(name: string): boolean;
|
|
21
|
+
getIndicatorCount(): number;
|
|
22
|
+
clearIndicators(): void;
|
|
23
|
+
invalidateCache(): void;
|
|
24
|
+
getUptime(): number;
|
|
25
|
+
private runWithTimeout;
|
|
26
|
+
private getWorstStatus;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=health.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"health.service.d.ts","sourceRoot":"","sources":["../src/health.service.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EACV,cAAc,EACd,gBAAgB,EAChB,qBAAqB,EACrB,iBAAiB,EAEjB,mBAAmB,EACpB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mCAAmC,CAAC;AA0CjE,qBACa,aAAc,YAAW,cAAc;IAClD,OAAO,CAAC,UAAU,CAA4C;IAC9D,OAAO,CAAC,OAAO,CAAgF;IAC/F,OAAO,CAAC,KAAK,CAA2B;IACxC,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,MAAM,CAAC,CAAU;gBAEb,OAAO,GAAE,mBAAwB;IAY7C,SAAS,CAAC,MAAM,EAAE,OAAO,GAAG,IAAI;IAOhC,iBAAiB,CAAC,SAAS,EAAE,gBAAgB,GAAG,IAAI;IAepD,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAYpC,KAAK,IAAI,OAAO,CAAC,iBAAiB,CAAC;IAmEnC,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,qBAAqB,CAAC;IAYtD,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC;IAS7B,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC;IAS3B,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC;IAQjC,aAAa,IAAI,MAAM,EAAE;IAOzB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,gBAAgB,GAAG,SAAS;IAOxD,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAOnC,iBAAiB,IAAI,MAAM;IAO3B,eAAe,IAAI,IAAI;IAQvB,eAAe,IAAI,IAAI;IAOvB,SAAS,IAAI,MAAM;YAOL,cAAc;IAkC5B,OAAO,CAAC,cAAc;CAcvB"}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
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;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
import { Injectable } from '@omnitron-dev/titan/decorators';
|
|
11
|
+
import { Errors } from '@omnitron-dev/titan/errors';
|
|
12
|
+
const DEFAULT_OPTIONS = {
|
|
13
|
+
timeout: 5000,
|
|
14
|
+
enableCaching: false,
|
|
15
|
+
cacheTtl: 1000,
|
|
16
|
+
};
|
|
17
|
+
let HealthService = class HealthService {
|
|
18
|
+
constructor(options = {}) {
|
|
19
|
+
this.indicators = new Map();
|
|
20
|
+
this.cache = null;
|
|
21
|
+
this.options = {
|
|
22
|
+
timeout: options.timeout ?? DEFAULT_OPTIONS.timeout,
|
|
23
|
+
enableCaching: options.enableCaching ?? DEFAULT_OPTIONS.enableCaching,
|
|
24
|
+
cacheTtl: options.cacheTtl ?? DEFAULT_OPTIONS.cacheTtl,
|
|
25
|
+
};
|
|
26
|
+
this.startTime = Date.now();
|
|
27
|
+
}
|
|
28
|
+
setLogger(logger) {
|
|
29
|
+
this.logger = logger;
|
|
30
|
+
}
|
|
31
|
+
registerIndicator(indicator) {
|
|
32
|
+
if (!indicator.name) {
|
|
33
|
+
throw Errors.badRequest('Health indicator must have a name');
|
|
34
|
+
}
|
|
35
|
+
if (this.indicators.has(indicator.name)) {
|
|
36
|
+
throw Errors.conflict(`Health indicator '${indicator.name}' is already registered`);
|
|
37
|
+
}
|
|
38
|
+
this.indicators.set(indicator.name, indicator);
|
|
39
|
+
this.cache = null;
|
|
40
|
+
}
|
|
41
|
+
unregisterIndicator(name) {
|
|
42
|
+
const result = this.indicators.delete(name);
|
|
43
|
+
if (result) {
|
|
44
|
+
this.cache = null;
|
|
45
|
+
}
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
async check() {
|
|
49
|
+
if (this.options.enableCaching && this.cache && Date.now() < this.cache.expiresAt) {
|
|
50
|
+
return this.cache.result;
|
|
51
|
+
}
|
|
52
|
+
const start = Date.now();
|
|
53
|
+
const indicators = {};
|
|
54
|
+
const statuses = [];
|
|
55
|
+
const indicatorEntries = Array.from(this.indicators.entries());
|
|
56
|
+
const results = await Promise.allSettled(indicatorEntries.map(async ([name, indicator]) => ({
|
|
57
|
+
name,
|
|
58
|
+
result: await this.runWithTimeout(indicator),
|
|
59
|
+
})));
|
|
60
|
+
for (const result of results) {
|
|
61
|
+
if (result.status === 'fulfilled') {
|
|
62
|
+
const { name, result: indicatorResult } = result.value;
|
|
63
|
+
indicators[name] = indicatorResult;
|
|
64
|
+
statuses.push(indicatorResult.status);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
const index = results.indexOf(result);
|
|
68
|
+
const name = indicatorEntries[index]?.[0] || 'unknown';
|
|
69
|
+
indicators[name] = {
|
|
70
|
+
status: 'unhealthy',
|
|
71
|
+
message: result.reason?.message || 'Health check failed',
|
|
72
|
+
error: {
|
|
73
|
+
message: result.reason?.message || 'Unknown error',
|
|
74
|
+
},
|
|
75
|
+
timestamp: new Date(),
|
|
76
|
+
};
|
|
77
|
+
statuses.push('unhealthy');
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const status = this.getWorstStatus(statuses);
|
|
81
|
+
const totalLatency = Date.now() - start;
|
|
82
|
+
const checkResult = {
|
|
83
|
+
status,
|
|
84
|
+
indicators,
|
|
85
|
+
timestamp: new Date(),
|
|
86
|
+
totalLatency,
|
|
87
|
+
uptime: Date.now() - this.startTime,
|
|
88
|
+
};
|
|
89
|
+
if (this.options.enableCaching) {
|
|
90
|
+
this.cache = {
|
|
91
|
+
result: checkResult,
|
|
92
|
+
expiresAt: Date.now() + this.options.cacheTtl,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
return checkResult;
|
|
96
|
+
}
|
|
97
|
+
async checkOne(name) {
|
|
98
|
+
const indicator = this.indicators.get(name);
|
|
99
|
+
if (!indicator) {
|
|
100
|
+
throw Errors.notFound('Health indicator', name);
|
|
101
|
+
}
|
|
102
|
+
return this.runWithTimeout(indicator);
|
|
103
|
+
}
|
|
104
|
+
async isHealthy() {
|
|
105
|
+
const result = await this.check();
|
|
106
|
+
return result.status === 'healthy';
|
|
107
|
+
}
|
|
108
|
+
async isReady() {
|
|
109
|
+
const result = await this.check();
|
|
110
|
+
return result.status !== 'unhealthy';
|
|
111
|
+
}
|
|
112
|
+
async isAlive() {
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
getIndicators() {
|
|
116
|
+
return Array.from(this.indicators.keys());
|
|
117
|
+
}
|
|
118
|
+
getIndicator(name) {
|
|
119
|
+
return this.indicators.get(name);
|
|
120
|
+
}
|
|
121
|
+
hasIndicator(name) {
|
|
122
|
+
return this.indicators.has(name);
|
|
123
|
+
}
|
|
124
|
+
getIndicatorCount() {
|
|
125
|
+
return this.indicators.size;
|
|
126
|
+
}
|
|
127
|
+
clearIndicators() {
|
|
128
|
+
this.indicators.clear();
|
|
129
|
+
this.cache = null;
|
|
130
|
+
}
|
|
131
|
+
invalidateCache() {
|
|
132
|
+
this.cache = null;
|
|
133
|
+
}
|
|
134
|
+
getUptime() {
|
|
135
|
+
return Date.now() - this.startTime;
|
|
136
|
+
}
|
|
137
|
+
async runWithTimeout(indicator) {
|
|
138
|
+
return new Promise((resolve) => {
|
|
139
|
+
const timer = setTimeout(() => {
|
|
140
|
+
resolve({
|
|
141
|
+
status: 'unhealthy',
|
|
142
|
+
message: `Health check timed out after ${this.options.timeout}ms`,
|
|
143
|
+
timestamp: new Date(),
|
|
144
|
+
});
|
|
145
|
+
}, this.options.timeout);
|
|
146
|
+
indicator
|
|
147
|
+
.check()
|
|
148
|
+
.then((result) => {
|
|
149
|
+
clearTimeout(timer);
|
|
150
|
+
resolve(result);
|
|
151
|
+
})
|
|
152
|
+
.catch((error) => {
|
|
153
|
+
clearTimeout(timer);
|
|
154
|
+
resolve({
|
|
155
|
+
status: 'unhealthy',
|
|
156
|
+
message: error?.message || 'Health check failed',
|
|
157
|
+
error: {
|
|
158
|
+
message: error?.message || 'Unknown error',
|
|
159
|
+
code: error?.code,
|
|
160
|
+
},
|
|
161
|
+
timestamp: new Date(),
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
getWorstStatus(statuses) {
|
|
167
|
+
if (statuses.length === 0)
|
|
168
|
+
return 'healthy';
|
|
169
|
+
const severity = {
|
|
170
|
+
healthy: 0,
|
|
171
|
+
degraded: 1,
|
|
172
|
+
unhealthy: 2,
|
|
173
|
+
};
|
|
174
|
+
return statuses.reduce((worst, current) => (severity[current] > severity[worst] ? current : worst), 'healthy');
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
HealthService = __decorate([
|
|
178
|
+
Injectable(),
|
|
179
|
+
__metadata("design:paramtypes", [Object])
|
|
180
|
+
], HealthService);
|
|
181
|
+
export { HealthService };
|
|
182
|
+
//# sourceMappingURL=health.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"health.service.js","sourceRoot":"","sources":["../src/health.service.ts"],"names":[],"mappings":";;;;;;;;;AAQA,OAAO,EAAE,UAAU,EAAE,MAAM,gCAAgC,CAAC;AAS5D,OAAO,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAC;AAMpD,MAAM,eAAe,GAAkF;IACrG,OAAO,EAAE,IAAI;IACb,aAAa,EAAE,KAAK;IACpB,QAAQ,EAAE,IAAI;CACf,CAAC;AAkCK,IAAM,aAAa,GAAnB,MAAM,aAAa;IAOxB,YAAY,UAA+B,EAAE;QANrC,eAAU,GAAkC,IAAI,GAAG,EAAE,CAAC;QAEtD,UAAK,GAAsB,IAAI,CAAC;QAKtC,IAAI,CAAC,OAAO,GAAG;YACb,OAAO,EAAE,OAAO,CAAC,OAAO,IAAI,eAAe,CAAC,OAAO;YACnD,aAAa,EAAE,OAAO,CAAC,aAAa,IAAI,eAAe,CAAC,aAAa;YACrE,QAAQ,EAAE,OAAO,CAAC,QAAQ,IAAI,eAAe,CAAC,QAAQ;SACvD,CAAC;QACF,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IAC9B,CAAC;IAKD,SAAS,CAAC,MAAe;QACvB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAKD,iBAAiB,CAAC,SAA2B;QAC3C,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC;YACpB,MAAM,MAAM,CAAC,UAAU,CAAC,mCAAmC,CAAC,CAAC;QAC/D,CAAC;QACD,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC;YACxC,MAAM,MAAM,CAAC,QAA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EAAE,CAAC;SACb,CAAC;QAEF,OAAO,QAAQ,CAAC,MAAM,CACpB,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,EAC3E,SAAyB,CAC1B,CAAC;IACJ,CAAC;CACF,CAAA;AAhQY,aAAa;IADzB,UAAU,EAAE;;GACA,aAAa,CAgQzB"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type Token } from '@omnitron-dev/titan/nexus';
|
|
2
|
+
import type { IHealthService, HealthModuleOptions } from './health.types.js';
|
|
3
|
+
import type { HealthRpcService } from './health.rpc-service.js';
|
|
4
|
+
export declare const HEALTH_SERVICE_TOKEN: Token<IHealthService>;
|
|
5
|
+
export declare const HEALTH_MODULE_OPTIONS_TOKEN: Token<HealthModuleOptions>;
|
|
6
|
+
export declare const MEMORY_HEALTH_INDICATOR_TOKEN: Token<any>;
|
|
7
|
+
export declare const EVENT_LOOP_HEALTH_INDICATOR_TOKEN: Token<any>;
|
|
8
|
+
export declare const DISK_HEALTH_INDICATOR_TOKEN: Token<any>;
|
|
9
|
+
export declare const DATABASE_HEALTH_INDICATOR_TOKEN: Token<any>;
|
|
10
|
+
export declare const REDIS_HEALTH_INDICATOR_TOKEN: Token<any>;
|
|
11
|
+
export declare const HEALTH_RPC_SERVICE_TOKEN: Token<HealthRpcService>;
|
|
12
|
+
//# sourceMappingURL=health.tokens.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"health.tokens.d.ts","sourceRoot":"","sources":["../src/health.tokens.ts"],"names":[],"mappings":"AAQA,OAAO,EAAe,KAAK,KAAK,EAAE,MAAM,2BAA2B,CAAC;AACpE,OAAO,KAAK,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAC7E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAKhE,eAAO,MAAM,oBAAoB,EAAE,KAAK,CAAC,cAAc,CAAgD,CAAC;AAKxG,eAAO,MAAM,2BAA2B,EAAE,KAAK,CAAC,mBAAmB,CAA2D,CAAC;AAS/H,eAAO,MAAM,6BAA6B,EAAE,KAAK,CAAC,GAAG,CAA6C,CAAC;AASnG,eAAO,MAAM,iCAAiC,EAAE,KAAK,CAAC,GAAG,CAAgD,CAAC;AAS1G,eAAO,MAAM,2BAA2B,EAAE,KAAK,CAAC,GAAG,CAA2C,CAAC;AAS/F,eAAO,MAAM,+BAA+B,EAAE,KAAK,CAAC,GAAG,CAA+C,CAAC;AASvG,eAAO,MAAM,4BAA4B,EAAE,KAAK,CAAC,GAAG,CAA4C,CAAC;AAKjG,eAAO,MAAM,wBAAwB,EAAE,KAAK,CAAC,gBAAgB,CAAqD,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { createToken } from '@omnitron-dev/titan/nexus';
|
|
2
|
+
export const HEALTH_SERVICE_TOKEN = createToken('HealthService');
|
|
3
|
+
export const HEALTH_MODULE_OPTIONS_TOKEN = createToken('HealthModuleOptions');
|
|
4
|
+
export const MEMORY_HEALTH_INDICATOR_TOKEN = createToken('MemoryHealthIndicator');
|
|
5
|
+
export const EVENT_LOOP_HEALTH_INDICATOR_TOKEN = createToken('EventLoopHealthIndicator');
|
|
6
|
+
export const DISK_HEALTH_INDICATOR_TOKEN = createToken('DiskHealthIndicator');
|
|
7
|
+
export const DATABASE_HEALTH_INDICATOR_TOKEN = createToken('DatabaseHealthIndicator');
|
|
8
|
+
export const REDIS_HEALTH_INDICATOR_TOKEN = createToken('RedisHealthIndicator');
|
|
9
|
+
export const HEALTH_RPC_SERVICE_TOKEN = createToken('HealthRpcService');
|
|
10
|
+
//# sourceMappingURL=health.tokens.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"health.tokens.js","sourceRoot":"","sources":["../src/health.tokens.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,WAAW,EAAc,MAAM,2BAA2B,CAAC;AAOpE,MAAM,CAAC,MAAM,oBAAoB,GAA0B,WAAW,CAAiB,eAAe,CAAC,CAAC;AAKxG,MAAM,CAAC,MAAM,2BAA2B,GAA+B,WAAW,CAAsB,qBAAqB,CAAC,CAAC;AAS/H,MAAM,CAAC,MAAM,6BAA6B,GAAe,WAAW,CAAM,uBAAuB,CAAC,CAAC;AASnG,MAAM,CAAC,MAAM,iCAAiC,GAAe,WAAW,CAAM,0BAA0B,CAAC,CAAC;AAS1G,MAAM,CAAC,MAAM,2BAA2B,GAAe,WAAW,CAAM,qBAAqB,CAAC,CAAC;AAS/F,MAAM,CAAC,MAAM,+BAA+B,GAAe,WAAW,CAAM,yBAAyB,CAAC,CAAC;AASvG,MAAM,CAAC,MAAM,4BAA4B,GAAe,WAAW,CAAM,sBAAsB,CAAC,CAAC;AAKjG,MAAM,CAAC,MAAM,wBAAwB,GAA4B,WAAW,CAAmB,kBAAkB,CAAC,CAAC"}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
export type HealthStatus = 'healthy' | 'degraded' | 'unhealthy';
|
|
2
|
+
export interface HealthIndicatorResult {
|
|
3
|
+
status: HealthStatus;
|
|
4
|
+
message?: string;
|
|
5
|
+
details?: Record<string, unknown>;
|
|
6
|
+
latency?: number;
|
|
7
|
+
timestamp?: Date;
|
|
8
|
+
error?: {
|
|
9
|
+
message: string;
|
|
10
|
+
code?: string;
|
|
11
|
+
stack?: string;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export interface HealthCheckResult {
|
|
15
|
+
status: HealthStatus;
|
|
16
|
+
indicators: Record<string, HealthIndicatorResult>;
|
|
17
|
+
timestamp: Date;
|
|
18
|
+
totalLatency?: number;
|
|
19
|
+
version?: string;
|
|
20
|
+
uptime?: number;
|
|
21
|
+
}
|
|
22
|
+
export interface DatabaseIndicatorOptions {
|
|
23
|
+
latencyDegradedThreshold?: number;
|
|
24
|
+
latencyUnhealthyThreshold?: number;
|
|
25
|
+
healthQuery?: string;
|
|
26
|
+
timeout?: number;
|
|
27
|
+
}
|
|
28
|
+
export interface RedisIndicatorOptions {
|
|
29
|
+
latencyDegradedThreshold?: number;
|
|
30
|
+
latencyUnhealthyThreshold?: number;
|
|
31
|
+
timeout?: number;
|
|
32
|
+
includeMemoryInfo?: boolean;
|
|
33
|
+
memoryDegradedThreshold?: number;
|
|
34
|
+
memoryUnhealthyThreshold?: number;
|
|
35
|
+
}
|
|
36
|
+
export interface HealthModuleOptions {
|
|
37
|
+
enableMemoryIndicator?: boolean;
|
|
38
|
+
enableEventLoopIndicator?: boolean;
|
|
39
|
+
enableDiskIndicator?: boolean;
|
|
40
|
+
enableDatabaseIndicator?: boolean;
|
|
41
|
+
databaseConnection?: {
|
|
42
|
+
execute?(query: string): Promise<unknown>;
|
|
43
|
+
raw?(query: string): {
|
|
44
|
+
execute(): Promise<unknown>;
|
|
45
|
+
};
|
|
46
|
+
query?(query: string): Promise<unknown>;
|
|
47
|
+
};
|
|
48
|
+
enableRedisIndicator?: boolean;
|
|
49
|
+
redisClient?: {
|
|
50
|
+
ping(): Promise<string>;
|
|
51
|
+
info?(section?: string): Promise<string>;
|
|
52
|
+
status?: string;
|
|
53
|
+
isReady?: boolean;
|
|
54
|
+
};
|
|
55
|
+
timeout?: number;
|
|
56
|
+
enableCaching?: boolean;
|
|
57
|
+
cacheTtl?: number;
|
|
58
|
+
enableRpcService?: boolean;
|
|
59
|
+
version?: string;
|
|
60
|
+
isGlobal?: boolean;
|
|
61
|
+
indicators?: Array<new (...args: any[]) => IHealthIndicator>;
|
|
62
|
+
memoryThresholds?: MemoryThresholds;
|
|
63
|
+
eventLoopThresholds?: EventLoopThresholds;
|
|
64
|
+
diskThresholds?: DiskThresholds;
|
|
65
|
+
databaseOptions?: DatabaseIndicatorOptions;
|
|
66
|
+
redisOptions?: RedisIndicatorOptions;
|
|
67
|
+
}
|
|
68
|
+
export interface HealthModuleAsyncOptions {
|
|
69
|
+
useFactory?: (...args: any[]) => Promise<HealthModuleOptions> | HealthModuleOptions;
|
|
70
|
+
inject?: any[];
|
|
71
|
+
imports?: any[];
|
|
72
|
+
isGlobal?: boolean;
|
|
73
|
+
}
|
|
74
|
+
export interface MemoryThresholds {
|
|
75
|
+
heapDegradedThreshold?: number;
|
|
76
|
+
heapUnhealthyThreshold?: number;
|
|
77
|
+
rssLimit?: number;
|
|
78
|
+
externalLimit?: number;
|
|
79
|
+
}
|
|
80
|
+
export interface EventLoopThresholds {
|
|
81
|
+
lagDegradedThreshold?: number;
|
|
82
|
+
lagUnhealthyThreshold?: number;
|
|
83
|
+
}
|
|
84
|
+
export interface DiskThresholds {
|
|
85
|
+
usageDegradedThreshold?: number;
|
|
86
|
+
usageUnhealthyThreshold?: number;
|
|
87
|
+
path?: string;
|
|
88
|
+
}
|
|
89
|
+
export interface IHealthIndicator {
|
|
90
|
+
readonly name: string;
|
|
91
|
+
check(): Promise<HealthIndicatorResult>;
|
|
92
|
+
}
|
|
93
|
+
export interface IHealthService {
|
|
94
|
+
registerIndicator(indicator: IHealthIndicator): void;
|
|
95
|
+
unregisterIndicator(name: string): boolean;
|
|
96
|
+
check(): Promise<HealthCheckResult>;
|
|
97
|
+
checkOne(name: string): Promise<HealthIndicatorResult>;
|
|
98
|
+
isHealthy(): Promise<boolean>;
|
|
99
|
+
isReady(): Promise<boolean>;
|
|
100
|
+
isAlive(): Promise<boolean>;
|
|
101
|
+
getIndicators(): string[];
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=health.types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"health.types.d.ts","sourceRoot":"","sources":["../src/health.types.ts"],"names":[],"mappings":"AAWA,MAAM,MAAM,YAAY,GAAG,SAAS,GAAG,UAAU,GAAG,WAAW,CAAC;AAKhE,MAAM,WAAW,qBAAqB;IAIpC,MAAM,EAAE,YAAY,CAAC;IAKr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version":3,"file":"health.types.js","sourceRoot":"","sources":["../src/health.types.ts"],"names":[],"mappings":""}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { TitanHealthModule } from './health.module.js';
|
|
2
|
+
export { TitanHealthModule as HealthModule } from './health.module.js';
|
|
3
|
+
export { HealthService } from './health.service.js';
|
|
4
|
+
export { HealthRpcService, type LivenessResponse, type ReadinessResponse, type HealthResponse, type IndicatorResponse, type UptimeResponse, } from './health.rpc-service.js';
|
|
5
|
+
export { HealthIndicator, CompositeHealthIndicator } from './health.indicator.js';
|
|
6
|
+
export { MemoryHealthIndicator, EventLoopHealthIndicator, HighResEventLoopIndicator, DiskHealthIndicator, DatabaseHealthIndicator, RedisHealthIndicator, type DatabaseHealthOptions, type IDatabaseConnection, type RedisHealthOptions, type IRedisClient, } from './indicators/index.js';
|
|
7
|
+
export { HEALTH_SERVICE_TOKEN, HEALTH_MODULE_OPTIONS_TOKEN, HEALTH_RPC_SERVICE_TOKEN, MEMORY_HEALTH_INDICATOR_TOKEN, EVENT_LOOP_HEALTH_INDICATOR_TOKEN, DISK_HEALTH_INDICATOR_TOKEN, DATABASE_HEALTH_INDICATOR_TOKEN, REDIS_HEALTH_INDICATOR_TOKEN, } from './health.tokens.js';
|
|
8
|
+
export type { HealthStatus, HealthIndicatorResult, HealthCheckResult, HealthModuleOptions, HealthModuleAsyncOptions, DatabaseIndicatorOptions, RedisIndicatorOptions, MemoryThresholds, EventLoopThresholds, DiskThresholds, IHealthIndicator, IHealthService, } from './health.types.js';
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAgEA,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAMvD,OAAO,EAAE,iBAAiB,IAAI,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAMvE,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAMpD,OAAO,EACL,gBAAgB,EAChB,KAAK,gBAAgB,EACrB,KAAK,iBAAiB,EACtB,KAAK,cAAc,EACnB,KAAK,iBAAiB,EACtB,KAAK,cAAc,GACpB,MAAM,yBAAyB,CAAC;AAMjC,OAAO,EAAE,eAAe,EAAE,wBAAwB,EAAE,MAAM,uBAAuB,CAAC;AAMlF,OAAO,EAEL,qBAAqB,EACrB,wBAAwB,EACxB,yBAAyB,EACzB,mBAAmB,EAEnB,uBAAuB,EACvB,oBAAoB,EAEpB,KAAK,qBAAqB,EAC1B,KAAK,mBAAmB,EACxB,KAAK,kBAAkB,EACvB,KAAK,YAAY,GAClB,MAAM,uBAAuB,CAAC;AAM/B,OAAO,EACL,oBAAoB,EACpB,2BAA2B,EAC3B,wBAAwB,EAIxB,6BAA6B,EAC7B,iCAAiC,EACjC,2BAA2B,EAC3B,+BAA+B,EAC/B,4BAA4B,GAC7B,MAAM,oBAAoB,CAAC;AAM5B,YAAY,EAEV,YAAY,EAGZ,qBAAqB,EACrB,iBAAiB,EAGjB,mBAAmB,EACnB,wBAAwB,EACxB,wBAAwB,EACxB,qBAAqB,EAGrB,gBAAgB,EAChB,mBAAmB,EACnB,cAAc,EAGd,gBAAgB,EAChB,cAAc,GACf,MAAM,mBAAmB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { TitanHealthModule } from './health.module.js';
|
|
2
|
+
export { TitanHealthModule as HealthModule } from './health.module.js';
|
|
3
|
+
export { HealthService } from './health.service.js';
|
|
4
|
+
export { HealthRpcService, } from './health.rpc-service.js';
|
|
5
|
+
export { HealthIndicator, CompositeHealthIndicator } from './health.indicator.js';
|
|
6
|
+
export { MemoryHealthIndicator, EventLoopHealthIndicator, HighResEventLoopIndicator, DiskHealthIndicator, DatabaseHealthIndicator, RedisHealthIndicator, } from './indicators/index.js';
|
|
7
|
+
export { HEALTH_SERVICE_TOKEN, HEALTH_MODULE_OPTIONS_TOKEN, HEALTH_RPC_SERVICE_TOKEN, MEMORY_HEALTH_INDICATOR_TOKEN, EVENT_LOOP_HEALTH_INDICATOR_TOKEN, DISK_HEALTH_INDICATOR_TOKEN, DATABASE_HEALTH_INDICATOR_TOKEN, REDIS_HEALTH_INDICATOR_TOKEN, } from './health.tokens.js';
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAgEA,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAMvD,OAAO,EAAE,iBAAiB,IAAI,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAMvE,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAMpD,OAAO,EACL,gBAAgB,GAMjB,MAAM,yBAAyB,CAAC;AAMjC,OAAO,EAAE,eAAe,EAAE,wBAAwB,EAAE,MAAM,uBAAuB,CAAC;AAMlF,OAAO,EAEL,qBAAqB,EACrB,wBAAwB,EACxB,yBAAyB,EACzB,mBAAmB,EAEnB,uBAAuB,EACvB,oBAAoB,GAMrB,MAAM,uBAAuB,CAAC;AAM/B,OAAO,EACL,oBAAoB,EACpB,2BAA2B,EAC3B,wBAAwB,EAIxB,6BAA6B,EAC7B,iCAAiC,EACjC,2BAA2B,EAC3B,+BAA+B,EAC/B,4BAA4B,GAC7B,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { HealthIndicator } from '../health.indicator.js';
|
|
2
|
+
import type { HealthIndicatorResult } from '../health.types.js';
|
|
3
|
+
export interface DatabaseHealthOptions {
|
|
4
|
+
latencyDegradedThreshold?: number;
|
|
5
|
+
latencyUnhealthyThreshold?: number;
|
|
6
|
+
healthQuery?: string;
|
|
7
|
+
timeout?: number;
|
|
8
|
+
}
|
|
9
|
+
export interface IDatabaseConnection {
|
|
10
|
+
execute?(query: string): Promise<unknown>;
|
|
11
|
+
raw?(query: string): {
|
|
12
|
+
execute(): Promise<unknown>;
|
|
13
|
+
};
|
|
14
|
+
query?(query: string): Promise<unknown>;
|
|
15
|
+
}
|
|
16
|
+
export declare class DatabaseHealthIndicator extends HealthIndicator {
|
|
17
|
+
readonly name = "database";
|
|
18
|
+
private readonly options;
|
|
19
|
+
private connection?;
|
|
20
|
+
constructor(connection?: IDatabaseConnection, options?: DatabaseHealthOptions);
|
|
21
|
+
setConnection(connection: IDatabaseConnection): void;
|
|
22
|
+
check(): Promise<HealthIndicatorResult>;
|
|
23
|
+
private executeHealthQuery;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=database.indicator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"database.indicator.d.ts","sourceRoot":"","sources":["../../src/indicators/database.indicator.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAKhE,MAAM,WAAW,qBAAqB;IAKpC,wBAAwB,CAAC,EAAE,MAAM,CAAC;IAMlC,yBAAyB,CAAC,EAAE,MAAM,CAAC;IAMnC,WAAW,CAAC,EAAE,MAAM,CAAC;IAMrB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAMD,MAAM,WAAW,mBAAmB;IAIlC,OAAO,CAAC,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAK1C,GAAG,CAAC,CAAC,KAAK,EAAE,MAAM,GAAG;QAAE,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC,CAAA;KAAE,CAAC;IAKrD,KAAK,CAAC,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CACzC;AAgCD,qBAAa,uBAAwB,SAAQ,eAAe;IAC1D,QAAQ,CAAC,IAAI,cAAc;IAE3B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAkC;IAC1D,OAAO,CAAC,UAAU,CAAC,CAAsB;gBAE7B,UAAU,CAAC,EAAE,mBAAmB,EAAE,OAAO,GAAE,qBAA0B;IAYjF,aAAa,CAAC,UAAU,EAAE,mBAAmB,GAAG,IAAI;IAO9C,KAAK,IAAI,OAAO,CAAC,qBAAqB,CAAC;YAwC/B,kBAAkB;CAuBjC"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { HealthIndicator } from '../health.indicator.js';
|
|
2
|
+
const DEFAULT_OPTIONS = {
|
|
3
|
+
latencyDegradedThreshold: 100,
|
|
4
|
+
latencyUnhealthyThreshold: 1000,
|
|
5
|
+
healthQuery: 'SELECT 1',
|
|
6
|
+
timeout: 5000,
|
|
7
|
+
};
|
|
8
|
+
export class DatabaseHealthIndicator extends HealthIndicator {
|
|
9
|
+
constructor(connection, options = {}) {
|
|
10
|
+
super();
|
|
11
|
+
this.name = 'database';
|
|
12
|
+
this.connection = connection;
|
|
13
|
+
this.options = {
|
|
14
|
+
...DEFAULT_OPTIONS,
|
|
15
|
+
...options,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
setConnection(connection) {
|
|
19
|
+
this.connection = connection;
|
|
20
|
+
}
|
|
21
|
+
async check() {
|
|
22
|
+
if (!this.connection) {
|
|
23
|
+
return this.unhealthy('Database connection not configured');
|
|
24
|
+
}
|
|
25
|
+
try {
|
|
26
|
+
const start = Date.now();
|
|
27
|
+
await this.withTimeout(() => this.executeHealthQuery(), this.options.timeout);
|
|
28
|
+
const latency = Date.now() - start;
|
|
29
|
+
const details = {
|
|
30
|
+
latency,
|
|
31
|
+
threshold: {
|
|
32
|
+
degraded: this.options.latencyDegradedThreshold,
|
|
33
|
+
unhealthy: this.options.latencyUnhealthyThreshold,
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
if (latency >= this.options.latencyUnhealthyThreshold) {
|
|
37
|
+
return this.unhealthy(`Database response too slow: ${latency}ms`, details);
|
|
38
|
+
}
|
|
39
|
+
if (latency >= this.options.latencyDegradedThreshold) {
|
|
40
|
+
return this.degraded(`Database response slow: ${latency}ms`, details);
|
|
41
|
+
}
|
|
42
|
+
return this.healthy(`Database responding in ${latency}ms`, details);
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
46
|
+
return this.unhealthy(`Database check failed: ${errorMessage}`, undefined, error instanceof Error ? error : new Error(errorMessage));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
async executeHealthQuery() {
|
|
50
|
+
const conn = this.connection;
|
|
51
|
+
if (typeof conn.raw === 'function') {
|
|
52
|
+
await conn.raw(this.options.healthQuery).execute();
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (typeof conn.execute === 'function') {
|
|
56
|
+
await conn.execute(this.options.healthQuery);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (typeof conn.query === 'function') {
|
|
60
|
+
await conn.query(this.options.healthQuery);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
throw new Error('Database connection does not support any known query method');
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=database.indicator.js.map
|