@ahmedhegazee/nestjs-telescope
Version:
Advanced observability and monitoring solution for NestJS applications with ML-powered analytics, enterprise features, and production-ready scaling
131 lines (130 loc) • 3.94 kB
TypeScript
import { OnModuleInit } from '@nestjs/common';
import { Observable } from 'rxjs';
import { TelescopeConfig } from '../interfaces/telescope-config.interface';
export interface MemoryConfig {
enabled: boolean;
monitoring: {
enabled: boolean;
interval: number;
threshold: {
heapUsed: number;
heapTotal: number;
external: number;
rss: number;
};
};
optimization: {
autoGc: boolean;
gcThreshold: number;
leakDetection: boolean;
memoryPooling: boolean;
compression: boolean;
};
alerts: {
enabled: boolean;
criticalThreshold: number;
warningThreshold: number;
};
}
export interface MemoryMetrics {
timestamp: Date;
heapUsed: number;
heapTotal: number;
heapFree: number;
external: number;
rss: number;
arrayBuffers: number;
heapUsedPercentage: number;
heapTotalPercentage: number;
externalPercentage: number;
rssPercentage: number;
gc: {
count: number;
duration: number;
type: string;
};
}
export interface MemoryLeak {
id: string;
type: 'object' | 'array' | 'function' | 'closure' | 'event' | 'heap' | 'external';
location: string;
size: number;
growth: number;
firstDetected: Date;
lastSeen: Date;
severity: 'low' | 'medium' | 'high' | 'critical';
status: 'active' | 'resolved' | 'investigating';
stackTrace?: string;
}
export interface MemoryOptimization {
type: 'gc' | 'compression' | 'cleanup' | 'pooling';
timestamp: Date;
duration: number;
freedMemory: number;
improvement: number;
success: boolean;
error?: string;
}
export interface MemoryHealth {
status: 'healthy' | 'warning' | 'critical';
score: number;
issues: string[];
recommendations: string[];
metrics: MemoryMetrics;
}
export declare class MemoryOptimizerService implements OnModuleInit {
private readonly telescopeConfig;
private readonly logger;
private readonly memoryHistory;
private readonly memoryLeaks;
private readonly optimizations;
private readonly metricsSubject;
private readonly healthSubject;
private readonly leakSubject;
private readonly optimizationSubject;
private readonly config;
private monitoringInterval;
private optimizationInterval;
private gcCount;
private lastGcTime;
constructor(telescopeConfig: TelescopeConfig);
onModuleInit(): Promise<void>;
private getDefaultMemoryConfig;
private initializeOptimizer;
private initializeMemoryPools;
private initializeLeakDetection;
private startMonitoring;
private startOptimization;
private setupGcMonitoring;
private getCurrentMemoryMetrics;
private getGcStats;
private checkMemoryHealth;
private calculateMemoryHealth;
private calculateMemoryGrowthTrend;
private checkMemoryAlerts;
private detectMemoryLeaks;
private analyzeMemoryGrowthPattern;
private investigatePotentialLeak;
private runOptimizationCycle;
private shouldRunGc;
private triggerGarbageCollection;
private createMemoryPressure;
private compressMemory;
private performMemoryCompression;
private cleanupMemoryPools;
private performPoolCleanup;
private resolveMemoryLeaks;
private calculateLeakGrowthTrend;
private formatBytes;
getMemoryMetrics(): MemoryMetrics[];
getCurrentMetrics(): MemoryMetrics;
getMemoryLeaks(): MemoryLeak[];
getOptimizations(): MemoryOptimization[];
getMetricsUpdates(): Observable<MemoryMetrics>;
getHealthUpdates(): Observable<MemoryHealth>;
getLeakUpdates(): Observable<MemoryLeak>;
getOptimizationUpdates(): Observable<MemoryOptimization>;
forceGarbageCollection(): Promise<MemoryOptimization>;
resolveLeak(leakId: string): Promise<boolean>;
shutdown(): Promise<void>;
}