This document describes the implemented API latency monitoring system that automatically alerts on-call engineers when API endpoints breach latency SLOs.
The latency monitoring system tracks P95 latency for all API endpoints and sends alerts when the 5-minute rolling P95 exceeds configured SLO thresholds.
- Real-time latency tracking for all API endpoints
- P95 latency calculation with configurable rolling windows
- SLO breach detection with automatic alerting
- Multiple alert integrations (Slack, PagerDuty)
- Configurable thresholds via environment variables
- Prometheus request error metrics for error-budget burn alerts
- External dependency latency and failure metrics from health probes
- Endpoint normalization for dynamic routes
- Admin endpoints for monitoring status
Add these to your .env file:
# SLO thresholds in milliseconds
SLO_READ_THRESHOLD_MS=200
SLO_WRITE_THRESHOLD_MS=500
# Evaluation window for P95 calculation (5 minutes = 300000 ms)
SLO_EVALUATION_WINDOW_MS=300000
# Alert cooldown period to prevent spam (15 minutes = 900000 ms)
SLO_ALERT_COOLDOWN_MS=900000
# SLO check interval (1 minute = 60000 ms)
SLO_CHECK_INTERVAL_MS=60000
# Alert Integration Configuration
# Set to 'slack', 'pagerduty', or 'both'
ALERT_TYPE=slack
# Slack Webhook URL (required if ALERT_TYPE includes 'slack')
SLACK_WEBHOOK_URL=https://hooks.slack.com/services/YOUR/SLACK/WEBHOOK
# PagerDuty Integration Key (required if ALERT_TYPE includes 'pagerduty')
PAGERDUTY_INTEGRATION_KEY=your-pagerduty-integration-keyEndpoints are automatically categorized as:
Read Endpoints (200ms SLO):
/api/v1/vault/summary/api/v1/vault/metrics/api/v1/vault/apy/api/v1/vault/:id/health/ready/metrics
Write Endpoints (500ms SLO):
/api/v1/vault/deposit/api/v1/vault/withdraw/api/v1/vault/create/admin/cache/invalidate/admin/api-keys/register
- Create a Slack webhook URL
- Set
ALERT_TYPE=slack - Set
SLACK_WEBHOOK_URLto your webhook URL
Sample Slack Alert:
🚨 API Latency SLO Breach Detected
Affected Endpoints:
• /api/v1/vault/summary: P95 = 245.50ms (SLO: 200ms, 45 samples)
Time: 2026-04-25T19:22:19.132Z
Service: YieldVault Backend
- Create a PagerDuty integration
- Set
ALERT_TYPE=pagerduty - Set
PAGERDUTY_INTEGRATION_KEYto your integration key
PagerDuty Alert Details:
- Severity: Critical
- Component: api-latency-monitoring
- Group: performance
- Class: latency-slo
Prometheus alert rules are versioned in
backend/monitoring/prometheus-alerts.yml. Load this file from the Prometheus
configuration used by the deployment. It alerts when critical endpoint P95
latency exceeds the endpoint registry budget, when 5xx traffic burns more than
1% of the critical endpoint error budget over five minutes, or when an external
dependency probe has a P95 latency above two seconds.
The /metrics endpoint exposes:
http_request_error_total, labeled by method, normalized route, status code, and status class.external_dependency_latency_seconds, labeled by dependency, operation, and success or failure outcome.external_dependency_error_total, labeled by dependency and operation.
Returns detailed latency monitoring status and metrics.
Response:
{
"status": {
"endpointsTracked": 12,
"monitoringActive": true,
"alertIntegrations": ["SlackAlert"]
},
"metrics": [
{
"endpoint": "/api/v1/vault/summary",
"type": "read",
"currentP95": 150.25,
"threshold": 200,
"isBreaching": false,
"dataPoints": 45,
"lastAlertTime": null
}
],
"timestamp": "2026-04-25T19:22:19.132Z"
}✅ Alert fires within 5 minutes of an SLO breach
- SLO check interval runs every 1 minute (configurable)
- 5-minute rolling window ensures detection within 5 minutes
✅ Alert message includes required information
- Endpoint name
- Current P95 value
- SLO threshold
- Number of samples
- Timestamp
✅ Alerting channel is configurable via environment variable
ALERT_TYPEcontrols integration (slack/pagerduty/both)- Individual webhook/integration keys configurable
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ Express App │───▶│ Latency Tracker │───▶│ Alert Service │
│ │ │ │ │ │
│ - Middleware │ │ - P95 Calculation │ │ - Slack Webhook │
│ - Route Handler │ │ - SLO Detection │ │ - PagerDuty API │
│ - Metrics │ │ - Rolling Window │ │ - Cooldown │
└─────────────────┘ └──────────────────┘ └─────────────────┘
- Request Processing: Express middleware records latency for each successful request
- Endpoint Normalization: Dynamic paths normalized (e.g.,
/api/v1/vault/123→/api/v1/vault/:id) - Latency Storage: Measurements stored in rolling window per endpoint
- P95 Calculation: P95 calculated for each endpoint's measurements
- SLO Evaluation: P95 compared against endpoint-specific thresholds
- Alert Dispatch: Alerts sent when SLO breached and cooldown expired
- Memory Usage: Only stores latency data within evaluation window
- CPU Usage: Efficient P95 calculation with sorting
- Network: Alert batching and cooldown prevent spam
- Scalability: Per-endpoint tracking scales with API surface area
Run the integration test to verify functionality:
npx tsx src/integration-test.tsRun unit tests (when available):
npm test -- latencyMonitoring.test.tsAlerts not firing:
- Check
ALERT_TYPEconfiguration - Verify webhook/integration keys
- Check if endpoints have enough traffic (need samples for P95)
- Verify SLO thresholds are appropriate
High memory usage:
- Reduce
SLO_EVALUATION_WINDOW_MS - Check for endpoint explosion (too many unique endpoints)
Missing endpoints:
- Verify middleware is properly integrated
- Check if requests are successful (only 2xx-3xx tracked)
- Verify endpoint normalization logic
Enable debug logging by setting LOG_LEVEL=debug in your environment.
- Dynamic SLO adjustment based on traffic patterns
- Multi-dimensional alerting (error rate + latency)
- Historical trend analysis
- Dashboard integration
- Automated SLO recommendations
- Geographic latency tracking
- Admin endpoints require API key authentication
- Webhook URLs should be kept secret
- Alert data doesn't contain sensitive request information
- Rate limiting applies to monitoring endpoints