UNPKG

@ahmedhegazee/nestjs-telescope

Version:

Advanced observability and monitoring solution for NestJS applications with ML-powered analytics, enterprise features, and production-ready scaling

89 lines (88 loc) 2.98 kB
import { OnModuleInit, OnModuleDestroy } from '@nestjs/common'; import { DataSource } from 'typeorm'; import { Observable } from 'rxjs'; export interface ConnectionPoolMetrics { totalConnections: number; activeConnections: number; idleConnections: number; waitingConnections: number; acquiredConnections: number; releasedConnections: number; createdConnections: number; destroyedConnections: number; poolSize: number; maxConnections: number; minConnections: number; connectionTimeouts: number; connectionErrors: number; averageAcquireTime: number; averageConnectionLifetime: number; healthScore: number; lastUpdate: Date; } export interface ConnectionEvent { type: 'acquire' | 'release' | 'create' | 'destroy' | 'timeout' | 'error'; timestamp: Date; connectionId?: string; duration?: number; error?: string; poolState: { active: number; idle: number; waiting: number; }; } export interface ConnectionPoolAlert { type: 'high_usage' | 'connection_timeout' | 'connection_error' | 'pool_exhausted' | 'connection_leak'; severity: 'low' | 'medium' | 'high' | 'critical'; message: string; timestamp: Date; metrics: Partial<ConnectionPoolMetrics>; recommendation: string; } export declare class ConnectionPoolMonitorService implements OnModuleInit, OnModuleDestroy { private readonly dataSource; private readonly logger; private readonly destroy$; private readonly metricsSubject; private readonly eventsSubject; private readonly alertsSubject; private connectionEvents; private readonly maxEventHistory; private readonly monitoringInterval; private acquireTimestamps; private connectionCreationTimes; private currentMetrics; private alertThresholds; constructor(dataSource: DataSource); onModuleInit(): Promise<void>; onModuleDestroy(): Promise<void>; private initializeMetrics; private setupMonitoring; private setupPoolEventListeners; private setupPostgresPoolListeners; private setupMySQLPoolListeners; private getCurrentPoolState; private updatePoolConfiguration; private startPeriodicMonitoring; private collectMetrics; private calculateAverageAcquireTime; private calculateAverageConnectionLifetime; private calculateHealthScore; private updateCountersFromEvents; private checkForAlerts; private recordEvent; private generateConnectionId; getMetrics(): ConnectionPoolMetrics; getMetricsStream(): Observable<ConnectionPoolMetrics>; getEventStream(): Observable<ConnectionEvent>; getAlertStream(): Observable<ConnectionPoolAlert>; getRecentEvents(limit?: number): ConnectionEvent[]; getConnectionPoolHealth(): { score: number; status: 'healthy' | 'warning' | 'critical'; issues: string[]; recommendations: string[]; }; resetMetrics(): void; }