This module implements an automated response system for common incidents with the following capabilities:
- Automatically detects incidents based on alert patterns
- Analyzes alert severity and consecutive occurrences
- Creates incident records with appropriate severity levels
- Tracks trigger metrics and detection statistics
- Executes predefined remediation actions automatically
- Supports multiple action types:
- Service restart
- Cache clearing
- Resource scaling
- Database operations
- Auto-rollback capability for failed actions
- Tracks all remediation attempts and results
- Executes predefined runbook procedures
- Supports standard runbooks:
- Database failure recovery
- Region outage failover
- Data corruption recovery
- Tracks step-by-step execution progress
- Generates execution summaries
- Multi-channel notifications (Email, Slack, PagerDuty, Webhooks)
- Severity-based escalation policies
- Auto-escalation after time thresholds
- Incident resolution notifications
- Configurable recipient lists
src/incident-management/
├── entities/ # Database models
│ ├── incident.entity.ts # Incident records
│ ├── remediation-action.entity.ts # Remediation action history
│ └── runbook-execution.entity.ts # Runbook execution logs
├── dto/ # Data transfer objects
│ ├── incident.dto.ts
│ ├── remediation-action.dto.ts
│ └── runbook-execution.dto.ts
├── services/ # Core services
│ ├── incident-detection.service.ts # Alert processing & pattern matching
│ ├── auto-remediation.service.ts # Remediation action execution
│ ├── runbook-execution.service.ts # Runbook orchestration
│ └── notification-and-escalation.service.ts # Notifications
├── tests/ # Unit tests
│ ├── incident-detection.service.spec.ts
│ ├── auto-remediation.service.spec.ts
│ └── runbook-execution.service.spec.ts
├── incident-management.service.ts # Main orchestration service
├── incident-management.controller.ts # REST API endpoints
└── incident-management.module.ts # Module definition
POST /incidents- Create incidentGET /incidents- List incidents (with filtering by status/severity)GET /incidents/:id- Get incident detailsPUT /incidents/:id- Update incidentPOST /incidents/:id/resolve- Resolve incidentPOST /incidents/:id/escalate- Escalate incident
POST /incidents/:id/remediation-actions- Create remediation actionGET /incidents/:id/remediation-actions- List remediation actions
POST /incidents/:id/runbook-executions- Execute runbookGET /incidents/:id/runbook-executions- List runbook executionsGET /incidents/runbooks/available- List available runbooks
GET /incidents/statistics/overview- Get incident management statistics
The module is automatically imported in app.module.ts.
# Migrations are auto-run on startup
npm run start:devcurl -X POST http://localhost:3000/incidents \
-H 'Content-Type: application/json' \
-d '{
"title": "High HTTP Error Rate",
"description": "Error rate exceeded threshold",
"severity": "critical",
"runbookId": "error-rate-investigation"
}'The system includes built-in detection rules for:
- Database performance degradation
- High CPU/Memory utilization
- High HTTP error rates
- Cache hit rate degradation
- Queue processing delays
- API latency issues
Add custom rules by extending INCIDENT_DETECTION_RULES in incident-detection.service.ts.
// In auto-remediation.service.ts, add to handlers array:
class CustomHandler implements RemediationHandler {
canHandle(actionType: string): boolean {
return actionType === 'custom_action';
}
async execute(parameters): Promise<...> {
// Implementation
}
}const policy: EscalationPolicy = {
delayMs: 2 * 60 * 1000,
severity: IncidentSeverity.WARNING,
recipients: [{
channel: NotificationChannel.EMAIL,
address: 'custom-team@example.com'
}],
maxRetries: 2
};
notificationService.registerEscalationPolicy('custom', policy);npm testnpm test -- src/incident-management/tests/incident-detection.service.spec.tsnpm run test:ciFor detailed step-by-step testing and validation, see INCIDENT_MANAGEMENT_TESTING_GUIDE.md
Detection → Remediation → Runbook → Notification → Escalation → Resolution
↓ ↓ ↓ ↓ ↓ ↓
Alert Auto Actions Execute Notify Team Critical Issues Resolved
Pattern Triggered Procedures Channels Escalated Tracked
Track incident management metrics:
- Total incidents created
- Active vs. resolved incidents
- Remediation success rate
- Average resolution time
- Escalation frequency
- Detection accuracy
- Incident data stored securely in database
- Authentication required for API endpoints (add via guards)
- Sensitive parameters not logged
- Escalation policies configurable per environment
Optional configuration:
EMAIL_HOST=smtp.example.com
EMAIL_PORT=587
EMAIL_USER=notifications@example.com
EMAIL_PASSWORD=password
EMAIL_FROM=incidents@teachlink.io
SLACK_WEBHOOK_URL=https://hooks.slack.com/...
PAGERDUTY_INTEGRATION_KEY=key-here
# Incident management specific
INCIDENT_AUTO_REMEDIATE=true
INCIDENT_AUTO_ESCALATE=true
To extend the incident management system:
- Add new detection rules in
incident-detection.service.ts - Implement custom remediation handlers
- Create new runbook definitions in
dr/runbooks/ - Add tests for new functionality
- Update documentation
For issues or questions:
- Check the testing guide: INCIDENT_MANAGEMENT_TESTING_GUIDE.md
- Review test cases for usage examples
- Check application logs for errors
- Verify database migrations completed