@qrvey/health-checker
Version:
 
275 lines (240 loc) • 8.85 kB
TypeScript
type Dependency = 'database' | 'cache' | 'eventBroker' | 'warehouse';
type HealthStatus = 'OK' | 'FAILED' | 'VOID';
interface EventBrokerHealthCheckerResult {
status: HealthStatus;
metadata: {
consumersSet?: Set<string>;
};
}
interface IHealthChecker<T = void> {
dependency: Dependency;
check(params?: DependencyHealthCheckerParams): Promise<T>;
}
interface IEventBrokerParams {
queues?: string[];
hostName?: string;
consumersSet?: Set<string>;
omitPing?: boolean;
returnConsumersSet?: boolean;
}
interface IWarehouseHealthCheckerParams {
thresholds?: {
cpuUsagePercent?: number;
jvmHeapPercent?: number;
diskUsagePercent?: number;
circuitBreakerPercent?: number;
searchQueueThreshold?: number;
};
skipRules?: {
skipCpuCheck?: boolean;
skipJvmHeapCheck?: boolean;
skipDiskCheck?: boolean;
skipClusterStatusCheck?: boolean;
skipUnassignedShardsCheck?: boolean;
skipCircuitBreakerCheck?: boolean;
skipThreadPoolCheck?: boolean;
skipSearchQueueCheck?: boolean;
};
connection?: {
host?: string;
port?: number;
username?: string;
password?: string;
timeout?: number;
};
returnClusterStats?: boolean;
clusterStats?: ElasticsearchClusterStats;
customClusterStatusCheck?: (status: 'green' | 'yellow' | 'red') => boolean;
customUnassignedShardsCheck?: (unassignedShards: number) => boolean;
customJvmHeapCheck?: (heapPercent: number) => boolean;
customCircuitBreakerCheck?: (breakers: {
parent?: {
tripped: number;
};
request?: {
tripped: number;
};
fielddata?: {
tripped: number;
};
in_flight_requests?: {
tripped: number;
};
}) => boolean;
}
interface IFrameworkRequest {
method?: string;
path?: string;
originalUrl?: string;
url?: string;
headers?: Record<string, string | string[] | undefined>;
}
interface IMiddlewareRequest {
method: string;
path: string;
headers: Record<string, string | string[] | undefined>;
}
interface IMiddlewareResponse {
sendJson: (statusCode: number, body: unknown) => void;
}
interface ElasticsearchClusterStats {
clusterName: string;
status: 'green' | 'yellow' | 'red';
nodes: {
total: number;
successful: number;
failed: number;
};
cpuUsagePercent: number;
jvmHeapUsagePercent: number;
diskUsagePercent: number;
activePrimaryShards: number;
activeShards: number;
relocatingShards: number;
initializingShards: number;
unassignedShards: number;
circuitBreakersException: boolean;
threadPoolRejections: boolean;
searchQueuedRequests: number;
indicesCount?: number;
totalDiskBytes?: number;
usedDiskBytes?: number;
totalHeapMaxBytes?: number;
totalHeapUsedBytes?: number;
}
interface WarehouseHealthCheckerResult {
status: HealthStatus;
metadata: {
clusterStats?: ElasticsearchClusterStats;
errorMessage?: string;
checkTimestamp: string;
};
}
type CheckerRegistry = {
cache: IHealthChecker<void>;
database: IHealthChecker<void>;
eventBroker: IHealthChecker<EventBrokerHealthCheckerResult>;
warehouse: IHealthChecker<WarehouseHealthCheckerResult>;
};
type DependencyHealthDetail = {
status: HealthStatus;
durationMs?: number;
metadata?: Record<string, unknown>;
};
type HealthCheckResult = {
status: HealthStatus;
details: Partial<Record<Dependency, DependencyHealthDetail>>;
};
type HealthCheckerParams = {
eventBroker?: IEventBrokerParams;
hostName?: string;
skip?: string[];
force?: boolean;
warehouse?: IWarehouseHealthCheckerParams;
};
type DependencyHealthCheckerParams = IEventBrokerParams | IWarehouseHealthCheckerParams | ({
hostName?: string;
} & Partial<Record<Dependency, unknown>>);
type RuntimeHealthThresholds = {
cpuPercent: number;
heapPercent: number;
eventLoopDelayMs: number;
};
type RuntimeHealthEnabledMetrics = {
cpuPercent: boolean;
heapPercent: boolean;
eventLoopDelayMs: boolean;
};
declare const RUNTIME_HEALTH_CACHE_SOURCE: {
readonly computed: "computed";
readonly cached: "cached";
readonly in_flight: "in_flight";
};
type RuntimeHealthCacheSource = (typeof RUNTIME_HEALTH_CACHE_SOURCE)[keyof typeof RUNTIME_HEALTH_CACHE_SOURCE];
type RuntimeHealthCacheInfo = {
source: RuntimeHealthCacheSource;
ttlMs: number;
ageMs: number;
cachedAt?: string;
};
type RuntimeHealthMetrics = {
cpuPercent: number | null;
heapPercent: number | null;
eventLoopDelayMs: number | null;
heapLimitMb: number | null;
};
type RuntimeHealthResult = {
status: HealthStatus;
metrics: RuntimeHealthMetrics;
enabledMetrics: RuntimeHealthEnabledMetrics;
thresholds: RuntimeHealthThresholds;
failedMetrics: string[];
cache?: RuntimeHealthCacheInfo;
};
type RuntimeHealthStatus = {
status: HealthStatus;
details: RuntimeHealthResult;
};
type RuntimeHealthOptions = Partial<RuntimeHealthThresholds> & {
windowMs?: number;
thresholds?: Partial<RuntimeHealthThresholds>;
};
type RuntimeHealthMiddlewareStartOptions = {
runtimeOptions?: RuntimeHealthOptions;
serviceNames?: string[];
serviceNameHeader?: string;
cacheTtlMs?: number;
};
type RuntimeHealthMiddlewareStartInput = RuntimeHealthOptions | RuntimeHealthMiddlewareStartOptions;
declare class HealthCheckService {
static check(dependencies: Dependency[], params?: HealthCheckerParams): Promise<HealthCheckResult>;
}
declare class RuntimeHealthService {
static getMetrics(options?: RuntimeHealthOptions): Promise<RuntimeHealthMetrics>;
static get(options?: RuntimeHealthOptions): Promise<RuntimeHealthStatus>;
static getStatus(options?: RuntimeHealthOptions): Promise<RuntimeHealthStatus>;
}
declare const RuntimeHealth: typeof RuntimeHealthService;
type FastifyDone = (err?: Error) => void;
type FastifyReply = {
code: (statusCode: number) => FastifyReply;
send: (body: unknown) => void;
};
type ExpressNext = (err?: unknown) => void;
type ExpressResponse = {
status: (code: number) => {
json: (body: unknown) => void;
};
};
declare const RuntimeHealthMiddleware: {
express: {
start: (options?: RuntimeHealthMiddlewareStartInput) => (req: IFrameworkRequest, res: ExpressResponse, next: ExpressNext) => Promise<void>;
};
fastify: {
start: (options?: RuntimeHealthMiddlewareStartInput) => (req: IFrameworkRequest, reply: FastifyReply, done: FastifyDone) => Promise<void>;
};
};
declare const PostgreSQLHealthChecker: IHealthChecker;
declare const RedisHealthChecker: IHealthChecker;
declare const RabbitMQHealthChecker: IHealthChecker<EventBrokerHealthCheckerResult>;
interface IElasticsearchConnection {
host: string;
port?: number;
timeout: number;
authType: 'basic' | 'aws' | 'none';
username?: string;
password?: string;
useAWSAuth?: boolean;
ssl?: {
rejectUnauthorized: boolean;
};
}
declare function getElasticsearchConfig(params?: IWarehouseHealthCheckerParams): IElasticsearchConnection;
declare function getClusterStats(config: IElasticsearchConnection, params?: IWarehouseHealthCheckerParams): Promise<ElasticsearchClusterStats>;
declare function registerHealthCheck(dependency: Dependency | Dependency[]): void;
declare function clearRegistry(): void;
declare const OK: HealthStatus;
declare const FAILED: HealthStatus;
declare const DEFAULT_HEALTH_STATUS: "OK";
declare const VOID: HealthStatus;
export { RedisHealthChecker as CacheHealthChecker, type CheckerRegistry, DEFAULT_HEALTH_STATUS, PostgreSQLHealthChecker as DatabaseHealthChecker, type Dependency, type DependencyHealthCheckerParams, type DependencyHealthDetail, type ElasticsearchClusterStats, RabbitMQHealthChecker as EventBrokerHealthChecker, type EventBrokerHealthCheckerResult, FAILED, type HealthCheckResult, HealthCheckService, type HealthCheckerParams, type HealthStatus, type IEventBrokerParams, type IFrameworkRequest, type IHealthChecker, type IMiddlewareRequest, type IMiddlewareResponse, type IWarehouseHealthCheckerParams, OK, RUNTIME_HEALTH_CACHE_SOURCE, RuntimeHealth, type RuntimeHealthCacheInfo, type RuntimeHealthCacheSource, type RuntimeHealthEnabledMetrics, type RuntimeHealthMetrics, RuntimeHealthMiddleware, type RuntimeHealthMiddlewareStartInput, type RuntimeHealthMiddlewareStartOptions, type RuntimeHealthOptions, type RuntimeHealthResult, RuntimeHealthService, type RuntimeHealthThresholds, VOID, type WarehouseHealthCheckerResult, clearRegistry, getClusterStats, getElasticsearchConfig, registerHealthCheck };