Skip to main content

Health Module

The Health module is a comprehensive system monitoring and health checking system for the Comdeall platform that provides real-time status monitoring of critical application components and infrastructure dependencies. It integrates with NestJS Terminus for standardized health checks, monitors database connectivity, Redis cache availability, memory usage, and external service dependencies. The module provides both JSON API endpoints for automated monitoring and a web-based dashboard for human-readable status visualization.

Table of Contents

  1. Module Structure
  2. Health Endpoints
  3. Core Features
  4. Health Indicators
  5. Monitoring Components
  6. Web Dashboard
  7. Integration Points
  8. Technical Implementation
  9. Error Handling
  10. Best Practices
  11. Conclusion

Module Structure

The Health module follows a microservice health check pattern using NestJS Terminus:

@Module({
imports: [TerminusModule, HttpModule, RedisModule],
controllers: [HealthController],
providers: [HealthService],
exports: [HealthService],
})
export class HealthModule {}

Core Components:

  1. Controller Layer (health.controller.ts): Exposes health check endpoints for both API consumers and web dashboard

  2. Service Layer (health.service.ts): Orchestrates multiple health indicators and aggregates system status

  3. Health Indicators: Specialized monitoring components for different system aspects (Redis, Database, Memory, HTTP)

  4. Web Dashboard (views/health.pug): Human-readable health status visualization with responsive design

  5. Custom Indicators (redis.health.ts): Application-specific health monitoring for Redis connectivity

Health Endpoints

EndpointMethodDescriptionAuth TypeResponse Format
/healthGETJSON health check APINone (Public)JSON
/health/health-uiGETWeb dashboard interfaceNone (Public)HTML

Health Check API Features:

  • Standardized Format: NestJS Terminus compatible health check responses
  • Comprehensive Status: Overall system status with individual component details
  • Real-time Monitoring: Live status updates for all monitored components
  • Machine Readable: JSON format suitable for automated monitoring systems

Web Dashboard Features:

  • Visual Status Display: Color-coded component status with intuitive indicators
  • Responsive Design: Mobile-compatible interface for on-the-go monitoring
  • Developer Tools Integration: Seamless navigation to other development utilities
  • Real-time Updates: Live status refresh with manual reload capability

Core Features

Comprehensive Health Monitoring

The module provides multi-layered health monitoring covering all critical system components:

System Health Checks:

  • External Connectivity: Internet connectivity verification through Google ping
  • Cache Layer: Redis connectivity and responsiveness monitoring
  • Memory Management: Heap memory usage tracking with configurable thresholds
  • Storage Monitoring: Disk space availability checking (configurable)

Health Check Orchestration:

  • Parallel Execution: Simultaneous health indicator execution for fast response times
  • Timeout Handling: Configurable timeouts prevent hanging health checks
  • Failure Aggregation: Comprehensive status reporting with individual component details
  • Performance Optimization: Efficient health check execution with minimal system impact

Real-time Status Monitoring

Live monitoring capabilities provide immediate visibility into system health:

Status Tracking:

  • Overall Status: Aggregated system health with pass/fail indicators
  • Component Status: Individual service health with detailed error information
  • Performance Metrics: Response time tracking for health check operations
  • Historical Tracking: Status change logging for trend analysis

Automated Monitoring Integration:

  • JSON API: Machine-readable health status for monitoring tools
  • Standard Format: Industry-standard health check response structure
  • Polling Support: Designed for regular automated health monitoring
  • Alert Integration: Compatible with external alerting and monitoring systems

Health Indicators

HTTP Health Indicator

External connectivity monitoring ensures internet access and DNS resolution:

Google Connectivity Check:

async () => this.http.pingCheck('google', 'https://google.com')

Features:

  • DNS Resolution: Verifies domain name resolution capability
  • HTTP Connectivity: Tests outbound HTTP/HTTPS connectivity
  • Response Validation: Confirms successful HTTP response reception
  • Timeout Protection: Prevents hanging on network issues

Redis Health Indicator

Custom Redis health monitoring ensures cache layer availability:

Redis Connectivity Verification:

async isHealthy(key: string): Promise<HealthIndicatorResult> {
await this.redisClient.ping();
return this.getStatus(key, true);
}

Monitoring Capabilities:

  • Connection Status: Active Redis connection verification
  • Response Time: Redis ping latency measurement
  • Error Handling: Graceful failure reporting with detailed error messages
  • Connection Pool: Multiple connection health verification

Memory Health Indicator

System memory monitoring prevents out-of-memory conditions:

Heap Memory Monitoring:

() => this.memory.checkHeap('memory_heap', 250 * 1024 * 1024)

Memory Tracking:

  • Heap Size Monitoring: Current heap memory usage tracking
  • Threshold Alerts: Configurable memory usage thresholds (250MB default)
  • Memory Leak Detection: Trend analysis for memory consumption patterns
  • Performance Impact: Memory usage impact on application performance

Disk Health Indicator

Storage monitoring ensures adequate disk space availability:

Disk Space Monitoring:

// Configurable disk space monitoring
() => this.disk.checkStorage('storage', { path: '/', thresholdPercent: 0.45 })

Storage Features:

  • Available Space: Free disk space percentage monitoring
  • Threshold Configuration: Customizable disk space thresholds (45% default)
  • Path Monitoring: Specific directory or mount point monitoring
  • Capacity Planning: Storage usage trend analysis for capacity planning

Monitoring Components

Health Check Service Integration

The module integrates with NestJS Terminus for standardized health monitoring:

Terminus Integration:

  • Standard Format: Industry-standard health check response format
  • Multiple Indicators: Parallel execution of multiple health indicators
  • Error Aggregation: Comprehensive error collection and reporting
  • Performance Optimization: Efficient health check execution patterns

Custom Health Indicators

Application-specific health indicators provide targeted monitoring:

Redis Health Indicator:

  • Custom Implementation: Tailored Redis connectivity monitoring
  • Error Handling: Detailed Redis-specific error reporting
  • Performance Metrics: Redis response time and connectivity analysis
  • Connection Management: Redis connection pool health verification

Composite Health Status

Aggregated health status provides comprehensive system overview:

Status Aggregation:

  • Overall Health: System-wide health status determination
  • Component Details: Individual service health with specific error information
  • Status Hierarchy: Critical vs non-critical component classification
  • Failure Impact: Component failure impact assessment on overall system health

Web Dashboard

Visual Health Dashboard

The web-based dashboard provides intuitive health status visualization:

Dashboard Features:

  • Bootstrap Integration: Responsive design with modern UI components
  • Color-coded Status: Green (healthy) and red (unhealthy) visual indicators
  • Real-time Display: Live health status with manual refresh capability
  • Component Cards: Individual component status cards with detailed information

Dashboard Styling and UX

Professional dashboard design enhances usability and accessibility:

Design Elements:

  • Fixed Header: Consistent navigation with developer tools integration
  • Status Cards: Clean component status display with border color coding
  • Responsive Layout: Mobile-compatible design for various screen sizes
  • Professional Typography: Clear, readable font choices for system monitoring

Seamless integration with development tools ecosystem:

Navigation Features:

  • Dev Tools Integration: Direct navigation to development utilities
  • Breadcrumb Navigation: Clear navigation hierarchy and context
  • User Context: Developer identification and role display
  • Back Navigation: Easy return to main development tools interface

Integration Points

Application Module Integration

Seamless integration with the main application architecture:

Module Registration:

// App module integration
imports: [
HealthModule, // Health monitoring integration
// Other modules...
]

Global Availability:

  • Application-wide: Health checks available throughout application lifecycle
  • Startup Verification: Health checks during application initialization
  • Runtime Monitoring: Continuous health monitoring during operation
  • Shutdown Graceful: Health check cleanup during application shutdown

External Monitoring Integration

Compatible with external monitoring and alerting systems:

Monitoring Tool Integration:

  • Prometheus Compatibility: Health check metrics export for Prometheus monitoring
  • Nagios Integration: Standard health check format for Nagios monitoring
  • Custom Monitoring: Flexible JSON API for custom monitoring solutions
  • Alert Manager: Compatible with various alerting and notification systems

Load Balancer Integration

Health check endpoints support load balancer health verification:

Load Balancer Features:

  • Health Check Endpoint: Standard /health endpoint for load balancer monitoring
  • Fast Response: Optimized health checks for frequent polling
  • Failure Detection: Rapid unhealthy instance detection and removal
  • Service Discovery: Health status integration with service discovery systems

Technical Implementation

Health Check Execution Strategy

Optimized health check execution ensures minimal performance impact:

Execution Patterns:

  • Parallel Processing: Simultaneous execution of multiple health indicators
  • Timeout Management: Configurable timeouts prevent system hanging
  • Resource Optimization: Minimal resource usage during health check execution
  • Caching Strategy: Intelligent caching for frequently accessed health data

Error Handling and Reporting

Comprehensive error handling provides detailed failure information:

Error Management:

// Redis health check error handling
catch (error) {
throw new HealthCheckError('Redis check failed',
this.getStatus(key, false, { message: error.message }));
}

Error Features:

  • Detailed Messages: Specific error information for troubleshooting
  • Error Classification: Different error types with appropriate handling
  • Graceful Degradation: System continues operation despite individual component failures
  • Debug Information: Comprehensive error details for development and debugging

Performance Optimization

Health check performance optimization ensures minimal system impact:

Optimization Strategies:

  • Efficient Indicators: Lightweight health check implementations
  • Connection Reuse: Redis and database connection pooling for efficiency
  • Timeout Configuration: Appropriate timeout values prevent resource waste
  • Monitoring Overhead: Minimal performance impact on application operations

Error Handling

The module implements comprehensive error handling for reliable health monitoring:

Error Categories:

  • Network Errors: Connectivity issues with external services and dependencies
  • Resource Errors: Memory, disk, and system resource availability issues
  • Service Errors: Database, cache, and application service connectivity problems
  • Configuration Errors: Invalid configuration or missing dependency issues

Error Response Format: All health check errors provide structured information including error type, component name, detailed error messages, and suggested resolution steps for operational teams.

Best Practices

Health Check Design

Strategic health check design ensures meaningful and actionable monitoring:

Design Principles:

  • Critical Path Focus: Monitor components essential for application functionality
  • Performance Considerations: Health checks should not impact application performance
  • Timeout Configuration: Appropriate timeout values prevent hanging health checks
  • Error Specificity: Detailed error messages enable rapid issue resolution

Monitoring Strategy

Effective health monitoring strategy supports proactive system management:

Monitoring Approach:

  • Automated Polling: Regular automated health check execution by monitoring systems
  • Alert Thresholds: Appropriate alert thresholds prevent alert fatigue
  • Escalation Policies: Clear escalation procedures for different health check failures
  • Documentation: Comprehensive documentation for health check interpretation and response

Operational Excellence

Health monitoring operational excellence ensures reliable system monitoring:

Operational Practices:

  • Regular Testing: Health check validation and testing procedures
  • Dashboard Monitoring: Regular dashboard review and status verification
  • Alert Response: Clear procedures for health check alert response and resolution
  • Capacity Planning: Health trend analysis for capacity planning and optimization

Conclusion

The Health module provides a comprehensive monitoring foundation for the Comdeall platform, ensuring reliable system operation and proactive issue detection. Key strengths include:

Comprehensive Monitoring:

  • Multi-layered Health Checks: Database, cache, memory, and external service monitoring
  • Real-time Status: Live system health visibility with immediate failure detection
  • Visual Dashboard: Intuitive web interface for human-readable status monitoring
  • API Integration: Machine-readable health status for automated monitoring systems

Production-Ready Features:

  • NestJS Terminus Integration: Industry-standard health check framework
  • Performance Optimized: Minimal overhead health monitoring with efficient execution
  • Error Handling: Comprehensive error reporting with detailed failure information
  • Scalable Architecture: Supports high-frequency health check polling without performance impact

Operational Excellence:

  • Load Balancer Integration: Standard health endpoints for infrastructure integration
  • Monitoring Tool Compatibility: Compatible with Prometheus, Nagios, and custom monitoring solutions
  • Developer Experience: Web dashboard integration with development tools ecosystem
  • Alert Integration: Supports various alerting and notification systems for proactive monitoring

The module's architecture enables reliable system monitoring, rapid issue detection, and proactive system management essential for production-grade applications serving the child development and therapy management platform.