Implementing Metrics and Analytics
1. Tracking Request Count Metrics
| Metric | Type | Labels |
|---|---|---|
gateway_requests_total | Counter | route, method, status |
gateway_active_requests | Gauge | route |
gateway_request_bytes_total | Counter | route |
Example: Prometheus query
sum(rate(gateway_requests_total{status=~"5.."}[5m])) by (route)
/
sum(rate(gateway_requests_total[5m])) by (route)
# Error rate per route
2. Measuring Response Time
| Metric | Type |
|---|---|
request_duration_seconds | Histogram |
upstream_duration_seconds | Histogram |
tls_handshake_duration | Histogram |
| Bucket boundaries | 5ms,10,25,50,100,250,500,1s,2.5,5,10 |
3. Monitoring Error Rates
| SLO | Target |
|---|---|
| Availability | 99.95% (~22 min/mo) |
| 5xx rate | < 0.1% |
| 429 rate | < 1% |
| 4xx (excl 429) | Track but no SLO |
4. Tracking Throughput
| Metric | Use |
|---|---|
| RPS per route | Capacity planning |
| Bytes in/out | Bandwidth billing |
| Concurrent conns | Connection pool sizing |
| Goodput (2xx only) | Effective service rate |
5. Measuring Cache Hit Rates
| Metric | Formula |
|---|---|
| Hit ratio | hits / (hits + misses) |
| Byte hit ratio | cached_bytes / total_bytes |
| Origin offload | 1 - (origin_reqs / total) |
6. Monitoring Backend Health Status
| Metric | Description |
|---|---|
upstream_healthy_targets | Gauge per upstream |
upstream_health_check_failures | Counter |
circuit_breaker_open | 0/1 gauge |
upstream_connect_errors | Counter |
7. Tracking Rate Limit Usage
| Metric | Use |
|---|---|
rate_limit_hits | Per consumer/route |
rate_limit_rejections | 429s |
quota_used_percent | Per plan |
| Top-N consumers | Identify heavy users |
8. Measuring Authentication Failures
| Counter | Alert If |
|---|---|
auth_invalid_token | > baseline 3x |
auth_expired_token | Spikes (clock skew?) |
auth_brute_force | Per-IP failures |
jwks_fetch_errors | IdP outage |
9. Monitoring Resource Utilization
| Resource | Threshold |
|---|---|
| CPU | < 70% sustained |
| Memory | < 75% |
| File descriptors | < 80% of ulimit |
| Connections | < 80% of pool |
| Network bandwidth | < 70% link |