Issue: The correlation and logging middleware were not registered in the main server setup.
Fix: Updated src/index.ts to register:
registerCorrelationMiddleware()- First in the middleware chainregisterRequestLoggingMiddleware()- After correlation middlewareregisterHealthCheckRoutes()- For health check endpointsregisterMetricsEndpoint()- For Prometheus metrics
Issue: The server already had a simple /health endpoint that returned { status: "ok" }.
Fix: Removed the old endpoint and replaced it with the comprehensive health check service that provides:
/health- Full system health status/ready- Readiness probe for Kubernetes/live- Liveness probe for Kubernetes
Issue: The traceContextMap could grow indefinitely, causing memory leaks in long-running processes.
Fix: Added automatic cleanup mechanism:
- Cleanup interval runs every 60 seconds
- Removes trace contexts older than 5 minutes
- Added
stopCleanup()method for graceful shutdown
Issue: The health check service tried to import Redis asynchronously at the module level, which is not allowed in ES modules. Fix: Implemented lazy loading:
- Created
getRedis()async function - Redis is loaded on first health check call
- Gracefully handles missing Redis configuration
Issue: Request body is not available in the onRequest hook - it's only available after parsing.
Fix: Simplified logging approach:
- Removed request body logging from
onRequesthook - Kept response body logging capability
- Added proper timing with
startTimetracking
Issue: The metrics service wasn't importing the config module.
Fix: Added import { config } from '../config/index.js' to metrics service.
- Error Handling: All middleware includes try-catch blocks with proper logging
- Type Safety: Full TypeScript interfaces for all data structures
- Configuration: All thresholds and timeouts are configurable via environment variables
- Memory Management: Automatic cleanup of trace contexts to prevent memory leaks
- Graceful Degradation: Health checks handle missing dependencies (Redis) gracefully
All required environment variables are already defined in src/config/index.ts:
LOG_LEVEL: 'info' (default)
LOG_FILE: optional
LOG_MAX_FILE_SIZE: 100MB (default)
LOG_MAX_FILES: 10 (default)
LOG_RETENTION_DAYS: 30 (default)
LOG_REQUEST_BODY: false (default)
LOG_RESPONSE_BODY: false (default)
LOG_SENSITIVE_DATA: false (default)
REQUEST_SLOW_THRESHOLD_MS: 1000 (default)
HEALTH_CHECK_MEMORY_THRESHOLD: 90 (default)
HEALTH_CHECK_DISK_THRESHOLD: 80 (default)
HEALTH_CHECK_TIMEOUT_MS: 5000 (default)-
feat: build logging and monitoring infrastructure (5798812)
- Initial implementation of all components
-
fix: integrate logging and monitoring middleware into server (8024a42)
- Register middleware in correct order
- Replace duplicate health endpoint
-
fix: improve logging middleware and add memory leak prevention (19dcbdf)
- Add cleanup interval to TraceManager
- Fix request timing calculation
- Improve error handling
-
fix: resolve async import issue in health check service (b3ee8cb)
- Implement lazy loading for Redis
- Fix module-level async import issue
-
Unit Tests: Test each component in isolation
- Logger JSON output format
- Correlation ID generation and propagation
- Metrics recording accuracy
- Health check status calculation
-
Integration Tests: Test end-to-end flows
- Request flows through all middleware
- Correlation ID present in all logs
- Metrics recorded correctly
- Health endpoints return proper status codes
-
Load Tests: Verify performance under load
- Memory usage with cleanup interval
- Metrics collection performance
- Health check response times
- Verify all environment variables are set in deployment
- Ensure log file directory has proper permissions
- Configure log rotation policy based on disk space
- Set up log aggregation (ELK/Datadog) if needed
- Configure Prometheus scraping for
/metricsendpoint - Set up Kubernetes probes using
/readyand/liveendpoints - Monitor memory usage with cleanup interval enabled
- Test health checks in staging environment
- Run full test suite to verify no regressions
- Deploy to staging environment
- Monitor logs and metrics in production
- Implement additional business metrics as needed
- Set up alerting based on health check status